[51587aa] | 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 | //
|
---|
[c92bdcc] | 7 | // SemanticError.cpp --
|
---|
[51587aa] | 8 | //
|
---|
[af39199d] | 9 | // Author : Thierry Delisle
|
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[01aeade] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[610354a] | 12 | // Last Modified On : Thu Dec 14 13:45:28 2023
|
---|
| 13 | // Update Count : 34
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[c92bdcc] | 16 | #include "SemanticError.hpp"
|
---|
| 17 |
|
---|
[244b934] | 18 | #include <cstdarg>
|
---|
[9ed4f94] | 19 | #include <cstdio> // for fileno, stderr
|
---|
[68e9ace] | 20 | #include <cstring>
|
---|
[9ed4f94] | 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
|
---|
[68e9ace] | 25 | #include <vector>
|
---|
[51b73452] | 26 |
|
---|
[c92bdcc] | 27 | #include "Common/Utility.hpp" // for to_string, CodeLocation (ptr only)
|
---|
[610354a] | 28 |
|
---|
[c92bdcc] | 29 | using namespace std;
|
---|
[51b73452] | 30 |
|
---|
[68e9ace] | 31 | //-----------------------------------------------------------------------------
|
---|
| 32 | // Severity Handling
|
---|
[610354a] | 33 | vector<Severity> & get_severities() {
|
---|
| 34 | static vector<Severity> severities;
|
---|
[68e9ace] | 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 |
|
---|
| 44 | void SemanticWarning_SuppressAll() {
|
---|
| 45 | for( auto & s : get_severities() ) {
|
---|
| 46 | s = Severity::Suppress;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | void SemanticWarning_EnableAll() {
|
---|
| 51 | for( auto & s : get_severities() ) {
|
---|
| 52 | s = Severity::Warn;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void SemanticWarning_WarningAsError() {
|
---|
| 57 | for( auto & s : get_severities() ) {
|
---|
| 58 | if(s == Severity::Warn) s = Severity::Error;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void SemanticWarning_Set(const char * const name, Severity s) {
|
---|
| 63 | size_t idx = 0;
|
---|
[af39199d] | 64 | for ( const auto & w : WarningFormats ) {
|
---|
[610354a] | 65 | if ( strcmp( name, w.name ) == 0 ) {
|
---|
[68e9ace] | 66 | get_severities()[idx] = s;
|
---|
| 67 | break;
|
---|
| 68 | }
|
---|
| 69 | idx++;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | //-----------------------------------------------------------------------------
|
---|
| 74 | // Semantic Error
|
---|
[b1f2007d] | 75 |
|
---|
[4358c1e] | 76 | bool SemanticErrorThrow = false;
|
---|
| 77 |
|
---|
[610354a] | 78 | SemanticErrorException::SemanticErrorException( CodeLocation location, string error ) {
|
---|
[d55d7a6] | 79 | append( location, error );
|
---|
[51b73452] | 80 | }
|
---|
| 81 |
|
---|
[a16764a6] | 82 | void SemanticErrorException::append( SemanticErrorException &other ) {
|
---|
[138e29e] | 83 | errors.splice( errors.end(), other.errors );
|
---|
[3906301] | 84 | }
|
---|
| 85 |
|
---|
[610354a] | 86 | void SemanticErrorException::append( CodeLocation location, const string & msg ) {
|
---|
[d55d7a6] | 87 | errors.emplace_back( location, msg );
|
---|
[51b73452] | 88 | }
|
---|
| 89 |
|
---|
[16ba4897] | 90 | void SemanticErrorException::throwIfNonEmpty() {
|
---|
| 91 | if ( !errors.empty() ) {
|
---|
| 92 | throw *this;
|
---|
| 93 | }
|
---|
[51b73452] | 94 | }
|
---|
| 95 |
|
---|
[a16764a6] | 96 | void SemanticErrorException::print() {
|
---|
[610354a] | 97 | // using to_string;
|
---|
[ddcedfe] | 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 |
|
---|
[9ed4f94] | 106 | for( auto err : errors ) {
|
---|
[610354a] | 107 | cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << endl;
|
---|
[138e29e] | 108 | }
|
---|
[51b73452] | 109 | }
|
---|
[01aeade] | 110 |
|
---|
[b1f2007d] | 111 | void 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 |
|
---|
[610354a] | 122 | void SemanticWarning( CodeLocation location, Warning warning, ... ) {
|
---|
[68e9ace] | 123 | Severity severity = get_severities()[(int)warning];
|
---|
[610354a] | 124 |
|
---|
| 125 | switch ( severity ) {
|
---|
[6040e67d] | 126 | case Severity::Suppress :
|
---|
| 127 | break;
|
---|
| 128 | case Severity::Warn :
|
---|
| 129 | case Severity::Error :
|
---|
| 130 | {
|
---|
[610354a] | 131 | char msg[2048]; // worst-case error-message buffer
|
---|
[6040e67d] | 132 | va_list args;
|
---|
[610354a] | 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 | }
|
---|
[6040e67d] | 142 | }
|
---|
| 143 | break;
|
---|
| 144 | case Severity::Critical :
|
---|
| 145 | assertf(false, "Critical errors not implemented yet");
|
---|
| 146 | break;
|
---|
| 147 | }
|
---|
[a16764a6] | 148 | }
|
---|
| 149 |
|
---|
| 150 | //-----------------------------------------------------------------------------
|
---|
| 151 | // Helpers
|
---|
| 152 | namespace ErrorHelpers {
|
---|
[1a69a90] | 153 | Colors colors = Colors::Auto;
|
---|
| 154 |
|
---|
| 155 | static inline bool with_colors() {
|
---|
| 156 | return colors == Colors::Auto ? isatty( STDERR_FILENO ) : bool(colors);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[610354a] | 159 | const string & error_str() {
|
---|
| 160 | static string str = with_colors() ? "\e[31merror:\e[39m " : "error: ";
|
---|
[a16764a6] | 161 | return str;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[610354a] | 164 | const string & warning_str() {
|
---|
| 165 | static string str = with_colors() ? "\e[95mwarning:\e[39m " : "warning: ";
|
---|
[a16764a6] | 166 | return str;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[610354a] | 169 | const string & bold_ttycode() {
|
---|
| 170 | static string str = with_colors() ? "\e[1m" : "";
|
---|
[a16764a6] | 171 | return str;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[610354a] | 174 | const string & reset_font_ttycode() {
|
---|
| 175 | static string str = with_colors() ? "\e[0m" : "";
|
---|
[a16764a6] | 176 | return str;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[610354a] | 179 | string make_bold( const string & str ) {
|
---|
[a16764a6] | 180 | return bold_ttycode() + str + reset_font_ttycode();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[610354a] | 183 | ostream & operator<<(ostream & os, bold) {
|
---|
[a16764a6] | 184 | os << bold_ttycode();
|
---|
| 185 | return os;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[610354a] | 188 | ostream & operator<<(ostream & os, reset_font) {
|
---|
[a16764a6] | 189 | os << reset_font_ttycode();
|
---|
| 190 | return os;
|
---|
| 191 | }
|
---|
[294647b] | 192 | }
|
---|
| 193 |
|
---|
[51587aa] | 194 | // Local Variables: //
|
---|
| 195 | // tab-width: 4 //
|
---|
| 196 | // mode: c++ //
|
---|
| 197 | // compile-command: "make install" //
|
---|
| 198 | // End: //
|
---|