[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 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[01aeade] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[9ed4f94] | 12 | // Last Modified On : Tue Aug 29 18:17:35 2017
|
---|
| 13 | // Update Count : 3
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[9ed4f94] | 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
|
---|
[51b73452] | 21 |
|
---|
[9ed4f94] | 22 | #include "Common/utility.h" // for to_string, CodeLocation (ptr only)
|
---|
[51b73452] | 23 | #include "SemanticError.h"
|
---|
| 24 |
|
---|
[a16764a6] | 25 | SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
|
---|
[d55d7a6] | 26 | append( location, error );
|
---|
[51b73452] | 27 | }
|
---|
| 28 |
|
---|
[a16764a6] | 29 | void SemanticErrorException::append( SemanticErrorException &other ) {
|
---|
[138e29e] | 30 | errors.splice( errors.end(), other.errors );
|
---|
[3906301] | 31 | }
|
---|
| 32 |
|
---|
[a16764a6] | 33 | void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
|
---|
[d55d7a6] | 34 | errors.emplace_back( location, msg );
|
---|
[51b73452] | 35 | }
|
---|
| 36 |
|
---|
[a16764a6] | 37 | bool SemanticErrorException::isEmpty() const {
|
---|
[01aeade] | 38 | return errors.empty();
|
---|
[51b73452] | 39 | }
|
---|
| 40 |
|
---|
[a16764a6] | 41 | void SemanticErrorException::print() {
|
---|
[138e29e] | 42 | using std::to_string;
|
---|
[9ed4f94] | 43 | for( auto err : errors ) {
|
---|
[a16764a6] | 44 | std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl;
|
---|
[138e29e] | 45 | }
|
---|
[51b73452] | 46 | }
|
---|
[01aeade] | 47 |
|
---|
[a16764a6] | 48 | void SemanticError( CodeLocation location, std::string error ) {
|
---|
| 49 | throw SemanticErrorException(location, error);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void SemanticWarningImpl( CodeLocation location, std::string msg ) {
|
---|
| 53 | std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | //-----------------------------------------------------------------------------
|
---|
| 57 | // Helpers
|
---|
| 58 | namespace ErrorHelpers {
|
---|
| 59 | const std::string & error_str() {
|
---|
| 60 | static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
|
---|
| 61 | return str;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | const std::string & warning_str() {
|
---|
| 65 | static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
|
---|
| 66 | return str;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | const std::string & bold_ttycode() {
|
---|
| 70 | static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
|
---|
| 71 | return str;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | const std::string & reset_font_ttycode() {
|
---|
| 75 | static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
|
---|
| 76 | return str;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | std::string make_bold( const std::string & str ) {
|
---|
| 80 | return bold_ttycode() + str + reset_font_ttycode();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | std::ostream & operator<<(std::ostream & os, bold) {
|
---|
| 84 | os << bold_ttycode();
|
---|
| 85 | return os;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | std::ostream & operator<<(std::ostream & os, reset_font) {
|
---|
| 89 | os << reset_font_ttycode();
|
---|
| 90 | return os;
|
---|
| 91 | }
|
---|
[294647b] | 92 | }
|
---|
| 93 |
|
---|
[51587aa] | 94 | // Local Variables: //
|
---|
| 95 | // tab-width: 4 //
|
---|
| 96 | // mode: c++ //
|
---|
| 97 | // compile-command: "make install" //
|
---|
| 98 | // End: //
|
---|