Changes in src/Common/SemanticError.cc [610354a:ddcedfe]
- File:
-
- 1 edited
-
src/Common/SemanticError.cc (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Common/SemanticError.cc
r610354a rddcedfe 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Dec 14 13:45:28 202313 // Update Count : 3412 // Last Modified On : Thu Jun 7 08:05:26 2018 13 // Update Count : 10 14 14 // 15 15 … … 23 23 #include <vector> 24 24 25 using namespace std;26 27 25 #include "Common/utility.h" // for to_string, CodeLocation (ptr only) 28 26 #include "SemanticError.h" … … 30 28 //----------------------------------------------------------------------------- 31 29 // Severity Handling 32 vector<Severity> & get_severities() {33 static vector<Severity> severities;30 std::vector<Severity> & get_severities() { 31 static std::vector<Severity> severities; 34 32 if(severities.empty()) { 35 33 severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS); … … 62 60 size_t idx = 0; 63 61 for ( const auto & w : WarningFormats ) { 64 if ( st rcmp( name, w.name ) == 0 ) {62 if ( std::strcmp( name, w.name ) == 0 ) { 65 63 get_severities()[idx] = s; 66 64 break; … … 72 70 //----------------------------------------------------------------------------- 73 71 // Semantic Error 74 75 72 bool SemanticErrorThrow = false; 76 73 77 SemanticErrorException::SemanticErrorException( CodeLocation location, st ring error ) {74 SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) { 78 75 append( location, error ); 79 76 } … … 83 80 } 84 81 85 void SemanticErrorException::append( CodeLocation location, const st ring & msg ) {82 void SemanticErrorException::append( CodeLocation location, const std::string & msg ) { 86 83 errors.emplace_back( location, msg ); 87 84 } … … 92 89 93 90 void SemanticErrorException::print() { 94 // usingto_string;91 using std::to_string; 95 92 96 93 errors.sort([](const error & lhs, const error & rhs) -> bool { … … 102 99 103 100 for( auto err : errors ) { 104 cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << endl; 105 } 106 } 107 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 101 std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl; 102 } 103 } 104 105 void SemanticError( CodeLocation location, std::string error ) { 115 106 SemanticErrorThrow = true; 116 throw SemanticErrorException( location, msg ); // convert msg to string 117 } 118 119 void SemanticWarning( CodeLocation location, Warning warning, ... ) { 107 throw SemanticErrorException( location, error ); 108 } 109 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 } 126 127 void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) { 120 128 Severity severity = get_severities()[(int)warning]; 121 122 switch ( severity ) { 129 switch(severity) { 123 130 case Severity::Suppress : 124 131 break; 125 132 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; 126 141 case Severity::Error : 127 142 { 128 char msg[2048]; // worst-case error-message buffer129 143 va_list args; 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 } 144 va_start(args, fmt); 145 std::string msg = fmtToString( fmt, args ); 146 va_end(args); 147 SemanticError(location, msg); 139 148 } 140 149 break; … … 154 163 } 155 164 156 const st ring & error_str() {157 static st ring str = with_colors() ? "\e[31merror:\e[39m " : "error: ";158 return str; 159 } 160 161 const st ring & warning_str() {162 static st ring str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: ";163 return str; 164 } 165 166 const st ring & bold_ttycode() {167 static st ring str = with_colors() ? "\e[1m" : "";168 return str; 169 } 170 171 const st ring & reset_font_ttycode() {172 static st ring str = with_colors() ? "\e[0m" : "";173 return str; 174 } 175 176 st ring make_bold( conststring & str ) {165 const std::string & error_str() { 166 static std::string str = with_colors() ? "\e[31merror:\e[39m " : "error: "; 167 return str; 168 } 169 170 const std::string & warning_str() { 171 static std::string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: "; 172 return str; 173 } 174 175 const std::string & bold_ttycode() { 176 static std::string str = with_colors() ? "\e[1m" : ""; 177 return str; 178 } 179 180 const std::string & reset_font_ttycode() { 181 static std::string str = with_colors() ? "\e[0m" : ""; 182 return str; 183 } 184 185 std::string make_bold( const std::string & str ) { 177 186 return bold_ttycode() + str + reset_font_ttycode(); 178 187 } 179 188 180 ostream & operator<<(ostream & os, bold) {189 std::ostream & operator<<(std::ostream & os, bold) { 181 190 os << bold_ttycode(); 182 191 return os; 183 192 } 184 193 185 ostream & operator<<(ostream & os, reset_font) {194 std::ostream & operator<<(std::ostream & os, reset_font) { 186 195 os << reset_font_ttycode(); 187 196 return os;
Note:
See TracChangeset
for help on using the changeset viewer.