[51587aa] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[3906301] | 7 | // SemanticError.cc -- |
---|
[51587aa] | 8 | // |
---|
[af39199d] | 9 | // Author : Thierry Delisle |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[01aeade] | 11 | // Last Modified By : Peter A. Buhr |
---|
[b1f2007] | 12 | // Last Modified On : Mon Dec 11 15:59:09 2023 |
---|
| 13 | // Update Count : 14 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
[244b934] | 16 | #include <cstdarg> |
---|
[9ed4f94] | 17 | #include <cstdio> // for fileno, stderr |
---|
[68e9ace] | 18 | #include <cstring> |
---|
[9ed4f94] | 19 | #include <unistd.h> // for isatty |
---|
| 20 | #include <iostream> // for basic_ostream, operator<<, ostream |
---|
| 21 | #include <list> // for list, _List_iterator |
---|
| 22 | #include <string> // for string, operator<<, operator+, to_string |
---|
[68e9ace] | 23 | #include <vector> |
---|
[51b7345] | 24 | |
---|
[9ed4f94] | 25 | #include "Common/utility.h" // for to_string, CodeLocation (ptr only) |
---|
[51b7345] | 26 | #include "SemanticError.h" |
---|
| 27 | |
---|
[68e9ace] | 28 | //----------------------------------------------------------------------------- |
---|
| 29 | // Severity Handling |
---|
| 30 | std::vector<Severity> & get_severities() { |
---|
| 31 | static std::vector<Severity> severities; |
---|
| 32 | if(severities.empty()) { |
---|
| 33 | severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS); |
---|
| 34 | for ( const auto w : WarningFormats ) { |
---|
| 35 | severities.push_back( w.default_severity ); |
---|
| 36 | } // for |
---|
| 37 | } |
---|
| 38 | return severities; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void SemanticWarning_SuppressAll() { |
---|
| 42 | for( auto & s : get_severities() ) { |
---|
| 43 | s = Severity::Suppress; |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | void SemanticWarning_EnableAll() { |
---|
| 48 | for( auto & s : get_severities() ) { |
---|
| 49 | s = Severity::Warn; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void SemanticWarning_WarningAsError() { |
---|
| 54 | for( auto & s : get_severities() ) { |
---|
| 55 | if(s == Severity::Warn) s = Severity::Error; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void SemanticWarning_Set(const char * const name, Severity s) { |
---|
| 60 | size_t idx = 0; |
---|
[af39199d] | 61 | for ( const auto & w : WarningFormats ) { |
---|
[68e9ace] | 62 | if ( std::strcmp( name, w.name ) == 0 ) { |
---|
| 63 | get_severities()[idx] = s; |
---|
| 64 | break; |
---|
| 65 | } |
---|
| 66 | idx++; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | //----------------------------------------------------------------------------- |
---|
| 71 | // Semantic Error |
---|
[b1f2007] | 72 | |
---|
[4358c1e] | 73 | bool SemanticErrorThrow = false; |
---|
| 74 | |
---|
[a16764a6] | 75 | SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) { |
---|
[d55d7a6] | 76 | append( location, error ); |
---|
[51b7345] | 77 | } |
---|
| 78 | |
---|
[a16764a6] | 79 | void SemanticErrorException::append( SemanticErrorException &other ) { |
---|
[138e29e] | 80 | errors.splice( errors.end(), other.errors ); |
---|
[3906301] | 81 | } |
---|
| 82 | |
---|
[a16764a6] | 83 | void SemanticErrorException::append( CodeLocation location, const std::string & msg ) { |
---|
[d55d7a6] | 84 | errors.emplace_back( location, msg ); |
---|
[51b7345] | 85 | } |
---|
| 86 | |
---|
[a16764a6] | 87 | bool SemanticErrorException::isEmpty() const { |
---|
[01aeade] | 88 | return errors.empty(); |
---|
[51b7345] | 89 | } |
---|
| 90 | |
---|
[a16764a6] | 91 | void SemanticErrorException::print() { |
---|
[138e29e] | 92 | using std::to_string; |
---|
[ddcedfe] | 93 | |
---|
| 94 | errors.sort([](const error & lhs, const error & rhs) -> bool { |
---|
| 95 | if(lhs.location.startsBefore(rhs.location)) return true; |
---|
| 96 | if(rhs.location.startsBefore(lhs.location)) return false; |
---|
| 97 | |
---|
| 98 | return lhs.description < rhs.description; |
---|
| 99 | }); |
---|
| 100 | |
---|
[9ed4f94] | 101 | for( auto err : errors ) { |
---|
[a16764a6] | 102 | std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl; |
---|
[138e29e] | 103 | } |
---|
[51b7345] | 104 | } |
---|
[01aeade] | 105 | |
---|
[b1f2007] | 106 | void SemanticError( CodeLocation location, const char * fmt, ... ) { |
---|
| 107 | char msg[2048]; // worst-case error-message buffer |
---|
| 108 | va_list args; |
---|
| 109 | va_start( args, fmt ); |
---|
| 110 | vsnprintf( msg, sizeof(msg), fmt, args ); // always null terminated, but may be truncated |
---|
| 111 | va_end( args ); |
---|
| 112 | |
---|
| 113 | SemanticErrorThrow = true; |
---|
| 114 | throw SemanticErrorException( location, msg ); // convert msg to string |
---|
| 115 | } |
---|
| 116 | |
---|
[a16764a6] | 117 | void SemanticError( CodeLocation location, std::string error ) { |
---|
[4358c1e] | 118 | SemanticErrorThrow = true; |
---|
[e5d5272] | 119 | throw SemanticErrorException( location, error ); |
---|
[a16764a6] | 120 | } |
---|
| 121 | |
---|
[2103a51] | 122 | namespace { |
---|
| 123 | // convert format string and arguments into a single string |
---|
| 124 | std::string fmtToString(const char * fmt, va_list ap) { |
---|
| 125 | int size = 128; |
---|
| 126 | while ( true ) { |
---|
| 127 | char buf[size]; |
---|
| 128 | va_list args; |
---|
| 129 | va_copy( args, ap ); |
---|
| 130 | int n = vsnprintf(&buf[0], size, fmt, args); |
---|
| 131 | va_end( args ); |
---|
| 132 | if ( n < size && n >= 0 ) return buf; |
---|
| 133 | size *= 2; |
---|
| 134 | } |
---|
| 135 | assert( false ); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
[6040e67d] | 139 | void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) { |
---|
[68e9ace] | 140 | Severity severity = get_severities()[(int)warning]; |
---|
[6040e67d] | 141 | switch(severity) { |
---|
| 142 | case Severity::Suppress : |
---|
| 143 | break; |
---|
| 144 | case Severity::Warn : |
---|
| 145 | { |
---|
| 146 | va_list args; |
---|
| 147 | va_start(args, fmt); |
---|
| 148 | std::string msg = fmtToString( fmt, args ); |
---|
| 149 | va_end(args); |
---|
| 150 | std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl; |
---|
| 151 | } |
---|
| 152 | break; |
---|
| 153 | case Severity::Error : |
---|
| 154 | { |
---|
| 155 | va_list args; |
---|
| 156 | va_start(args, fmt); |
---|
| 157 | std::string msg = fmtToString( fmt, args ); |
---|
| 158 | va_end(args); |
---|
| 159 | SemanticError(location, msg); |
---|
| 160 | } |
---|
| 161 | break; |
---|
| 162 | case Severity::Critical : |
---|
| 163 | assertf(false, "Critical errors not implemented yet"); |
---|
| 164 | break; |
---|
| 165 | } |
---|
[a16764a6] | 166 | } |
---|
| 167 | |
---|
| 168 | //----------------------------------------------------------------------------- |
---|
| 169 | // Helpers |
---|
| 170 | namespace ErrorHelpers { |
---|
[1a69a90] | 171 | Colors colors = Colors::Auto; |
---|
| 172 | |
---|
| 173 | static inline bool with_colors() { |
---|
| 174 | return colors == Colors::Auto ? isatty( STDERR_FILENO ) : bool(colors); |
---|
| 175 | } |
---|
| 176 | |
---|
[a16764a6] | 177 | const std::string & error_str() { |
---|
[1a69a90] | 178 | static std::string str = with_colors() ? "\e[31merror:\e[39m " : "error: "; |
---|
[a16764a6] | 179 | return str; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | const std::string & warning_str() { |
---|
[1a69a90] | 183 | static std::string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: "; |
---|
[a16764a6] | 184 | return str; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | const std::string & bold_ttycode() { |
---|
[1a69a90] | 188 | static std::string str = with_colors() ? "\e[1m" : ""; |
---|
[a16764a6] | 189 | return str; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | const std::string & reset_font_ttycode() { |
---|
[1a69a90] | 193 | static std::string str = with_colors() ? "\e[0m" : ""; |
---|
[a16764a6] | 194 | return str; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | std::string make_bold( const std::string & str ) { |
---|
| 198 | return bold_ttycode() + str + reset_font_ttycode(); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | std::ostream & operator<<(std::ostream & os, bold) { |
---|
| 202 | os << bold_ttycode(); |
---|
| 203 | return os; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | std::ostream & operator<<(std::ostream & os, reset_font) { |
---|
| 207 | os << reset_font_ttycode(); |
---|
| 208 | return os; |
---|
| 209 | } |
---|
[294647b] | 210 | } |
---|
| 211 | |
---|
[51587aa] | 212 | // Local Variables: // |
---|
| 213 | // tab-width: 4 // |
---|
| 214 | // mode: c++ // |
---|
| 215 | // compile-command: "make install" // |
---|
| 216 | // End: // |
---|