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