source: src/Common/SemanticError.cc@ 520145b

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 520145b was 244b934, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add missing cstdarg include

  • Property mode set to 100644
File size: 3.3 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, const char * const fmt, ... ) {
71 va_list args;
72 va_start(args, fmt);
73 std::string msg = fmtToString( fmt, args );
74 va_end(args);
75 std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl;
76}
77
78//-----------------------------------------------------------------------------
79// Helpers
80namespace 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 }
114}
115
116// Local Variables: //
117// tab-width: 4 //
118// mode: c++ //
119// compile-command: "make install" //
120// End: //
Note: See TracBrowser for help on using the repository browser.