Changeset 138e29e for src/Common
- Timestamp:
- Feb 14, 2017, 3:53:52 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:
- 9bb90a86
- Parents:
- 294647b
- Location:
- src/Common
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/Common/SemanticError.cc ¶
r294647b r138e29e 22 22 #include "SemanticError.h" 23 23 24 #include <unistd.h> 25 26 inline const std::string& error_str() { 27 static std::string str = isatty( fileno(stderr) ) ? "\e[31merror:\e[39m " : "error: "; 28 return str; 29 } 30 24 31 SemanticError::SemanticError() { 25 32 } 26 33 27 34 SemanticError::SemanticError( std::string error ) { 28 35 append( error ); 29 36 } 30 37 31 38 void SemanticError::append( SemanticError &other ) { 32 39 errors.splice( errors.end(), other.errors ); 33 40 } 34 41 35 42 void SemanticError::append( const std::string & msg ) { 36 using std::to_string; 37 const std::string loc = location.linenumber >= 0 ? "At \"" + to_string(location) + "\" " : ""; 38 errors.push_back( loc + "Error: " + msg ); 43 errors.emplace_back( error_str() + msg ); 39 44 } 40 45 … … 44 49 45 50 void SemanticError::print( std::ostream &os ) { 46 std::copy( errors.begin(), errors.end(), std::ostream_iterator< std::string >( os, "\n" ) ); 51 using std::to_string; 52 for(auto err : errors) { 53 os << to_string( err.location ) << err.description << '\n'; 54 } 47 55 } 48 56 49 57 void SemanticError::set_location( const CodeLocation& location ) { 50 this->location = location; 51 using std::to_string; 52 const std::string loc = location.linenumber >= 0 ? "At \"" + to_string(location) + "\" " : ""; 53 auto& error = *errors.begin(); 54 error.insert( 0, loc.c_str()); 58 errors.begin()->maybeSet( location ); 55 59 } 56 60 -
TabularUnified src/Common/SemanticError.h ¶
r294647b r138e29e 25 25 #include "utility.h" 26 26 27 struct error { 28 std::string description; 29 CodeLocation location; 30 31 error() = default; 32 error( const std::string& str ) : description( str ) {} 33 34 void maybeSet( const CodeLocation& location ) { 35 if( this->location.linenumber < 0 ) { 36 this->location = location; 37 } 38 } 39 }; 40 27 41 class SemanticError : public std::exception { 28 42 public: … … 41 55 // representation of the obj (T must have a print method) 42 56 private: 43 std::list< std::string > errors; 44 CodeLocation location; 57 std::list< error > errors; 45 58 }; 46 59 -
TabularUnified src/Common/utility.h ¶
r294647b r138e29e 320 320 321 321 inline std::string to_string( const CodeLocation& location ) { 322 return location. filename + ":" + std::to_string(location.linenumber);322 return location.linenumber >= 0 ? location.filename + ":" + std::to_string(location.linenumber) + " " : ""; 323 323 } 324 324 #endif // _UTILITY_H
Note: See TracChangeset
for help on using the changeset viewer.