source: src/Common/SemanticError.cpp@ 35cc6d4

Last change on this file since 35cc6d4 was 16ba4897, checked in by Andrew Beach <ajbeach@…>, 12 months ago

Replaced SemanticErrorException::isEmpty with ...::throwIfNonEmpty. This is how it was used in every context and it saves a bit of text (if not two lines) at every use. I considered putting this function in the header for better inlining, but this should have at least the same preformance as the last version.

  • 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
90void SemanticErrorException::throwIfNonEmpty() {
91 if ( !errors.empty() ) {
92 throw *this;
93 }
94}
95
96void SemanticErrorException::print() {
97// using to_string;
98
99 errors.sort([](const error & lhs, const error & rhs) -> bool {
100 if(lhs.location.startsBefore(rhs.location)) return true;
101 if(rhs.location.startsBefore(lhs.location)) return false;
102
103 return lhs.description < rhs.description;
104 });
105
106 for( auto err : errors ) {
107 cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << endl;
108 }
109}
110
111void SemanticError( CodeLocation location, const char * fmt, ... ) {
112 char msg[2048]; // worst-case error-message buffer
113 va_list args;
114 va_start( args, fmt );
115 vsnprintf( msg, sizeof(msg), fmt, args ); // always null terminated, but may be truncated
116 va_end( args );
117
118 SemanticErrorThrow = true;
119 throw SemanticErrorException( location, msg ); // convert msg to string
120}
121
122void SemanticWarning( CodeLocation location, Warning warning, ... ) {
123 Severity severity = get_severities()[(int)warning];
124
125 switch ( severity ) {
126 case Severity::Suppress :
127 break;
128 case Severity::Warn :
129 case Severity::Error :
130 {
131 char msg[2048]; // worst-case error-message buffer
132 va_list args;
133 va_start( args, warning );
134 vsnprintf( msg, sizeof(msg), WarningFormats[(int)warning].message, args ); // always null terminated, but may be truncated
135 va_end( args );
136
137 if ( severity == Severity::Warn ) {
138 cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << endl;
139 } else {
140 SemanticError( location, string( msg ) );
141 }
142 }
143 break;
144 case Severity::Critical :
145 assertf(false, "Critical errors not implemented yet");
146 break;
147 }
148}
149
150//-----------------------------------------------------------------------------
151// Helpers
152namespace ErrorHelpers {
153 Colors colors = Colors::Auto;
154
155 static inline bool with_colors() {
156 return colors == Colors::Auto ? isatty( STDERR_FILENO ) : bool(colors);
157 }
158
159 const string & error_str() {
160 static string str = with_colors() ? "\e[31merror:\e[39m " : "error: ";
161 return str;
162 }
163
164 const string & warning_str() {
165 static string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: ";
166 return str;
167 }
168
169 const string & bold_ttycode() {
170 static string str = with_colors() ? "\e[1m" : "";
171 return str;
172 }
173
174 const string & reset_font_ttycode() {
175 static string str = with_colors() ? "\e[0m" : "";
176 return str;
177 }
178
179 string make_bold( const string & str ) {
180 return bold_ttycode() + str + reset_font_ttycode();
181 }
182
183 ostream & operator<<(ostream & os, bold) {
184 os << bold_ttycode();
185 return os;
186 }
187
188 ostream & operator<<(ostream & os, reset_font) {
189 os << reset_font_ttycode();
190 return os;
191 }
192}
193
194// Local Variables: //
195// tab-width: 4 //
196// mode: c++ //
197// compile-command: "make install" //
198// End: //
Note: See TracBrowser for help on using the repository browser.