Changeset 5546eee4 for src/Common


Ignore:
Timestamp:
Dec 16, 2023, 1:01:44 AM (2 years ago)
Author:
JiadaL <j82liang@…>
Branches:
master, stuck-waitfor-destruct
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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/Common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Common/ErrorObjects.h

    r0fa0201d r5546eee4  
    4646        std::list< error > errors;
    4747};
    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

    r0fa0201d r5546eee4  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jun  7 08:05:26 2018
    13 // Update Count     : 10
     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;
     
    7072//-----------------------------------------------------------------------------
    7173// Semantic Error
     74
    7275bool SemanticErrorThrow = false;
    7376
    74 SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
     77SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) {
    7578        append( location, error );
    7679}
     
    8083}
    8184
    82 void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
     85void SemanticErrorException::append( CodeLocation location, const string & msg ) {
    8386        errors.emplace_back( location, msg );
    8487}
     
    8992
    9093void SemanticErrorException::print() {
    91         using std::to_string;
     94//      using to_string;
    9295
    9396        errors.sort([](const error & lhs, const error & rhs) -> bool {
     
    99102
    100103        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;
    102105        }
    103106}
    104107
    105 void SemanticError( CodeLocation location, std::string error ) {
     108void 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
    106115        SemanticErrorThrow = true;
    107         throw SemanticErrorException( location, error );
     116        throw SemanticErrorException( location, msg );          // convert msg to string
    108117}
    109118
    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 }
     119void SemanticWarning( CodeLocation location, Warning warning, ... ) {
     120        Severity severity = get_severities()[(int)warning];
    126121
    127 void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) {
    128         Severity severity = get_severities()[(int)warning];
    129         switch(severity) {
     122        switch ( severity ) {
    130123        case Severity::Suppress :
    131124                break;
    132125        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;
    141126        case Severity::Error :
    142127                {
     128                        char msg[2048];                                                         // worst-case error-message buffer
    143129                        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                        }
    148139                }
    149140                break;
     
    163154        }
    164155
    165         const std::string & error_str() {
    166                 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: ";
    167158                return str;
    168159        }
    169160
    170         const std::string & warning_str() {
    171                 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: ";
    172163                return str;
    173164        }
    174165
    175         const std::string & bold_ttycode() {
    176                 static std::string str = with_colors() ? "\e[1m" : "";
     166        const string & bold_ttycode() {
     167                static string str = with_colors() ? "\e[1m" : "";
    177168                return str;
    178169        }
    179170
    180         const std::string & reset_font_ttycode() {
    181                 static std::string str = with_colors() ? "\e[0m" : "";
     171        const string & reset_font_ttycode() {
     172                static string str = with_colors() ? "\e[0m" : "";
    182173                return str;
    183174        }
    184175
    185         std::string make_bold( const std::string & str ) {
     176        string make_bold( const string & str ) {
    186177                return bold_ttycode() + str + reset_font_ttycode();
    187178        }
    188179
    189         std::ostream & operator<<(std::ostream & os, bold) {
     180        ostream & operator<<(ostream & os, bold) {
    190181                os << bold_ttycode();
    191182                return os;
    192183        }
    193184
    194         std::ostream & operator<<(std::ostream & os, reset_font) {
     185        ostream & operator<<(ostream & os, reset_font) {
    195186                os << reset_font_ttycode();
    196187                return os;
  • src/Common/SemanticError.h

    r0fa0201d r5546eee4  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Feb 25 12:01:31 2023
    13 // Update Count     : 37
     12// Last Modified On : Thu Dec 14 13:48:07 2023
     13// Update Count     : 72
    1414//
    1515
     
    1818#include "ErrorObjects.h"
    1919#include "AST/Node.hpp"
     20#include "AST/ParseNode.hpp"
    2021#include <cstring>
    2122
     
    2526extern bool SemanticErrorThrow;
    2627
    27 __attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error );
     28__attribute__((noreturn, format(printf, 2, 3))) void SemanticError( CodeLocation location, const char fmt[], ... );
    2829
    29 template< typename T >
    30 __attribute__((noreturn)) static inline void SemanticError( const T * obj, const std::string & error ) {
     30__attribute__((noreturn)) static inline void SemanticError( CodeLocation location, std::string error ) {
     31        SemanticErrorThrow = true;
     32        throw SemanticErrorException( location, error );
     33}
     34
     35__attribute__((noreturn)) static inline void SemanticError( const ast::ParseNode * obj, const std::string & error ) {
    3136        SemanticError( obj->location, toString( error, obj ) );
    3237}
    3338
    34 template< typename T >
    35 __attribute__((noreturn)) static inline void SemanticError( CodeLocation location, const T * obj, const std::string & error ) {
     39__attribute__((noreturn)) static inline void SemanticError( CodeLocation location, const ast::Node * obj, const std::string & error ) {
    3640        SemanticError( location, toString( error, obj ) );
    3741}
     
    5458
    5559constexpr WarningData WarningFormats[] = {
    56         {"self-assign"              , Severity::Warn    , "self assignment of expression: %s"                          },
    57         {"reference-conversion"     , Severity::Warn    , "rvalue to reference conversion of rvalue: %s"               },
    58         {"qualifiers-zero_t-one_t"  , Severity::Warn    , "questionable use of type qualifier(s) with %s"              },
    59         {"aggregate-forward-decl"   , Severity::Warn    , "forward declaration of nested aggregate: %s"                },
    60         {"superfluous-decl"         , Severity::Warn    , "declaration does not allocate storage: %s"                  },
    61         {"superfluous-else"         , Severity::Warn    , "else clause never executed for empty loop conditional"      },
    62         {"gcc-attributes"           , Severity::Warn    , "invalid attribute: %s"                                      },
    63         {"c++-like-copy"            , Severity::Warn    , "Constructor from reference is not a valid copy constructor" },
    64         {"depreciated-trait-syntax" , Severity::Warn    , "trait type-parameters are now specified using the forall clause" },
     60        {"self-assign"              , Severity::Warn, "self assignment of expression: %s"                          },
     61        {"reference-conversion"     , Severity::Warn, "rvalue to reference conversion of rvalue: %s"               },
     62        {"qualifiers-zero_t-one_t"  , Severity::Warn, "questionable use of type qualifier(s) with %s"              },
     63        {"aggregate-forward-decl"   , Severity::Warn, "forward declaration of nested aggregate: %s"                },
     64        {"superfluous-decl"         , Severity::Warn, "declaration does not allocate storage: %s"                  },
     65        {"superfluous-else"         , Severity::Warn, "else clause never executed for empty loop conditional"      },
     66        {"gcc-attributes"           , Severity::Warn, "invalid attribute: %s"                                      },
     67        {"c++-like-copy"            , Severity::Warn, "Constructor from reference is not a valid copy constructor" },
     68        {"depreciated-trait-syntax" , Severity::Warn, "trait type-parameters are now specified using the forall clause" },
    6569};
    6670
     
    7579        CppCopy,
    7680        DeprecTraitSyntax,
    77         NUMBER_OF_WARNINGS, // This MUST be the last warning
     81        NUMBER_OF_WARNINGS, // MUST be last warning
    7882};
    7983
     
    8387);
    8488
    85 #define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[(int)id].message, ##__VA_ARGS__)
     89void SemanticWarning( CodeLocation loc, Warning warn, ... );
    8690
    87 void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4)));
    88 
    89 void SemanticWarning_SuppressAll   ();
    90 void SemanticWarning_EnableAll     ();
     91void SemanticWarning_SuppressAll();
     92void SemanticWarning_EnableAll();
    9193void SemanticWarning_WarningAsError();
    92 void SemanticWarning_Set           (const char * const name, Severity s);
     94void SemanticWarning_Set(const char * const name, Severity s);
    9395
    9496// SKULLDUGGERY: cfa.cc is built before SemanticError.cc but needs this routine.
Note: See TracChangeset for help on using the changeset viewer.