source: src/Common/SemanticError.cc@ b91e8c1

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 with_gc
Last change on this file since b91e8c1 was 6040e67d, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Added warning data to warnings

  • Property mode set to 100644
File size: 3.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 <cstdarg>
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
22
23#include "Common/utility.h" // for to_string, CodeLocation (ptr only)
24#include "SemanticError.h"
25
26SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
27 append( location, error );
28}
29
30void SemanticErrorException::append( SemanticErrorException &other ) {
31 errors.splice( errors.end(), other.errors );
32}
33
34void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
35 errors.emplace_back( location, msg );
36}
37
38bool SemanticErrorException::isEmpty() const {
39 return errors.empty();
40}
41
42void SemanticErrorException::print() {
43 using std::to_string;
44 for( auto err : errors ) {
45 std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl;
46 }
47}
48
49void SemanticError( CodeLocation location, std::string error ) {
50 throw SemanticErrorException(location, error);
51}
52
53namespace {
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
70void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) {
71 Severity severity = WarningFormats[(int)warning].severity;
72 switch(severity) {
73 case Severity::Suppress :
74 break;
75 case Severity::Warn :
76 {
77 va_list args;
78 va_start(args, fmt);
79 std::string msg = fmtToString( fmt, args );
80 va_end(args);
81 std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl;
82 }
83 break;
84 case Severity::Error :
85 {
86 va_list args;
87 va_start(args, fmt);
88 std::string msg = fmtToString( fmt, args );
89 va_end(args);
90 SemanticError(location, msg);
91 }
92 break;
93 case Severity::Critical :
94 assertf(false, "Critical errors not implemented yet");
95 break;
96 }
97}
98
99//-----------------------------------------------------------------------------
100// Helpers
101namespace ErrorHelpers {
102 const std::string & error_str() {
103 static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: ";
104 return str;
105 }
106
107 const std::string & warning_str() {
108 static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: ";
109 return str;
110 }
111
112 const std::string & bold_ttycode() {
113 static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : "";
114 return str;
115 }
116
117 const std::string & reset_font_ttycode() {
118 static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : "";
119 return str;
120 }
121
122 std::string make_bold( const std::string & str ) {
123 return bold_ttycode() + str + reset_font_ttycode();
124 }
125
126 std::ostream & operator<<(std::ostream & os, bold) {
127 os << bold_ttycode();
128 return os;
129 }
130
131 std::ostream & operator<<(std::ostream & os, reset_font) {
132 os << reset_font_ttycode();
133 return os;
134 }
135}
136
137// Local Variables: //
138// tab-width: 4 //
139// mode: c++ //
140// compile-command: "make install" //
141// End: //
Note: See TracBrowser for help on using the repository browser.