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.cc -- |
---|
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 <cstdarg> |
---|
17 | #include <cstdio> // for fileno, stderr |
---|
18 | #include <cstring> |
---|
19 | #include <unistd.h> // for isatty |
---|
20 | #include <iostream> // for basic_ostream, operator<<, ostream |
---|
21 | #include <list> // for list, _List_iterator |
---|
22 | #include <string> // for string, operator<<, operator+, to_string |
---|
23 | #include <vector> |
---|
24 | |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | #include "Common/utility.h" // for to_string, CodeLocation (ptr only) |
---|
28 | #include "SemanticError.h" |
---|
29 | |
---|
30 | //----------------------------------------------------------------------------- |
---|
31 | // Severity Handling |
---|
32 | vector<Severity> & get_severities() { |
---|
33 | static vector<Severity> severities; |
---|
34 | if(severities.empty()) { |
---|
35 | severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS); |
---|
36 | for ( const auto w : WarningFormats ) { |
---|
37 | severities.push_back( w.default_severity ); |
---|
38 | } // for |
---|
39 | } |
---|
40 | return severities; |
---|
41 | } |
---|
42 | |
---|
43 | void SemanticWarning_SuppressAll() { |
---|
44 | for( auto & s : get_severities() ) { |
---|
45 | s = Severity::Suppress; |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | void SemanticWarning_EnableAll() { |
---|
50 | for( auto & s : get_severities() ) { |
---|
51 | s = Severity::Warn; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | void SemanticWarning_WarningAsError() { |
---|
56 | for( auto & s : get_severities() ) { |
---|
57 | if(s == Severity::Warn) s = Severity::Error; |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | void SemanticWarning_Set(const char * const name, Severity s) { |
---|
62 | size_t idx = 0; |
---|
63 | for ( const auto & w : WarningFormats ) { |
---|
64 | if ( strcmp( name, w.name ) == 0 ) { |
---|
65 | get_severities()[idx] = s; |
---|
66 | break; |
---|
67 | } |
---|
68 | idx++; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | //----------------------------------------------------------------------------- |
---|
73 | // Semantic Error |
---|
74 | |
---|
75 | bool SemanticErrorThrow = false; |
---|
76 | |
---|
77 | SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) { |
---|
78 | append( location, error ); |
---|
79 | } |
---|
80 | |
---|
81 | void SemanticErrorException::append( SemanticErrorException &other ) { |
---|
82 | errors.splice( errors.end(), other.errors ); |
---|
83 | } |
---|
84 | |
---|
85 | void SemanticErrorException::append( CodeLocation location, const string & msg ) { |
---|
86 | errors.emplace_back( location, msg ); |
---|
87 | } |
---|
88 | |
---|
89 | bool SemanticErrorException::isEmpty() const { |
---|
90 | return errors.empty(); |
---|
91 | } |
---|
92 | |
---|
93 | void SemanticErrorException::print() { |
---|
94 | // using to_string; |
---|
95 | |
---|
96 | errors.sort([](const error & lhs, const error & rhs) -> bool { |
---|
97 | if(lhs.location.startsBefore(rhs.location)) return true; |
---|
98 | if(rhs.location.startsBefore(lhs.location)) return false; |
---|
99 | |
---|
100 | return lhs.description < rhs.description; |
---|
101 | }); |
---|
102 | |
---|
103 | for( auto err : errors ) { |
---|
104 | cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << endl; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void SemanticError( CodeLocation location, const char * fmt, ... ) { |
---|
109 | char msg[2048]; // worst-case error-message buffer |
---|
110 | va_list args; |
---|
111 | va_start( args, fmt ); |
---|
112 | vsnprintf( msg, sizeof(msg), fmt, args ); // always null terminated, but may be truncated |
---|
113 | va_end( args ); |
---|
114 | |
---|
115 | SemanticErrorThrow = true; |
---|
116 | throw SemanticErrorException( location, msg ); // convert msg to string |
---|
117 | } |
---|
118 | |
---|
119 | void SemanticWarning( CodeLocation location, Warning warning, ... ) { |
---|
120 | Severity severity = get_severities()[(int)warning]; |
---|
121 | |
---|
122 | switch ( severity ) { |
---|
123 | case Severity::Suppress : |
---|
124 | break; |
---|
125 | case Severity::Warn : |
---|
126 | case Severity::Error : |
---|
127 | { |
---|
128 | char msg[2048]; // worst-case error-message buffer |
---|
129 | va_list args; |
---|
130 | va_start( args, warning ); |
---|
131 | vsnprintf( msg, sizeof(msg), WarningFormats[(int)warning].message, args ); // always null terminated, but may be truncated |
---|
132 | va_end( args ); |
---|
133 | |
---|
134 | if ( severity == Severity::Warn ) { |
---|
135 | cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << endl; |
---|
136 | } else { |
---|
137 | SemanticError( location, string( msg ) ); |
---|
138 | } |
---|
139 | } |
---|
140 | break; |
---|
141 | case Severity::Critical : |
---|
142 | assertf(false, "Critical errors not implemented yet"); |
---|
143 | break; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | //----------------------------------------------------------------------------- |
---|
148 | // Helpers |
---|
149 | namespace ErrorHelpers { |
---|
150 | Colors colors = Colors::Auto; |
---|
151 | |
---|
152 | static inline bool with_colors() { |
---|
153 | return colors == Colors::Auto ? isatty( STDERR_FILENO ) : bool(colors); |
---|
154 | } |
---|
155 | |
---|
156 | const string & error_str() { |
---|
157 | static string str = with_colors() ? "\e[31merror:\e[39m " : "error: "; |
---|
158 | return str; |
---|
159 | } |
---|
160 | |
---|
161 | const string & warning_str() { |
---|
162 | static string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: "; |
---|
163 | return str; |
---|
164 | } |
---|
165 | |
---|
166 | const string & bold_ttycode() { |
---|
167 | static string str = with_colors() ? "\e[1m" : ""; |
---|
168 | return str; |
---|
169 | } |
---|
170 | |
---|
171 | const string & reset_font_ttycode() { |
---|
172 | static string str = with_colors() ? "\e[0m" : ""; |
---|
173 | return str; |
---|
174 | } |
---|
175 | |
---|
176 | string make_bold( const string & str ) { |
---|
177 | return bold_ttycode() + str + reset_font_ttycode(); |
---|
178 | } |
---|
179 | |
---|
180 | ostream & operator<<(ostream & os, bold) { |
---|
181 | os << bold_ttycode(); |
---|
182 | return os; |
---|
183 | } |
---|
184 | |
---|
185 | ostream & operator<<(ostream & os, reset_font) { |
---|
186 | os << reset_font_ttycode(); |
---|
187 | return os; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | // Local Variables: // |
---|
192 | // tab-width: 4 // |
---|
193 | // mode: c++ // |
---|
194 | // compile-command: "make install" // |
---|
195 | // End: // |
---|