[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 | // |
---|
[3906301] | 7 | // SemanticError.cc -- |
---|
[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 |
---|
[af39199d] | 12 | // Last Modified On : Wed May 2 18:13:37 2018 |
---|
| 13 | // Update Count : 8 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
[244b934] | 16 | #include <cstdarg> |
---|
[9ed4f94] | 17 | #include <cstdio> // for fileno, stderr |
---|
[68e9ace] | 18 | #include <cstring> |
---|
[9ed4f94] | 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 |
---|
[68e9ace] | 23 | #include <vector> |
---|
[51b7345] | 24 | |
---|
[9ed4f94] | 25 | #include "Common/utility.h" // for to_string, CodeLocation (ptr only) |
---|
[51b7345] | 26 | #include "SemanticError.h" |
---|
| 27 | |
---|
[68e9ace] | 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; |
---|
[af39199d] | 61 | for ( const auto & w : WarningFormats ) { |
---|
[68e9ace] | 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 |
---|
[a16764a6] | 72 | SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) { |
---|
[d55d7a6] | 73 | append( location, error ); |
---|
[51b7345] | 74 | } |
---|
| 75 | |
---|
[a16764a6] | 76 | void SemanticErrorException::append( SemanticErrorException &other ) { |
---|
[138e29e] | 77 | errors.splice( errors.end(), other.errors ); |
---|
[3906301] | 78 | } |
---|
| 79 | |
---|
[a16764a6] | 80 | void SemanticErrorException::append( CodeLocation location, const std::string & msg ) { |
---|
[d55d7a6] | 81 | errors.emplace_back( location, msg ); |
---|
[51b7345] | 82 | } |
---|
| 83 | |
---|
[a16764a6] | 84 | bool SemanticErrorException::isEmpty() const { |
---|
[01aeade] | 85 | return errors.empty(); |
---|
[51b7345] | 86 | } |
---|
| 87 | |
---|
[a16764a6] | 88 | void SemanticErrorException::print() { |
---|
[138e29e] | 89 | using std::to_string; |
---|
[9ed4f94] | 90 | for( auto err : errors ) { |
---|
[a16764a6] | 91 | std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl; |
---|
[138e29e] | 92 | } |
---|
[51b7345] | 93 | } |
---|
[01aeade] | 94 | |
---|
[a16764a6] | 95 | void SemanticError( CodeLocation location, std::string error ) { |
---|
| 96 | throw SemanticErrorException(location, error); |
---|
| 97 | } |
---|
| 98 | |
---|
[2103a51] | 99 | namespace { |
---|
| 100 | // convert format string and arguments into a single string |
---|
| 101 | std::string fmtToString(const char * fmt, va_list ap) { |
---|
| 102 | int size = 128; |
---|
| 103 | while ( true ) { |
---|
| 104 | char buf[size]; |
---|
| 105 | va_list args; |
---|
| 106 | va_copy( args, ap ); |
---|
| 107 | int n = vsnprintf(&buf[0], size, fmt, args); |
---|
| 108 | va_end( args ); |
---|
| 109 | if ( n < size && n >= 0 ) return buf; |
---|
| 110 | size *= 2; |
---|
| 111 | } |
---|
| 112 | assert( false ); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
[6040e67d] | 116 | void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) { |
---|
[68e9ace] | 117 | Severity severity = get_severities()[(int)warning]; |
---|
[6040e67d] | 118 | switch(severity) { |
---|
| 119 | case Severity::Suppress : |
---|
| 120 | break; |
---|
| 121 | case Severity::Warn : |
---|
| 122 | { |
---|
| 123 | va_list args; |
---|
| 124 | va_start(args, fmt); |
---|
| 125 | std::string msg = fmtToString( fmt, args ); |
---|
| 126 | va_end(args); |
---|
| 127 | std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl; |
---|
| 128 | } |
---|
| 129 | break; |
---|
| 130 | case Severity::Error : |
---|
| 131 | { |
---|
| 132 | va_list args; |
---|
| 133 | va_start(args, fmt); |
---|
| 134 | std::string msg = fmtToString( fmt, args ); |
---|
| 135 | va_end(args); |
---|
| 136 | SemanticError(location, msg); |
---|
| 137 | } |
---|
| 138 | break; |
---|
| 139 | case Severity::Critical : |
---|
| 140 | assertf(false, "Critical errors not implemented yet"); |
---|
| 141 | break; |
---|
| 142 | } |
---|
[a16764a6] | 143 | } |
---|
| 144 | |
---|
| 145 | //----------------------------------------------------------------------------- |
---|
| 146 | // Helpers |
---|
| 147 | namespace ErrorHelpers { |
---|
| 148 | const std::string & error_str() { |
---|
| 149 | static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: "; |
---|
| 150 | return str; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | const std::string & warning_str() { |
---|
| 154 | static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: "; |
---|
| 155 | return str; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | const std::string & bold_ttycode() { |
---|
| 159 | static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : ""; |
---|
| 160 | return str; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | const std::string & reset_font_ttycode() { |
---|
| 164 | static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : ""; |
---|
| 165 | return str; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | std::string make_bold( const std::string & str ) { |
---|
| 169 | return bold_ttycode() + str + reset_font_ttycode(); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | std::ostream & operator<<(std::ostream & os, bold) { |
---|
| 173 | os << bold_ttycode(); |
---|
| 174 | return os; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | std::ostream & operator<<(std::ostream & os, reset_font) { |
---|
| 178 | os << reset_font_ttycode(); |
---|
| 179 | return os; |
---|
| 180 | } |
---|
[294647b] | 181 | } |
---|
| 182 | |
---|
[51587aa] | 183 | // Local Variables: // |
---|
| 184 | // tab-width: 4 // |
---|
| 185 | // mode: c++ // |
---|
| 186 | // compile-command: "make install" // |
---|
| 187 | // End: // |
---|