source: src/Common/SemanticError.cc@ 1f37ed02

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 1f37ed02 was a16764a6, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Changed warning system to prepare for toggling warnings

  • Property mode set to 100644
File size: 2.8 KB
Line 
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
25SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
26 append( location, error );
27}
28
29void SemanticErrorException::append( SemanticErrorException &other ) {
30 errors.splice( errors.end(), other.errors );
31}
32
33void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
34 errors.emplace_back( location, msg );
35}
36
37bool SemanticErrorException::isEmpty() const {
38 return errors.empty();
39}
40
41void 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
48void SemanticError( CodeLocation location, std::string error ) {
49 throw SemanticErrorException(location, error);
50}
51
52void 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
58namespace 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 }
92}
93
94// Local Variables: //
95// tab-width: 4 //
96// mode: c++ //
97// compile-command: "make install" //
98// End: //
Note: See TracBrowser for help on using the repository browser.