source: src/Common/SemanticError.cpp @ cad8c88

Last change on this file since cad8c88 was c92bdcc, checked in by Andrew Beach <ajbeach@…>, 8 weeks ago

Updated the rest of the names in src/ (except for the generated files).

  • Property mode set to 100644
File size: 5.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// SemanticError.cpp --
8//
9// Author           : Thierry Delisle
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Dec 14 13:45:28 2023
13// Update Count     : 34
14//
15
16#include "SemanticError.hpp"
17
18#include <cstdarg>
19#include <cstdio>                                                                               // for fileno, stderr
20#include <cstring>
21#include <unistd.h>                                                                             // for isatty
22#include <iostream>                                                                             // for basic_ostream, operator<<, ostream
23#include <list>                                                                                 // for list, _List_iterator
24#include <string>                                                                               // for string, operator<<, operator+, to_string
25#include <vector>
26
27#include "Common/Utility.hpp"                                                   // for to_string, CodeLocation (ptr only)
28
29using namespace std;
30
31//-----------------------------------------------------------------------------
32// Severity Handling
33vector<Severity> & get_severities() {
34        static vector<Severity> severities;
35        if(severities.empty()) {
36                severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS);
37                for ( const auto w : WarningFormats ) {
38                        severities.push_back( w.default_severity );
39                } // for
40        }
41        return severities;
42}
43
44void SemanticWarning_SuppressAll() {
45        for( auto & s : get_severities() ) {
46                s = Severity::Suppress;
47        }
48}
49
50void SemanticWarning_EnableAll() {
51        for( auto & s : get_severities() ) {
52                s = Severity::Warn;
53        }
54}
55
56void SemanticWarning_WarningAsError() {
57        for( auto & s : get_severities() ) {
58                if(s == Severity::Warn) s = Severity::Error;
59        }
60}
61
62void SemanticWarning_Set(const char * const name, Severity s) {
63        size_t idx = 0;
64        for ( const auto & w : WarningFormats ) {
65                if ( strcmp( name, w.name ) == 0 ) {
66                        get_severities()[idx] = s;
67                        break;
68                }
69                idx++;
70        }
71}
72
73//-----------------------------------------------------------------------------
74// Semantic Error
75
76bool SemanticErrorThrow = false;
77
78SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) {
79        append( location, error );
80}
81
82void SemanticErrorException::append( SemanticErrorException &other ) {
83        errors.splice( errors.end(), other.errors );
84}
85
86void SemanticErrorException::append( CodeLocation location, const string & msg ) {
87        errors.emplace_back( location, msg );
88}
89
90bool SemanticErrorException::isEmpty() const {
91        return errors.empty();
92}
93
94void SemanticErrorException::print() {
95//      using to_string;
96
97        errors.sort([](const error & lhs, const error & rhs) -> bool {
98                if(lhs.location.startsBefore(rhs.location)) return true;
99                if(rhs.location.startsBefore(lhs.location)) return false;
100
101                return lhs.description < rhs.description;
102        });
103
104        for( auto err : errors ) {
105                cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << endl;
106        }
107}
108
109void SemanticError( CodeLocation location, const char * fmt, ... ) {
110        char msg[2048];                                                                         // worst-case error-message buffer
111        va_list args;
112        va_start( args, fmt );
113        vsnprintf( msg, sizeof(msg), fmt, args );                       // always null terminated, but may be truncated
114        va_end( args );
115
116        SemanticErrorThrow = true;
117        throw SemanticErrorException( location, msg );          // convert msg to string
118}
119
120void SemanticWarning( CodeLocation location, Warning warning, ... ) {
121        Severity severity = get_severities()[(int)warning];
122
123        switch ( severity ) {
124        case Severity::Suppress :
125                break;
126        case Severity::Warn :
127        case Severity::Error :
128                {
129                        char msg[2048];                                                         // worst-case error-message buffer
130                        va_list args;
131                        va_start( args, warning );
132                        vsnprintf( msg, sizeof(msg), WarningFormats[(int)warning].message, args ); // always null terminated, but may be truncated
133                        va_end( args );
134
135                        if ( severity == Severity::Warn ) {
136                                cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << endl;
137                        } else {
138                                SemanticError( location, string( msg ) );
139                        }
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        Colors colors = Colors::Auto;
152
153        static inline bool with_colors() {
154                return colors == Colors::Auto ? isatty( STDERR_FILENO ) : bool(colors);
155        }
156
157        const string & error_str() {
158                static string str = with_colors() ? "\e[31merror:\e[39m " : "error: ";
159                return str;
160        }
161
162        const string & warning_str() {
163                static string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: ";
164                return str;
165        }
166
167        const string & bold_ttycode() {
168                static string str = with_colors() ? "\e[1m" : "";
169                return str;
170        }
171
172        const string & reset_font_ttycode() {
173                static string str = with_colors() ? "\e[0m" : "";
174                return str;
175        }
176
177        string make_bold( const string & str ) {
178                return bold_ttycode() + str + reset_font_ttycode();
179        }
180
181        ostream & operator<<(ostream & os, bold) {
182                os << bold_ttycode();
183                return os;
184        }
185
186        ostream & operator<<(ostream & os, reset_font) {
187                os << reset_font_ttycode();
188                return os;
189        }
190}
191
192// Local Variables: //
193// tab-width: 4 //
194// mode: c++ //
195// compile-command: "make install" //
196// End: //
Note: See TracBrowser for help on using the repository browser.