Changeset 610354a
- Timestamp:
- Dec 14, 2023, 1:55:18 PM (12 months ago)
- Branches:
- master
- Children:
- 21ad568
- Parents:
- 3e49c477
- Location:
- src/Common
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/ErrorObjects.h
r3e49c477 r610354a 46 46 std::list< error > errors; 47 47 }; 48 49 void SemanticWarningImpl( CodeLocation location, std::string error );50 51 template< typename T >52 static inline void SemanticWarningImpl( const T * obj, const std::string & error ) {53 SemanticWarning( obj->location, toString( error, obj ) );54 }55 56 template< typename T >57 static inline void SemanticWarningImpl( CodeLocation location, const T * obj, const std::string & error ) {58 SemanticWarningImpl( location, toString( error, obj ) );59 } -
src/Common/SemanticError.cc
r3e49c477 r610354a 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Dec 11 15:59:09202313 // Update Count : 1412 // Last Modified On : Thu Dec 14 13:45:28 2023 13 // Update Count : 34 14 14 // 15 15 … … 23 23 #include <vector> 24 24 25 using namespace std; 26 25 27 #include "Common/utility.h" // for to_string, CodeLocation (ptr only) 26 28 #include "SemanticError.h" … … 28 30 //----------------------------------------------------------------------------- 29 31 // Severity Handling 30 std::vector<Severity> & get_severities() {31 static std::vector<Severity> severities;32 vector<Severity> & get_severities() { 33 static vector<Severity> severities; 32 34 if(severities.empty()) { 33 35 severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS); … … 60 62 size_t idx = 0; 61 63 for ( const auto & w : WarningFormats ) { 62 if ( st d::strcmp( name, w.name ) == 0 ) {64 if ( strcmp( name, w.name ) == 0 ) { 63 65 get_severities()[idx] = s; 64 66 break; … … 73 75 bool SemanticErrorThrow = false; 74 76 75 SemanticErrorException::SemanticErrorException( CodeLocation location, st d::string error ) {77 SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) { 76 78 append( location, error ); 77 79 } … … 81 83 } 82 84 83 void SemanticErrorException::append( CodeLocation location, const st d::string & msg ) {85 void SemanticErrorException::append( CodeLocation location, const string & msg ) { 84 86 errors.emplace_back( location, msg ); 85 87 } … … 90 92 91 93 void SemanticErrorException::print() { 92 using std::to_string;94 // using to_string; 93 95 94 96 errors.sort([](const error & lhs, const error & rhs) -> bool { … … 100 102 101 103 for( auto err : errors ) { 102 std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl;104 cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << endl; 103 105 } 104 106 } … … 115 117 } 116 118 117 void SemanticError( CodeLocation location, std::string error ) { 118 SemanticErrorThrow = true; 119 throw SemanticErrorException( location, error ); 120 } 119 void SemanticWarning( CodeLocation location, Warning warning, ... ) { 120 Severity severity = get_severities()[(int)warning]; 121 121 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 139 void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) { 140 Severity severity = get_severities()[(int)warning]; 141 switch(severity) { 122 switch ( severity ) { 142 123 case Severity::Suppress : 143 124 break; 144 125 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 126 case Severity::Error : 154 127 { 128 char msg[2048]; // worst-case error-message buffer 155 129 va_list args; 156 va_start(args, fmt); 157 std::string msg = fmtToString( fmt, args ); 158 va_end(args); 159 SemanticError(location, msg); 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 } 160 139 } 161 140 break; … … 175 154 } 176 155 177 const st d::string & error_str() {178 static st d::string str = with_colors() ? "\e[31merror:\e[39m " : "error: ";156 const string & error_str() { 157 static string str = with_colors() ? "\e[31merror:\e[39m " : "error: "; 179 158 return str; 180 159 } 181 160 182 const st d::string & warning_str() {183 static st d::string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: ";161 const string & warning_str() { 162 static string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: "; 184 163 return str; 185 164 } 186 165 187 const st d::string & bold_ttycode() {188 static st d::string str = with_colors() ? "\e[1m" : "";166 const string & bold_ttycode() { 167 static string str = with_colors() ? "\e[1m" : ""; 189 168 return str; 190 169 } 191 170 192 const st d::string & reset_font_ttycode() {193 static st d::string str = with_colors() ? "\e[0m" : "";171 const string & reset_font_ttycode() { 172 static string str = with_colors() ? "\e[0m" : ""; 194 173 return str; 195 174 } 196 175 197 st d::string make_bold( const std::string & str ) {176 string make_bold( const string & str ) { 198 177 return bold_ttycode() + str + reset_font_ttycode(); 199 178 } 200 179 201 std::ostream & operator<<(std::ostream & os, bold) {180 ostream & operator<<(ostream & os, bold) { 202 181 os << bold_ttycode(); 203 182 return os; 204 183 } 205 184 206 std::ostream & operator<<(std::ostream & os, reset_font) {185 ostream & operator<<(ostream & os, reset_font) { 207 186 os << reset_font_ttycode(); 208 187 return os; -
src/Common/SemanticError.h
r3e49c477 r610354a 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Dec 11 21:54:22202313 // Update Count : 5412 // Last Modified On : Thu Dec 14 13:48:07 2023 13 // Update Count : 72 14 14 // 15 15 … … 28 28 __attribute__((noreturn, format(printf, 2, 3))) void SemanticError( CodeLocation location, const char fmt[], ... ); 29 29 30 __attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error ); 30 __attribute__((noreturn)) static inline void SemanticError( CodeLocation location, std::string error ) { 31 SemanticErrorThrow = true; 32 throw SemanticErrorException( location, error ); 33 } 31 34 32 35 __attribute__((noreturn)) static inline void SemanticError( const ast::ParseNode * obj, const std::string & error ) { … … 76 79 CppCopy, 77 80 DeprecTraitSyntax, 78 NUMBER_OF_WARNINGS, // MUST be thelast warning81 NUMBER_OF_WARNINGS, // MUST be last warning 79 82 }; 80 83 … … 84 87 ); 85 88 86 #define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[(int)id].message, ##__VA_ARGS__) 89 void SemanticWarning( CodeLocation loc, Warning warn, ... ); 87 90 88 void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4))); 89 90 void SemanticWarning_SuppressAll (); 91 void SemanticWarning_EnableAll (); 91 void SemanticWarning_SuppressAll(); 92 void SemanticWarning_EnableAll(); 92 93 void SemanticWarning_WarningAsError(); 93 void SemanticWarning_Set 94 void SemanticWarning_Set(const char * const name, Severity s); 94 95 95 96 // SKULLDUGGERY: cfa.cc is built before SemanticError.cc but needs this routine.
Note: See TracChangeset
for help on using the changeset viewer.