[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 |
---|
[610354a] | 12 | // Last Modified On : Thu Dec 14 13:45:28 2023 |
---|
| 13 | // Update Count : 34 |
---|
[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 | |
---|
[610354a] | 25 | using namespace std; |
---|
| 26 | |
---|
[9ed4f94] | 27 | #include "Common/utility.h" // for to_string, CodeLocation (ptr only) |
---|
[51b7345] | 28 | #include "SemanticError.h" |
---|
| 29 | |
---|
[68e9ace] | 30 | //----------------------------------------------------------------------------- |
---|
| 31 | // Severity Handling |
---|
[610354a] | 32 | vector<Severity> & get_severities() { |
---|
| 33 | static vector<Severity> severities; |
---|
[68e9ace] | 34 | if(severities.empty()) { |
---|
| 35 | severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS); |
---|
| 36 | for ( const auto w : WarningFormats ) { |
---|
| 37 | severities.push_back( w.default_severity ); |
---|
| 38 | } // for |
---|
| 39 | } |
---|
| 40 | return severities; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | void SemanticWarning_SuppressAll() { |
---|
| 44 | for( auto & s : get_severities() ) { |
---|
| 45 | s = Severity::Suppress; |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void SemanticWarning_EnableAll() { |
---|
| 50 | for( auto & s : get_severities() ) { |
---|
| 51 | s = Severity::Warn; |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void SemanticWarning_WarningAsError() { |
---|
| 56 | for( auto & s : get_severities() ) { |
---|
| 57 | if(s == Severity::Warn) s = Severity::Error; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void SemanticWarning_Set(const char * const name, Severity s) { |
---|
| 62 | size_t idx = 0; |
---|
[af39199d] | 63 | for ( const auto & w : WarningFormats ) { |
---|
[610354a] | 64 | if ( strcmp( name, w.name ) == 0 ) { |
---|
[68e9ace] | 65 | get_severities()[idx] = s; |
---|
| 66 | break; |
---|
| 67 | } |
---|
| 68 | idx++; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | //----------------------------------------------------------------------------- |
---|
| 73 | // Semantic Error |
---|
[b1f2007] | 74 | |
---|
[4358c1e] | 75 | bool SemanticErrorThrow = false; |
---|
| 76 | |
---|
[610354a] | 77 | SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) { |
---|
[d55d7a6] | 78 | append( location, error ); |
---|
[51b7345] | 79 | } |
---|
| 80 | |
---|
[a16764a6] | 81 | void SemanticErrorException::append( SemanticErrorException &other ) { |
---|
[138e29e] | 82 | errors.splice( errors.end(), other.errors ); |
---|
[3906301] | 83 | } |
---|
| 84 | |
---|
[610354a] | 85 | void SemanticErrorException::append( CodeLocation location, const string & msg ) { |
---|
[d55d7a6] | 86 | errors.emplace_back( location, msg ); |
---|
[51b7345] | 87 | } |
---|
| 88 | |
---|
[a16764a6] | 89 | bool SemanticErrorException::isEmpty() const { |
---|
[01aeade] | 90 | return errors.empty(); |
---|
[51b7345] | 91 | } |
---|
| 92 | |
---|
[a16764a6] | 93 | void SemanticErrorException::print() { |
---|
[610354a] | 94 | // using to_string; |
---|
[ddcedfe] | 95 | |
---|
| 96 | errors.sort([](const error & lhs, const error & rhs) -> bool { |
---|
| 97 | if(lhs.location.startsBefore(rhs.location)) return true; |
---|
| 98 | if(rhs.location.startsBefore(lhs.location)) return false; |
---|
| 99 | |
---|
| 100 | return lhs.description < rhs.description; |
---|
| 101 | }); |
---|
| 102 | |
---|
[9ed4f94] | 103 | for( auto err : errors ) { |
---|
[610354a] | 104 | cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << endl; |
---|
[138e29e] | 105 | } |
---|
[51b7345] | 106 | } |
---|
[01aeade] | 107 | |
---|
[b1f2007] | 108 | void SemanticError( CodeLocation location, const char * fmt, ... ) { |
---|
| 109 | char msg[2048]; // worst-case error-message buffer |
---|
| 110 | va_list args; |
---|
| 111 | va_start( args, fmt ); |
---|
| 112 | vsnprintf( msg, sizeof(msg), fmt, args ); // always null terminated, but may be truncated |
---|
| 113 | va_end( args ); |
---|
| 114 | |
---|
| 115 | SemanticErrorThrow = true; |
---|
| 116 | throw SemanticErrorException( location, msg ); // convert msg to string |
---|
| 117 | } |
---|
| 118 | |
---|
[610354a] | 119 | void SemanticWarning( CodeLocation location, Warning warning, ... ) { |
---|
[68e9ace] | 120 | Severity severity = get_severities()[(int)warning]; |
---|
[610354a] | 121 | |
---|
| 122 | switch ( severity ) { |
---|
[6040e67d] | 123 | case Severity::Suppress : |
---|
| 124 | break; |
---|
| 125 | case Severity::Warn : |
---|
| 126 | case Severity::Error : |
---|
| 127 | { |
---|
[610354a] | 128 | char msg[2048]; // worst-case error-message buffer |
---|
[6040e67d] | 129 | va_list args; |
---|
[610354a] | 130 | va_start( args, warning ); |
---|
| 131 | vsnprintf( msg, sizeof(msg), WarningFormats[(int)warning].message, args ); // always null terminated, but may be truncated |
---|
| 132 | va_end( args ); |
---|
| 133 | |
---|
| 134 | if ( severity == Severity::Warn ) { |
---|
| 135 | cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << endl; |
---|
| 136 | } else { |
---|
| 137 | SemanticError( location, string( msg ) ); |
---|
| 138 | } |
---|
[6040e67d] | 139 | } |
---|
| 140 | break; |
---|
| 141 | case Severity::Critical : |
---|
| 142 | assertf(false, "Critical errors not implemented yet"); |
---|
| 143 | break; |
---|
| 144 | } |
---|
[a16764a6] | 145 | } |
---|
| 146 | |
---|
| 147 | //----------------------------------------------------------------------------- |
---|
| 148 | // Helpers |
---|
| 149 | namespace ErrorHelpers { |
---|
[1a69a90] | 150 | Colors colors = Colors::Auto; |
---|
| 151 | |
---|
| 152 | static inline bool with_colors() { |
---|
| 153 | return colors == Colors::Auto ? isatty( STDERR_FILENO ) : bool(colors); |
---|
| 154 | } |
---|
| 155 | |
---|
[610354a] | 156 | const string & error_str() { |
---|
| 157 | static string str = with_colors() ? "\e[31merror:\e[39m " : "error: "; |
---|
[a16764a6] | 158 | return str; |
---|
| 159 | } |
---|
| 160 | |
---|
[610354a] | 161 | const string & warning_str() { |
---|
| 162 | static string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: "; |
---|
[a16764a6] | 163 | return str; |
---|
| 164 | } |
---|
| 165 | |
---|
[610354a] | 166 | const string & bold_ttycode() { |
---|
| 167 | static string str = with_colors() ? "\e[1m" : ""; |
---|
[a16764a6] | 168 | return str; |
---|
| 169 | } |
---|
| 170 | |
---|
[610354a] | 171 | const string & reset_font_ttycode() { |
---|
| 172 | static string str = with_colors() ? "\e[0m" : ""; |
---|
[a16764a6] | 173 | return str; |
---|
| 174 | } |
---|
| 175 | |
---|
[610354a] | 176 | string make_bold( const string & str ) { |
---|
[a16764a6] | 177 | return bold_ttycode() + str + reset_font_ttycode(); |
---|
| 178 | } |
---|
| 179 | |
---|
[610354a] | 180 | ostream & operator<<(ostream & os, bold) { |
---|
[a16764a6] | 181 | os << bold_ttycode(); |
---|
| 182 | return os; |
---|
| 183 | } |
---|
| 184 | |
---|
[610354a] | 185 | ostream & operator<<(ostream & os, reset_font) { |
---|
[a16764a6] | 186 | os << reset_font_ttycode(); |
---|
| 187 | return os; |
---|
| 188 | } |
---|
[294647b] | 189 | } |
---|
| 190 | |
---|
[51587aa] | 191 | // Local Variables: // |
---|
| 192 | // tab-width: 4 // |
---|
| 193 | // mode: c++ // |
---|
| 194 | // compile-command: "make install" // |
---|
| 195 | // End: // |
---|