Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/SemanticError.cc

    rb1f2007d r610354a  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Dec 11 15:59:09 2023
    13 // Update Count     : 14
     12// Last Modified On : Thu Dec 14 13:45:28 2023
     13// Update Count     : 34
    1414//
    1515
     
    2323#include <vector>
    2424
     25using namespace std;
     26
    2527#include "Common/utility.h"                                                             // for to_string, CodeLocation (ptr only)
    2628#include "SemanticError.h"
     
    2830//-----------------------------------------------------------------------------
    2931// Severity Handling
    30 std::vector<Severity> & get_severities() {
    31         static std::vector<Severity> severities;
     32vector<Severity> & get_severities() {
     33        static vector<Severity> severities;
    3234        if(severities.empty()) {
    3335                severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS);
     
    6062        size_t idx = 0;
    6163        for ( const auto & w : WarningFormats ) {
    62                 if ( std::strcmp( name, w.name ) == 0 ) {
     64                if ( strcmp( name, w.name ) == 0 ) {
    6365                        get_severities()[idx] = s;
    6466                        break;
     
    7375bool SemanticErrorThrow = false;
    7476
    75 SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
     77SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) {
    7678        append( location, error );
    7779}
     
    8183}
    8284
    83 void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
     85void SemanticErrorException::append( CodeLocation location, const string & msg ) {
    8486        errors.emplace_back( location, msg );
    8587}
     
    9092
    9193void SemanticErrorException::print() {
    92         using std::to_string;
     94//      using to_string;
    9395
    9496        errors.sort([](const error & lhs, const error & rhs) -> bool {
     
    100102
    101103        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;
    103105        }
    104106}
     
    115117}
    116118
    117 void SemanticError( CodeLocation location, std::string error ) {
    118         SemanticErrorThrow = true;
    119         throw SemanticErrorException( location, error );
    120 }
     119void SemanticWarning( CodeLocation location, Warning warning, ... ) {
     120        Severity severity = get_severities()[(int)warning];
    121121
    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 ) {
    142123        case Severity::Suppress :
    143124                break;
    144125        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;
    153126        case Severity::Error :
    154127                {
     128                        char msg[2048];                                                         // worst-case error-message buffer
    155129                        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                        }
    160139                }
    161140                break;
     
    175154        }
    176155
    177         const std::string & error_str() {
    178                 static std::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: ";
    179158                return str;
    180159        }
    181160
    182         const std::string & warning_str() {
    183                 static std::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: ";
    184163                return str;
    185164        }
    186165
    187         const std::string & bold_ttycode() {
    188                 static std::string str = with_colors() ? "\e[1m" : "";
     166        const string & bold_ttycode() {
     167                static string str = with_colors() ? "\e[1m" : "";
    189168                return str;
    190169        }
    191170
    192         const std::string & reset_font_ttycode() {
    193                 static std::string str = with_colors() ? "\e[0m" : "";
     171        const string & reset_font_ttycode() {
     172                static string str = with_colors() ? "\e[0m" : "";
    194173                return str;
    195174        }
    196175
    197         std::string make_bold( const std::string & str ) {
     176        string make_bold( const string & str ) {
    198177                return bold_ttycode() + str + reset_font_ttycode();
    199178        }
    200179
    201         std::ostream & operator<<(std::ostream & os, bold) {
     180        ostream & operator<<(ostream & os, bold) {
    202181                os << bold_ttycode();
    203182                return os;
    204183        }
    205184
    206         std::ostream & operator<<(std::ostream & os, reset_font) {
     185        ostream & operator<<(ostream & os, reset_font) {
    207186                os << reset_font_ttycode();
    208187                return os;
Note: See TracChangeset for help on using the changeset viewer.