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