Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/SemanticError.cc

    r610354a rb1f2007d  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Dec 14 13:45:28 2023
    13 // Update Count     : 34
     12// Last Modified On : Mon Dec 11 15:59:09 2023
     13// Update Count     : 14
    1414//
    1515
     
    2323#include <vector>
    2424
    25 using namespace std;
    26 
    2725#include "Common/utility.h"                                                             // for to_string, CodeLocation (ptr only)
    2826#include "SemanticError.h"
     
    3028//-----------------------------------------------------------------------------
    3129// Severity Handling
    32 vector<Severity> & get_severities() {
    33         static vector<Severity> severities;
     30std::vector<Severity> & get_severities() {
     31        static std::vector<Severity> severities;
    3432        if(severities.empty()) {
    3533                severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS);
     
    6260        size_t idx = 0;
    6361        for ( const auto & w : WarningFormats ) {
    64                 if ( strcmp( name, w.name ) == 0 ) {
     62                if ( std::strcmp( name, w.name ) == 0 ) {
    6563                        get_severities()[idx] = s;
    6664                        break;
     
    7573bool SemanticErrorThrow = false;
    7674
    77 SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) {
     75SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
    7876        append( location, error );
    7977}
     
    8381}
    8482
    85 void SemanticErrorException::append( CodeLocation location, const string & msg ) {
     83void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
    8684        errors.emplace_back( location, msg );
    8785}
     
    9290
    9391void SemanticErrorException::print() {
    94 //      using to_string;
     92        using std::to_string;
    9593
    9694        errors.sort([](const error & lhs, const error & rhs) -> bool {
     
    102100
    103101        for( auto err : errors ) {
    104                 cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << endl;
     102                std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl;
    105103        }
    106104}
     
    117115}
    118116
    119 void SemanticWarning( CodeLocation location, Warning warning, ... ) {
     117void SemanticError( CodeLocation location, std::string error ) {
     118        SemanticErrorThrow = true;
     119        throw SemanticErrorException( location, error );
     120}
     121
     122namespace {
     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
     139void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) {
    120140        Severity severity = get_severities()[(int)warning];
    121 
    122         switch ( severity ) {
     141        switch(severity) {
    123142        case Severity::Suppress :
    124143                break;
    125144        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;
    126153        case Severity::Error :
    127154                {
    128                         char msg[2048];                                                         // worst-case error-message buffer
    129155                        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                         }
     156                        va_start(args, fmt);
     157                        std::string msg = fmtToString( fmt, args );
     158                        va_end(args);
     159                        SemanticError(location, msg);
    139160                }
    140161                break;
     
    154175        }
    155176
    156         const string & error_str() {
    157                 static string str = with_colors() ? "\e[31merror:\e[39m " : "error: ";
    158                 return str;
    159         }
    160 
    161         const string & warning_str() {
    162                 static string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: ";
    163                 return str;
    164         }
    165 
    166         const string & bold_ttycode() {
    167                 static string str = with_colors() ? "\e[1m" : "";
    168                 return str;
    169         }
    170 
    171         const string & reset_font_ttycode() {
    172                 static string str = with_colors() ? "\e[0m" : "";
    173                 return str;
    174         }
    175 
    176         string make_bold( const string & str ) {
     177        const std::string & error_str() {
     178                static std::string str = with_colors() ? "\e[31merror:\e[39m " : "error: ";
     179                return str;
     180        }
     181
     182        const std::string & warning_str() {
     183                static std::string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: ";
     184                return str;
     185        }
     186
     187        const std::string & bold_ttycode() {
     188                static std::string str = with_colors() ? "\e[1m" : "";
     189                return str;
     190        }
     191
     192        const std::string & reset_font_ttycode() {
     193                static std::string str = with_colors() ? "\e[0m" : "";
     194                return str;
     195        }
     196
     197        std::string make_bold( const std::string & str ) {
    177198                return bold_ttycode() + str + reset_font_ttycode();
    178199        }
    179200
    180         ostream & operator<<(ostream & os, bold) {
     201        std::ostream & operator<<(std::ostream & os, bold) {
    181202                os << bold_ttycode();
    182203                return os;
    183204        }
    184205
    185         ostream & operator<<(ostream & os, reset_font) {
     206        std::ostream & operator<<(std::ostream & os, reset_font) {
    186207                os << reset_font_ttycode();
    187208                return os;
Note: See TracChangeset for help on using the changeset viewer.