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