Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/SemanticError.h

    rfd2debf rd48e529  
    77// SemanticError.h --
    88//
    9 // Author           : Thierry Delisle
     9// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jul 19 10:09:17 2018
    13 // Update Count     : 31
     12// Last Modified On : Tue Aug 29 22:03:36 2017
     13// Update Count     : 17
    1414//
    1515
    1616#pragma once
    1717
    18 #include "ErrorObjects.h"
    19 #include <cstring>
     18#include <exception>                                                                    // for exception
     19#include <iostream>                                                                             // for ostream
     20#include <list>                                                                                 // for list
     21#include <string>                                                                               // for string
     22#include <unistd.h>                                                                             // for isatty
    2023
    21 //-----------------------------------------------------------------------------
    22 // Errors
     24#include "CodeLocation.h"                                                               // for CodeLocation, toString
    2325
    24 extern bool SemanticErrorThrow;
     26struct error {
     27        std::string description;
     28        CodeLocation location;
    2529
    26 __attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error );
     30        error() = default;
     31        error( const std::string & str ) : description( str ) {}
     32
     33        void maybeSet( const CodeLocation & location ) {
     34                if( this->location.isUnset() ) {
     35                        this->location = location;
     36                }
     37        }
     38};
     39
     40class SemanticError : public std::exception {
     41  public:
     42        SemanticError();
     43        SemanticError( std::string error );
     44        template< typename T > SemanticError( const std::string & error, const T * obj );
     45        ~SemanticError() throw() {}
     46
     47        static inline const std::string & error_str() {
     48                static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
     49                return str;
     50        }
     51
     52        void append( SemanticError & other );
     53        void append( const std::string & );
     54        bool isEmpty() const;
     55        void print( std::ostream & os );
     56
     57        void set_location( const CodeLocation & location );
     58        // constructs an exception using the given message and the printed representation of the obj (T must have a print
     59        // method)
     60  private:
     61        std::list< error > errors;
     62};
    2763
    2864template< typename T >
    29 __attribute__((noreturn)) static inline void SemanticError( const T * obj, const std::string & error ) {
    30         SemanticError( obj->location, toString( error, obj ) );
    31 }
    32 
    33 template< typename T >
    34 __attribute__((noreturn)) static inline void SemanticError( CodeLocation location, const T * obj, const std::string & error ) {
    35         SemanticError( location, toString( error, obj ) );
    36 }
    37 
    38 //-----------------------------------------------------------------------------
    39 // Warnings
    40 
    41 enum class Severity {
    42         Suppress,
    43         Warn,
    44         Error,
    45         Critical
    46 };
    47 
    48 struct WarningData {
    49         const char * const name;
    50         const char * const message;
    51         const Severity default_severity;
    52 };
    53 
    54 constexpr WarningData WarningFormats[] = {
    55         {"self-assign"            , "self assignment of expression: %s"            , Severity::Warn},
    56         {"reference-conversion"   , "rvalue to reference conversion of rvalue: %s" , Severity::Warn},
    57         {"qualifiers-zero_t-one_t", "questionable use of type qualifier %s with %s", Severity::Warn},
    58         {"aggregate-forward-decl" , "forward declaration of nested aggregate: %s"  , Severity::Warn},
    59         {"superfluous-decl"       , "declaration does not allocate storage: %s"    , Severity::Warn},
    60         {"gcc-attributes"         , "invalid attribute: %s"                        , Severity::Warn},
    61 };
    62 
    63 enum class Warning {
    64         SelfAssignment,
    65         RvalueToReferenceConversion,
    66         BadQualifiersZeroOne,
    67         AggrForwardDecl,
    68         SuperfluousDecl,
    69         GccAttributes,
    70         NUMBER_OF_WARNINGS, // This MUST be the last warning
    71 };
    72 
    73 static_assert(
    74         (sizeof(WarningFormats) / sizeof(WarningFormats[0])) == ((unsigned long)Warning::NUMBER_OF_WARNINGS),
    75         "Each warning format should have a corresponding warning enum value"
    76 );
    77 
    78 #define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[(int)id].message, __VA_ARGS__)
    79 
    80 void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4)));
    81 
    82 void SemanticWarning_SuppressAll   ();
    83 void SemanticWarning_EnableAll     ();
    84 void SemanticWarning_WarningAsError();
    85 void SemanticWarning_Set           (const char * const name, Severity s);
    86 
    87 // SKULLDUGGERY: cfa.cc is built before SemanticError.cc but needs this routine.
    88 static inline bool SemanticWarning_Exist(const char * const name) {
    89         for ( const auto & w : WarningFormats ) {
    90                 if ( std::strcmp( name, w.name ) == 0 ) return true;
    91         }
    92         return false;
    93 }
    94 
    95 //-----------------------------------------------------------------------------
    96 // Helpers
    97 namespace ErrorHelpers {
    98         const std::string & error_str();
    99         const std::string & warning_str();
    100         const std::string & bold_ttycode();
    101         const std::string & reset_font_ttycode();
    102 
    103         std::string make_bold( const std::string & str );
    104 
    105         struct bold {};
    106         std::ostream & operator<<(std::ostream & os, bold);
    107 
    108         struct reset_font {};
    109         std::ostream & operator<<(std::ostream & os, reset_font);
     65SemanticError::SemanticError( const std::string & error, const T * obj ) {
     66        append( toString( error, obj ) );
    11067}
    11168
Note: See TracChangeset for help on using the changeset viewer.