Changeset d55d7a6 for src/Common


Ignore:
Timestamp:
Feb 15, 2018, 3:58:56 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
75e3cb2
Parents:
d27e340
Message:

Massive change to errors to enable warnings

Location:
src/Common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.impl.h

    rd27e340 rd55d7a6  
    7777                        maybeAccept_impl( *i, visitor );
    7878                } catch( SemanticError &e ) {
    79                         e.set_location( (*i)->location );
    8079                        errors.append( e );
    8180                }
     
    104103                        maybeMutate_impl( *i, mutator );
    105104                } catch( SemanticError &e ) {
    106                         e.set_location( (*i)->location );
    107105                        errors.append( e );
    108106                }
     
    134132                        }
    135133                } catch( SemanticError &e ) {
    136                         e.set_location( (*i)->location );
    137134                        errors.append( e );
    138135                }
     
    163160                        } // if
    164161                } catch( SemanticError &e ) {
    165                         e.set_location( (*i)->location );
    166162                        errors.append( e );
    167163                } // try
     
    200196
    201197                } catch ( SemanticError &e ) {
    202                         e.set_location( (*i)->location );
    203198                        errors.append( e );
    204199                }
  • src/Common/SemanticError.cc

    rd27e340 rd55d7a6  
    2323#include "SemanticError.h"
    2424
    25 SemanticError::SemanticError() {
    26 }
    27 
    28 SemanticError::SemanticError( std::string error ) {
    29         append( error );
     25SemanticError::SemanticError( CodeLocation location, std::string error ) {
     26        append( location, error );
    3027}
    3128
     
    3431}
    3532
    36 void SemanticError::append( const std::string & msg ) {
    37         errors.emplace_back( error_str() + msg );
     33void SemanticError::append( CodeLocation location, const std::string & msg ) {
     34        errors.emplace_back( location, msg );
    3835}
    3936
     
    4239}
    4340
    44 void SemanticError::print( std::ostream &os ) {
     41void SemanticError::print() {
    4542        using std::to_string;
    4643        for( auto err : errors ) {
    47                 os << err.location << err.description << std::endl;
     44                std::cerr << bold() << err.location << error_str() << reset_font() << err.description << std::endl;
    4845        }
    4946}
    5047
    51 void SemanticError::set_location( const CodeLocation& location ) {
    52         errors.begin()->maybeSet( location );
     48SemanticWarning::SemanticWarning( CodeLocation location, std::string msg ) {
     49        std::cerr << bold() << location << warning_str() << reset_font() << msg << std::endl;
    5350}
    5451
  • src/Common/SemanticError.h

    rd27e340 rd55d7a6  
    2424#include "CodeLocation.h"                                                               // for CodeLocation, toString
    2525
     26//-----------------------------------------------------------------------------
     27// Errors
    2628struct error {
     29        CodeLocation location;
    2730        std::string description;
    28         CodeLocation location;
    2931
    3032        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         }
     33        error( CodeLocation loc, const std::string & str ) : location( loc ), description( str ) {}
    3834};
    3935
    4036class SemanticError : public std::exception {
    4137  public:
    42         SemanticError();
    43         SemanticError( std::string error );
    44         template< typename T > SemanticError( const std::string & error, const T * obj );
     38        SemanticError() = default;
     39        SemanticError( CodeLocation location, std::string error );
    4540        ~SemanticError() throw() {}
     41
     42        // constructs an exception using the given message and the printed representation of the obj (T must have a print method)
     43        template< typename T > SemanticError(const T * obj, const std::string & error);
     44        template< typename T > SemanticError( CodeLocation location, const T * obj, const std::string & error);
    4645
    4746        static inline const std::string & error_str() {
     
    5150
    5251        void append( SemanticError & other );
    53         void append( const std::string & );
     52        void append( CodeLocation location, const std::string & );
    5453        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)
     54        void print();
    6055  private:
    6156        std::list< error > errors;
     
    6358
    6459template< typename T >
    65 SemanticError::SemanticError( const std::string & error, const T * obj ) {
    66         append( toString( error, obj ) );
     60SemanticError::SemanticError( const T * obj, const std::string & error )
     61        : SemanticError( obj->location, toString( error, obj ) )
     62{}
     63
     64template< typename T >
     65SemanticError::SemanticError( CodeLocation location, const T * obj, const std::string & error )
     66        : SemanticError( location, toString( error, obj ) )
     67{}
     68
     69//-----------------------------------------------------------------------------
     70// Warnings
     71class SemanticWarning {
     72  public:
     73        SemanticWarning( CodeLocation location, std::string error );
     74        ~SemanticWarning() throw() {}
     75
     76        // constructs an exception using the given message and the printed representation of the obj (T must have a print method)
     77        template< typename T > SemanticWarning(const T * obj, const std::string & error);
     78        template< typename T > SemanticWarning( CodeLocation location, const T * obj, const std::string & error);
     79
     80        static inline const std::string & warning_str() {
     81                static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
     82                return str;
     83        }
     84
     85  private:
     86};
     87
     88template< typename T >
     89SemanticWarning::SemanticWarning( const T * obj, const std::string & error )
     90        : SemanticWarning( obj->location, toString( error, obj ) )
     91{}
     92
     93template< typename T >
     94SemanticWarning::SemanticWarning( CodeLocation location, const T * obj, const std::string & error )
     95        : SemanticWarning( location, toString( error, obj ) )
     96{}
     97
     98//-----------------------------------------------------------------------------
     99// Helpers
     100static inline const std::string & bold_ttycode() {
     101        static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
     102        return str;
     103}
     104
     105static inline const std::string & reset_font_ttycode() {
     106        static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
     107        return str;
     108}
     109
     110static inline std::string make_bold( const std::string & str ) {
     111        return bold_ttycode() + str + reset_font_ttycode();
     112}
     113
     114struct bold {};
     115static inline std::ostream & operator<<(std::ostream & os, bold) {
     116        os << bold_ttycode();
     117        return os;
     118}
     119
     120struct reset_font {};
     121static inline std::ostream & operator<<(std::ostream & os, reset_font) {
     122        os << reset_font_ttycode();
     123        return os;
    67124}
    68125
Note: See TracChangeset for help on using the changeset viewer.