Changeset 138e29e for src/Common


Ignore:
Timestamp:
Feb 14, 2017, 3:53:52 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:
9bb90a86
Parents:
294647b
Message:

Implemented filename and linenumber errors in most cases, only missing constructor errors apparently

Location:
src/Common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/Common/SemanticError.cc

    r294647b r138e29e  
    2222#include "SemanticError.h"
    2323
     24#include <unistd.h>
     25
     26inline const std::string& error_str() {
     27        static std::string str = isatty( fileno(stderr) ) ? "\e[31merror:\e[39m " : "error: ";
     28        return str;
     29}
     30
    2431SemanticError::SemanticError() {
    2532}
    2633
    2734SemanticError::SemanticError( std::string error ) {
    28   append( error );
     35        append( error );
    2936}
    3037
    3138void SemanticError::append( SemanticError &other ) {
    32   errors.splice( errors.end(), other.errors );
     39        errors.splice( errors.end(), other.errors );
    3340}
    3441
    3542void 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 );
    3944}
    4045
     
    4449
    4550void 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        }
    4755}
    4856
    4957void 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 );
    5559}
    5660
  • TabularUnified src/Common/SemanticError.h

    r294647b r138e29e  
    2525#include "utility.h"
    2626
     27struct 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
    2741class SemanticError : public std::exception {
    2842  public:
     
    4155        // representation of the obj (T must have a print method)
    4256  private:
    43         std::list< std::string > errors;
    44         CodeLocation location;
     57        std::list< error > errors;
    4558};
    4659
  • TabularUnified src/Common/utility.h

    r294647b r138e29e  
    320320
    321321inline 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) + " " : "";
    323323}
    324324#endif // _UTILITY_H
Note: See TracChangeset for help on using the changeset viewer.