Ignore:
Timestamp:
Mar 1, 2018, 9:29:03 AM (6 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:
1f37ed02
Parents:
b002261 (diff), 446ffa3 (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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/SemanticError.h

    rb002261 rdcbb03b  
    1616#pragma once
    1717
    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
    23 
    24 #include "CodeLocation.h"                                                               // for CodeLocation, toString
     18#include "ErrorObjects.h"
    2519
    2620//-----------------------------------------------------------------------------
    2721// Errors
    28 struct error {
    29         CodeLocation location;
    30         std::string description;
    3122
    32         error() = default;
    33         error( CodeLocation loc, const std::string & str ) : location( loc ), description( str ) {}
    34 };
    35 
    36 class SemanticError : public std::exception {
    37   public:
    38         SemanticError() = default;
    39         SemanticError( CodeLocation location, std::string error );
    40         ~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);
    45 
    46         static inline const std::string & error_str() {
    47                 static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
    48                 return str;
    49         }
    50 
    51         void append( SemanticError & other );
    52         void append( CodeLocation location, const std::string & );
    53         bool isEmpty() const;
    54         void print();
    55   private:
    56         std::list< error > errors;
    57 };
     23__attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error );
    5824
    5925template< typename T >
    60 SemanticError::SemanticError( const T * obj, const std::string & error )
    61         : SemanticError( obj->location, toString( error, obj ) )
    62 {}
     26__attribute__((noreturn)) static inline void SemanticError( const T * obj, const std::string & error ) {
     27        SemanticError( obj->location, toString( error, obj ) );
     28}
    6329
    6430template< typename T >
    65 SemanticError::SemanticError( CodeLocation location, const T * obj, const std::string & error )
    66         : SemanticError( location, toString( error, obj ) )
    67 {}
     31__attribute__((noreturn)) static inline void SemanticError( CodeLocation location, const T * obj, const std::string & error ) {
     32        SemanticError( location, toString( error, obj ) );
     33}
    6834
    6935//-----------------------------------------------------------------------------
    7036// Warnings
    71 class SemanticWarning {
    72   public:
    73         SemanticWarning( CodeLocation location, std::string error );
    74         ~SemanticWarning() throw() {}
    7537
    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);
     38constexpr const char * const WarningFormats[] = {
    7939
    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:
    8640};
    8741
    88 template< typename T >
    89 SemanticWarning::SemanticWarning( const T * obj, const std::string & error )
    90         : SemanticWarning( obj->location, toString( error, obj ) )
    91 {}
     42enum class Warning {
     43        NUMBER_OF_WARNINGS, //This MUST be the last warning
     44};
    9245
    93 template< typename T >
    94 SemanticWarning::SemanticWarning( CodeLocation location, const T * obj, const std::string & error )
    95         : SemanticWarning( location, toString( error, obj ) )
    96 {}
     46static_assert(
     47        (sizeof(WarningFormats) / sizeof(WarningFormats[0])) == ((unsigned long)Warning::NUMBER_OF_WARNINGS),
     48        "Each warning format should have a corresponding warning enum value"
     49);
     50
     51#define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[id], __VA_ARGS__)
     52
     53void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4)));
     54
    9755
    9856//-----------------------------------------------------------------------------
    9957// Helpers
    100 static inline const std::string & bold_ttycode() {
    101         static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
    102         return str;
     58namespace ErrorHelpers {
     59        const std::string & error_str();
     60        const std::string & warning_str();
     61        const std::string & bold_ttycode();
     62        const std::string & reset_font_ttycode();
     63
     64        std::string make_bold( const std::string & str );
     65
     66        struct bold {};
     67        std::ostream & operator<<(std::ostream & os, bold);
     68
     69        struct reset_font {};
     70        std::ostream & operator<<(std::ostream & os, reset_font);
    10371}
    10472
    105 static inline const std::string & reset_font_ttycode() {
    106         static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
    107         return str;
    108 }
    10973
    110 static inline std::string make_bold( const std::string & str ) {
    111         return bold_ttycode() + str + reset_font_ttycode();
    112 }
    11374
    114 struct bold {};
    115 static inline std::ostream & operator<<(std::ostream & os, bold) {
    116         os << bold_ttycode();
    117         return os;
    118 }
    119 
    120 struct reset_font {};
    121 static inline std::ostream & operator<<(std::ostream & os, reset_font) {
    122         os << reset_font_ttycode();
    123         return os;
    124 }
    12575
    12676// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.