Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (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' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/SemanticError.cc

    rf9feab8 r90152a4  
    77// SemanticError.cc --
    88//
    9 // Author           : Richard C. Bilson
     9// Author           : Thierry Delisle
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 29 18:17:35 2017
    13 // Update Count     : 3
     12// Last Modified On : Thu Jun  7 08:05:26 2018
     13// Update Count     : 10
    1414//
    1515
     16#include <cstdarg>
    1617#include <cstdio>                                                                               // for fileno, stderr
     18#include <cstring>
    1719#include <unistd.h>                                                                             // for isatty
    1820#include <iostream>                                                                             // for basic_ostream, operator<<, ostream
    1921#include <list>                                                                                 // for list, _List_iterator
    2022#include <string>                                                                               // for string, operator<<, operator+, to_string
     23#include <vector>
    2124
    2225#include "Common/utility.h"                                                             // for to_string, CodeLocation (ptr only)
    2326#include "SemanticError.h"
    2427
    25 SemanticError::SemanticError() {
     28//-----------------------------------------------------------------------------
     29// Severity Handling
     30std::vector<Severity> & get_severities() {
     31        static std::vector<Severity> severities;
     32        if(severities.empty()) {
     33                severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS);
     34                for ( const auto w : WarningFormats ) {
     35                        severities.push_back( w.default_severity );
     36                } // for
     37        }
     38        return severities;
    2639}
    2740
    28 SemanticError::SemanticError( std::string error ) {
    29         append( error );
     41void SemanticWarning_SuppressAll() {
     42        for( auto & s : get_severities() ) {
     43                s = Severity::Suppress;
     44        }
    3045}
    3146
    32 void SemanticError::append( SemanticError &other ) {
     47void SemanticWarning_EnableAll() {
     48        for( auto & s : get_severities() ) {
     49                s = Severity::Warn;
     50        }
     51}
     52
     53void SemanticWarning_WarningAsError() {
     54        for( auto & s : get_severities() ) {
     55                if(s == Severity::Warn) s = Severity::Error;
     56        }
     57}
     58
     59void SemanticWarning_Set(const char * const name, Severity s) {
     60        size_t idx = 0;
     61        for ( const auto & w : WarningFormats ) {
     62                if ( std::strcmp( name, w.name ) == 0 ) {
     63                        get_severities()[idx] = s;
     64                        break;
     65                }
     66                idx++;
     67        }
     68}
     69
     70//-----------------------------------------------------------------------------
     71// Semantic Error
     72bool SemanticErrorThrow = false;
     73
     74SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
     75        append( location, error );
     76}
     77
     78void SemanticErrorException::append( SemanticErrorException &other ) {
    3379        errors.splice( errors.end(), other.errors );
    3480}
    3581
    36 void SemanticError::append( const std::string & msg ) {
    37         errors.emplace_back( error_str() + msg );
     82void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
     83        errors.emplace_back( location, msg );
    3884}
    3985
    40 bool SemanticError::isEmpty() const {
     86bool SemanticErrorException::isEmpty() const {
    4187        return errors.empty();
    4288}
    4389
    44 void SemanticError::print( std::ostream &os ) {
     90void SemanticErrorException::print() {
    4591        using std::to_string;
    4692        for( auto err : errors ) {
    47                 os << err.location << err.description << std::endl;
     93                std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl;
    4894        }
    4995}
    5096
    51 void SemanticError::set_location( const CodeLocation& location ) {
    52         errors.begin()->maybeSet( location );
     97void SemanticError( CodeLocation location, std::string error ) {
     98        SemanticErrorThrow = true;
     99        throw SemanticErrorException( location, error );
     100}
     101
     102namespace {
     103        // convert format string and arguments into a single string
     104        std::string fmtToString(const char * fmt, va_list ap) {
     105                int size = 128;
     106                while ( true ) {
     107                        char buf[size];
     108                        va_list args;
     109                        va_copy( args, ap );
     110                        int n = vsnprintf(&buf[0], size, fmt, args);
     111                        va_end( args );
     112                        if ( n < size && n >= 0 ) return buf;
     113                        size *= 2;
     114                }
     115                assert( false );
     116        }
     117}
     118
     119void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) {
     120        Severity severity = get_severities()[(int)warning];
     121        switch(severity) {
     122        case Severity::Suppress :
     123                break;
     124        case Severity::Warn :
     125                {
     126                        va_list args;
     127                        va_start(args, fmt);
     128                        std::string msg = fmtToString( fmt, args );
     129                        va_end(args);
     130                        std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl;
     131                }
     132                break;
     133        case Severity::Error :
     134                {
     135                        va_list args;
     136                        va_start(args, fmt);
     137                        std::string msg = fmtToString( fmt, args );
     138                        va_end(args);
     139                        SemanticError(location, msg);
     140                }
     141                break;
     142        case Severity::Critical :
     143                assertf(false, "Critical errors not implemented yet");
     144                break;
     145        }
     146}
     147
     148//-----------------------------------------------------------------------------
     149// Helpers
     150namespace ErrorHelpers {
     151        const std::string & error_str() {
     152                static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
     153                return str;
     154        }
     155
     156        const std::string & warning_str() {
     157                static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
     158                return str;
     159        }
     160
     161        const std::string & bold_ttycode() {
     162                static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
     163                return str;
     164        }
     165
     166        const std::string & reset_font_ttycode() {
     167                static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
     168                return str;
     169        }
     170
     171        std::string make_bold( const std::string & str ) {
     172                return bold_ttycode() + str + reset_font_ttycode();
     173        }
     174
     175        std::ostream & operator<<(std::ostream & os, bold) {
     176                os << bold_ttycode();
     177                return os;
     178        }
     179
     180        std::ostream & operator<<(std::ostream & os, reset_font) {
     181                os << reset_font_ttycode();
     182                return os;
     183        }
    53184}
    54185
Note: See TracChangeset for help on using the changeset viewer.