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 Jun 7 08:05:26 2018 |
---|
13 | // Update Count : 10 |
---|
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 | #include "Common/utility.h" // for to_string, CodeLocation (ptr only) |
---|
26 | #include "SemanticError.h" |
---|
27 | |
---|
28 | //----------------------------------------------------------------------------- |
---|
29 | // Severity Handling |
---|
30 | std::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; |
---|
39 | } |
---|
40 | |
---|
41 | void SemanticWarning_SuppressAll() { |
---|
42 | for( auto & s : get_severities() ) { |
---|
43 | s = Severity::Suppress; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | void SemanticWarning_EnableAll() { |
---|
48 | for( auto & s : get_severities() ) { |
---|
49 | s = Severity::Warn; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | void SemanticWarning_WarningAsError() { |
---|
54 | for( auto & s : get_severities() ) { |
---|
55 | if(s == Severity::Warn) s = Severity::Error; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | void 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 |
---|
72 | bool SemanticErrorThrow = false; |
---|
73 | |
---|
74 | SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) { |
---|
75 | append( location, error ); |
---|
76 | } |
---|
77 | |
---|
78 | void SemanticErrorException::append( SemanticErrorException &other ) { |
---|
79 | errors.splice( errors.end(), other.errors ); |
---|
80 | } |
---|
81 | |
---|
82 | void SemanticErrorException::append( CodeLocation location, const std::string & msg ) { |
---|
83 | errors.emplace_back( location, msg ); |
---|
84 | } |
---|
85 | |
---|
86 | bool SemanticErrorException::isEmpty() const { |
---|
87 | return errors.empty(); |
---|
88 | } |
---|
89 | |
---|
90 | void SemanticErrorException::print() { |
---|
91 | using std::to_string; |
---|
92 | for( auto err : errors ) { |
---|
93 | std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | void SemanticError( CodeLocation location, std::string error ) { |
---|
98 | SemanticErrorThrow = true; |
---|
99 | throw SemanticErrorException( location, error ); |
---|
100 | } |
---|
101 | |
---|
102 | namespace { |
---|
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 | |
---|
119 | void 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 |
---|
150 | namespace 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 std::string & error_str() { |
---|
158 | static std::string str = with_colors() ? "\e[31merror:\e[39m " : "error: "; |
---|
159 | return str; |
---|
160 | } |
---|
161 | |
---|
162 | const std::string & warning_str() { |
---|
163 | static std::string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: "; |
---|
164 | return str; |
---|
165 | } |
---|
166 | |
---|
167 | const std::string & bold_ttycode() { |
---|
168 | static std::string str = with_colors() ? "\e[1m" : ""; |
---|
169 | return str; |
---|
170 | } |
---|
171 | |
---|
172 | const std::string & reset_font_ttycode() { |
---|
173 | static std::string str = with_colors() ? "\e[0m" : ""; |
---|
174 | return str; |
---|
175 | } |
---|
176 | |
---|
177 | std::string make_bold( const std::string & str ) { |
---|
178 | return bold_ttycode() + str + reset_font_ttycode(); |
---|
179 | } |
---|
180 | |
---|
181 | std::ostream & operator<<(std::ostream & os, bold) { |
---|
182 | os << bold_ttycode(); |
---|
183 | return os; |
---|
184 | } |
---|
185 | |
---|
186 | std::ostream & operator<<(std::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: // |
---|