Changeset 5546eee4 for src/Common/SemanticError.cc
- Timestamp:
- Dec 16, 2023, 1:01:44 AM (22 months ago)
- Branches:
- master
- Children:
- b7898ac
- Parents:
- 0fa0201d (diff), 69ab896 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/SemanticError.cc
r0fa0201d r5546eee4 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Jun 7 08:05:26 201813 // Update Count : 1012 // 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; … … 70 72 //----------------------------------------------------------------------------- 71 73 // Semantic Error 74 72 75 bool SemanticErrorThrow = false; 73 76 74 SemanticErrorException::SemanticErrorException( CodeLocation location, st d::string error ) {77 SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) { 75 78 append( location, error ); 76 79 } … … 80 83 } 81 84 82 void SemanticErrorException::append( CodeLocation location, const st d::string & msg ) {85 void SemanticErrorException::append( CodeLocation location, const string & msg ) { 83 86 errors.emplace_back( location, msg ); 84 87 } … … 89 92 90 93 void SemanticErrorException::print() { 91 using std::to_string;94 // using to_string; 92 95 93 96 errors.sort([](const error & lhs, const error & rhs) -> bool { … … 99 102 100 103 for( auto err : errors ) { 101 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; 102 105 } 103 106 } 104 107 105 void SemanticError( CodeLocation location, std::string error ) { 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 106 115 SemanticErrorThrow = true; 107 throw SemanticErrorException( location, error );116 throw SemanticErrorException( location, msg ); // convert msg to string 108 117 } 109 118 110 namespace { 111 // convert format string and arguments into a single string 112 std::string fmtToString(const char * fmt, va_list ap) { 113 int size = 128; 114 while ( true ) { 115 char buf[size]; 116 va_list args; 117 va_copy( args, ap ); 118 int n = vsnprintf(&buf[0], size, fmt, args); 119 va_end( args ); 120 if ( n < size && n >= 0 ) return buf; 121 size *= 2; 122 } 123 assert( false ); 124 } 125 } 119 void SemanticWarning( CodeLocation location, Warning warning, ... ) { 120 Severity severity = get_severities()[(int)warning]; 126 121 127 void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) { 128 Severity severity = get_severities()[(int)warning]; 129 switch(severity) { 122 switch ( severity ) { 130 123 case Severity::Suppress : 131 124 break; 132 125 case Severity::Warn : 133 {134 va_list args;135 va_start(args, fmt);136 std::string msg = fmtToString( fmt, args );137 va_end(args);138 std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl;139 }140 break;141 126 case Severity::Error : 142 127 { 128 char msg[2048]; // worst-case error-message buffer 143 129 va_list args; 144 va_start(args, fmt); 145 std::string msg = fmtToString( fmt, args ); 146 va_end(args); 147 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 } 148 139 } 149 140 break; … … 163 154 } 164 155 165 const st d::string & error_str() {166 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: "; 167 158 return str; 168 159 } 169 160 170 const st d::string & warning_str() {171 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: "; 172 163 return str; 173 164 } 174 165 175 const st d::string & bold_ttycode() {176 static st d::string str = with_colors() ? "\e[1m" : "";166 const string & bold_ttycode() { 167 static string str = with_colors() ? "\e[1m" : ""; 177 168 return str; 178 169 } 179 170 180 const st d::string & reset_font_ttycode() {181 static st d::string str = with_colors() ? "\e[0m" : "";171 const string & reset_font_ttycode() { 172 static string str = with_colors() ? "\e[0m" : ""; 182 173 return str; 183 174 } 184 175 185 st d::string make_bold( const std::string & str ) {176 string make_bold( const string & str ) { 186 177 return bold_ttycode() + str + reset_font_ttycode(); 187 178 } 188 179 189 std::ostream & operator<<(std::ostream & os, bold) {180 ostream & operator<<(ostream & os, bold) { 190 181 os << bold_ttycode(); 191 182 return os; 192 183 } 193 184 194 std::ostream & operator<<(std::ostream & os, reset_font) {185 ostream & operator<<(ostream & os, reset_font) { 195 186 os << reset_font_ttycode(); 196 187 return os;
Note:
See TracChangeset
for help on using the changeset viewer.