source: src/Common/SemanticError.cc@ 2ec65ad

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 2ec65ad was 2103a51, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Fix SemanticWarningImpl

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[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]25SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
[d55d7a6]26 append( location, error );
[51b73452]27}
28
[a16764a6]29void SemanticErrorException::append( SemanticErrorException &other ) {
[138e29e]30 errors.splice( errors.end(), other.errors );
[3906301]31}
32
[a16764a6]33void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
[d55d7a6]34 errors.emplace_back( location, msg );
[51b73452]35}
36
[a16764a6]37bool SemanticErrorException::isEmpty() const {
[01aeade]38 return errors.empty();
[51b73452]39}
40
[a16764a6]41void 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]48void SemanticError( CodeLocation location, std::string error ) {
49 throw SemanticErrorException(location, error);
50}
51
[2103a51]52namespace {
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
69void 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);
[a16764a6]74 std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl;
75}
76
77//-----------------------------------------------------------------------------
78// Helpers
79namespace 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 }
[294647b]113}
114
[51587aa]115// Local Variables: //
116// tab-width: 4 //
117// mode: c++ //
118// compile-command: "make install" //
119// End: //
Note: See TracBrowser for help on using the repository browser.