Changeset a16764a6 for src/Common
- Timestamp:
- Feb 28, 2018, 4:48:22 PM (8 years ago)
- 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:
- 446ffa3
- Parents:
- 6a8df56
- Location:
- src/Common
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/PassVisitor.impl.h
r6a8df56 ra16764a6 65 65 DeclList_t* beforeDecls = visitor.get_beforeDecls(); 66 66 DeclList_t* afterDecls = visitor.get_afterDecls(); 67 SemanticError errors;67 SemanticErrorException errors; 68 68 69 69 for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) { … … 76 76 // run visitor on declaration 77 77 maybeAccept_impl( *i, visitor ); 78 } catch( SemanticError &e ) {78 } catch( SemanticErrorException &e ) { 79 79 errors.append( e ); 80 80 } … … 92 92 DeclList_t* beforeDecls = mutator.get_beforeDecls(); 93 93 DeclList_t* afterDecls = mutator.get_afterDecls(); 94 SemanticError errors;94 SemanticErrorException errors; 95 95 96 96 for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) { … … 102 102 // run mutator on declaration 103 103 maybeMutate_impl( *i, mutator ); 104 } catch( SemanticError &e ) {104 } catch( SemanticErrorException &e ) { 105 105 errors.append( e ); 106 106 } … … 125 125 inline void maybeAccept_impl( Container & container, PassVisitor< pass_type > & visitor ) { 126 126 if ( ! visitor.get_visit_children() ) return; 127 SemanticError errors;127 SemanticErrorException errors; 128 128 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { 129 129 try { … … 131 131 (*i)->accept( visitor ); 132 132 } 133 } catch( SemanticError &e ) {133 } catch( SemanticErrorException &e ) { 134 134 errors.append( e ); 135 135 } … … 152 152 inline void maybeMutate_impl( Container & container, PassVisitor< pass_type > & mutator ) { 153 153 if ( ! mutator.get_visit_children() ) return; 154 SemanticError errors;154 SemanticErrorException errors; 155 155 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { 156 156 try { … … 159 159 assert( *i ); 160 160 } // if 161 } catch( SemanticError &e ) {161 } catch( SemanticErrorException &e ) { 162 162 errors.append( e ); 163 163 } // try … … 172 172 void PassVisitor< pass_type >::handleStatementList( std::list< Statement * > & statements, func_t func ) { 173 173 if ( ! get_visit_children() ) return; 174 SemanticError errors;174 SemanticErrorException errors; 175 175 176 176 // don't want statements from outer CompoundStmts to be added to this CompoundStmt … … 195 195 || ( empty( beforeDecls ) && empty( afterDecls )) ); 196 196 197 } catch ( SemanticError &e ) {197 } catch ( SemanticErrorException &e ) { 198 198 errors.append( e ); 199 199 } -
src/Common/SemanticError.cc
r6a8df56 ra16764a6 23 23 #include "SemanticError.h" 24 24 25 SemanticError ::SemanticError( CodeLocation location, std::string error ) {25 SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) { 26 26 append( location, error ); 27 27 } 28 28 29 void SemanticError ::append( SemanticError&other ) {29 void SemanticErrorException::append( SemanticErrorException &other ) { 30 30 errors.splice( errors.end(), other.errors ); 31 31 } 32 32 33 void SemanticError ::append( CodeLocation location, const std::string & msg ) {33 void SemanticErrorException::append( CodeLocation location, const std::string & msg ) { 34 34 errors.emplace_back( location, msg ); 35 35 } 36 36 37 bool SemanticError ::isEmpty() const {37 bool SemanticErrorException::isEmpty() const { 38 38 return errors.empty(); 39 39 } 40 40 41 void SemanticError ::print() {41 void SemanticErrorException::print() { 42 42 using std::to_string; 43 43 for( auto err : errors ) { 44 std::cerr << bold() << err.location << error_str() <<reset_font() << err.description << std::endl;44 std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl; 45 45 } 46 46 } 47 47 48 SemanticWarning::SemanticWarning( CodeLocation location, std::string msg ) { 49 std::cerr << bold() << location << warning_str() << reset_font() << msg << std::endl; 48 void SemanticError( CodeLocation location, std::string error ) { 49 throw SemanticErrorException(location, error); 50 } 51 52 void SemanticWarningImpl( CodeLocation location, std::string msg ) { 53 std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl; 54 } 55 56 //----------------------------------------------------------------------------- 57 // Helpers 58 namespace ErrorHelpers { 59 const std::string & error_str() { 60 static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: "; 61 return str; 62 } 63 64 const std::string & warning_str() { 65 static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: "; 66 return str; 67 } 68 69 const std::string & bold_ttycode() { 70 static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : ""; 71 return str; 72 } 73 74 const std::string & reset_font_ttycode() { 75 static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : ""; 76 return str; 77 } 78 79 std::string make_bold( const std::string & str ) { 80 return bold_ttycode() + str + reset_font_ttycode(); 81 } 82 83 std::ostream & operator<<(std::ostream & os, bold) { 84 os << bold_ttycode(); 85 return os; 86 } 87 88 std::ostream & operator<<(std::ostream & os, reset_font) { 89 os << reset_font_ttycode(); 90 return os; 91 } 50 92 } 51 93 -
src/Common/SemanticError.h
r6a8df56 ra16764a6 16 16 #pragma once 17 17 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" 25 19 26 20 //----------------------------------------------------------------------------- 27 21 // Errors 28 struct error {29 CodeLocation location;30 std::string description;31 22 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 ); 58 24 59 25 template< 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 } 63 29 64 30 template< 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 } 68 34 69 35 //----------------------------------------------------------------------------- 70 36 // Warnings 71 class SemanticWarning {72 public:73 SemanticWarning( CodeLocation location, std::string error );74 ~SemanticWarning() throw() {}75 37 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); 38 constexpr const char * const WarningFormats[] = { 79 39 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 40 }; 87 41 88 template< typename T > 89 SemanticWarning::SemanticWarning( const T * obj, const std::string & error ) 90 : SemanticWarning( obj->location, toString( error, obj ) ) 91 {} 42 enum class Warning { 43 NUMBER_OF_WARNINGS, //This MUST be the last warning 44 }; 92 45 93 template< typename T > 94 SemanticWarning::SemanticWarning( CodeLocation location, const T * obj, const std::string & error ) 95 : SemanticWarning( location, toString( error, obj ) ) 96 {} 46 static_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 53 void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4))); 54 97 55 98 56 //----------------------------------------------------------------------------- 99 57 // Helpers 100 static inline const std::string & bold_ttycode() { 101 static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : ""; 102 return str; 58 namespace 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); 103 71 } 104 72 105 static inline const std::string & reset_font_ttycode() {106 static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";107 return str;108 }109 73 110 static inline std::string make_bold( const std::string & str ) {111 return bold_ttycode() + str + reset_font_ttycode();112 }113 74 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 }125 75 126 76 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.