| 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           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue Aug 29 18:17:35 2017 | 
|---|
| 13 | // Update Count     : 3 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <cstdio>                                                                               // for fileno, stderr | 
|---|
| 17 | #include <unistd.h>                                                                             // for isatty | 
|---|
| 18 | #include <iostream>                                                                             // for basic_ostream, operator<<, ostream | 
|---|
| 19 | #include <list>                                                                                 // for list, _List_iterator | 
|---|
| 20 | #include <string>                                                                               // for string, operator<<, operator+, to_string | 
|---|
| 21 |  | 
|---|
| 22 | #include "Common/utility.h"                                                             // for to_string, CodeLocation (ptr only) | 
|---|
| 23 | #include "SemanticError.h" | 
|---|
| 24 |  | 
|---|
| 25 | SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) { | 
|---|
| 26 | append( location, error ); | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | void SemanticErrorException::append( SemanticErrorException &other ) { | 
|---|
| 30 | errors.splice( errors.end(), other.errors ); | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | void SemanticErrorException::append( CodeLocation location, const std::string & msg ) { | 
|---|
| 34 | errors.emplace_back( location, msg ); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | bool SemanticErrorException::isEmpty() const { | 
|---|
| 38 | return errors.empty(); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | void SemanticErrorException::print() { | 
|---|
| 42 | using std::to_string; | 
|---|
| 43 | for( auto err : errors ) { | 
|---|
| 44 | std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl; | 
|---|
| 45 | } | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | void SemanticError( CodeLocation location, std::string error ) { | 
|---|
| 49 | throw SemanticErrorException(location, error); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | namespace { | 
|---|
| 53 | // convert format string and arguments into a single string | 
|---|
| 54 | std::string fmtToString(const char * fmt, va_list ap) { | 
|---|
| 55 | int size = 128; | 
|---|
| 56 | while ( true ) { | 
|---|
| 57 | char buf[size]; | 
|---|
| 58 | va_list args; | 
|---|
| 59 | va_copy( args, ap ); | 
|---|
| 60 | int n = vsnprintf(&buf[0], size, fmt, args); | 
|---|
| 61 | va_end( args ); | 
|---|
| 62 | if ( n < size && n >= 0 ) return buf; | 
|---|
| 63 | size *= 2; | 
|---|
| 64 | } | 
|---|
| 65 | assert( false ); | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | void SemanticWarningImpl( CodeLocation location, Warning, const char * const fmt, ... ) { | 
|---|
| 70 | va_list args; | 
|---|
| 71 | va_start(args, fmt); | 
|---|
| 72 | std::string msg = fmtToString( fmt, args ); | 
|---|
| 73 | va_end(args); | 
|---|
| 74 | std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | //----------------------------------------------------------------------------- | 
|---|
| 78 | // Helpers | 
|---|
| 79 | namespace ErrorHelpers { | 
|---|
| 80 | const std::string & error_str() { | 
|---|
| 81 | static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: "; | 
|---|
| 82 | return str; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | const std::string & warning_str() { | 
|---|
| 86 | static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: "; | 
|---|
| 87 | return str; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | const std::string & bold_ttycode() { | 
|---|
| 91 | static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : ""; | 
|---|
| 92 | return str; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | const std::string & reset_font_ttycode() { | 
|---|
| 96 | static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : ""; | 
|---|
| 97 | return str; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | std::string make_bold( const std::string & str ) { | 
|---|
| 101 | return bold_ttycode() + str + reset_font_ttycode(); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | std::ostream & operator<<(std::ostream & os, bold) { | 
|---|
| 105 | os << bold_ttycode(); | 
|---|
| 106 | return os; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | std::ostream & operator<<(std::ostream & os, reset_font) { | 
|---|
| 110 | os << reset_font_ttycode(); | 
|---|
| 111 | return os; | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | // Local Variables: // | 
|---|
| 116 | // tab-width: 4 // | 
|---|
| 117 | // mode: c++ // | 
|---|
| 118 | // compile-command: "make install" // | 
|---|
| 119 | // End: // | 
|---|