source: src/Common/SemanticError.cc @ 5527759

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 5527759 was 6040e67d, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Added warning data to warnings

  • Property mode set to 100644
File size: 3.8 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//
[51b7345]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
[51b7345]22
[9ed4f94]23#include "Common/utility.h"                                                             // for to_string, CodeLocation (ptr only)
[51b7345]24#include "SemanticError.h"
25
[a16764a6]26SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) {
[d55d7a6]27        append( location, error );
[51b7345]28}
29
[a16764a6]30void SemanticErrorException::append( SemanticErrorException &other ) {
[138e29e]31        errors.splice( errors.end(), other.errors );
[3906301]32}
33
[a16764a6]34void SemanticErrorException::append( CodeLocation location, const std::string & msg ) {
[d55d7a6]35        errors.emplace_back( location, msg );
[51b7345]36}
37
[a16764a6]38bool SemanticErrorException::isEmpty() const {
[01aeade]39        return errors.empty();
[51b7345]40}
41
[a16764a6]42void 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        }
[51b7345]47}
[01aeade]48
[a16764a6]49void SemanticError( CodeLocation location, std::string error ) {
50        throw SemanticErrorException(location, error);
51}
52
[2103a51]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
[6040e67d]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        }
[a16764a6]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        }
[294647b]135}
136
[51587aa]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.