source: src/Common/SemanticError.h @ 3e49c477

Last change on this file since 3e49c477 was b1f2007, checked in by Peter A. Buhr <pabuhr@…>, 10 months ago

first attempt at simplifying SemanticError? and its usage

  • Property mode set to 100644
File size: 4.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.h --
8//
9// Author           : Thierry Delisle
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Dec 11 21:54:22 2023
13// Update Count     : 54
14//
15
16#pragma once
17
18#include "ErrorObjects.h"
19#include "AST/Node.hpp"
20#include "AST/ParseNode.hpp"
21#include <cstring>
22
23//-----------------------------------------------------------------------------
24// Errors
25
26extern bool SemanticErrorThrow;
27
28__attribute__((noreturn, format(printf, 2, 3))) void SemanticError( CodeLocation location, const char fmt[], ... );
29
30__attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error );
31
32__attribute__((noreturn)) static inline void SemanticError( const ast::ParseNode * obj, const std::string & error ) {
33        SemanticError( obj->location, toString( error, obj ) );
34}
35
36__attribute__((noreturn)) static inline void SemanticError( CodeLocation location, const ast::Node * obj, const std::string & error ) {
37        SemanticError( location, toString( error, obj ) );
38}
39
40//-----------------------------------------------------------------------------
41// Warnings
42
43enum class Severity {
44        Suppress,
45        Warn,
46        Error,
47        Critical
48};
49
50struct WarningData {
51        const char * const name;
52        const Severity default_severity;
53        const char * const message;
54};
55
56constexpr WarningData WarningFormats[] = {
57        {"self-assign"              , Severity::Warn, "self assignment of expression: %s"                          },
58        {"reference-conversion"     , Severity::Warn, "rvalue to reference conversion of rvalue: %s"               },
59        {"qualifiers-zero_t-one_t"  , Severity::Warn, "questionable use of type qualifier(s) with %s"              },
60        {"aggregate-forward-decl"   , Severity::Warn, "forward declaration of nested aggregate: %s"                },
61        {"superfluous-decl"         , Severity::Warn, "declaration does not allocate storage: %s"                  },
62        {"superfluous-else"         , Severity::Warn, "else clause never executed for empty loop conditional"      },
63        {"gcc-attributes"           , Severity::Warn, "invalid attribute: %s"                                      },
64        {"c++-like-copy"            , Severity::Warn, "Constructor from reference is not a valid copy constructor" },
65        {"depreciated-trait-syntax" , Severity::Warn, "trait type-parameters are now specified using the forall clause" },
66};
67
68enum class Warning {
69        SelfAssignment,
70        RvalueToReferenceConversion,
71        BadQualifiersZeroOne,
72        AggrForwardDecl,
73        SuperfluousDecl,
74        SuperfluousElse,
75        GccAttributes,
76        CppCopy,
77        DeprecTraitSyntax,
78        NUMBER_OF_WARNINGS, // MUST be the last warning
79};
80
81static_assert(
82        (sizeof(WarningFormats) / sizeof(WarningFormats[0])) == ((unsigned long)Warning::NUMBER_OF_WARNINGS),
83        "Each warning format should have a corresponding warning enum value"
84);
85
86#define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[(int)id].message, ##__VA_ARGS__)
87
88void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4)));
89
90void SemanticWarning_SuppressAll   ();
91void SemanticWarning_EnableAll     ();
92void SemanticWarning_WarningAsError();
93void SemanticWarning_Set           (const char * const name, Severity s);
94
95// SKULLDUGGERY: cfa.cc is built before SemanticError.cc but needs this routine.
96static inline bool SemanticWarning_Exist(const char * const name) {
97        for ( const auto & w : WarningFormats ) {
98                if ( std::strcmp( name, w.name ) == 0 ) return true;
99        }
100        return false;
101}
102
103//-----------------------------------------------------------------------------
104// Helpers
105namespace ErrorHelpers {
106        enum class Colors {
107                Never = false,
108                Always = true,
109                Auto,
110        };
111
112        extern Colors colors;
113
114        const std::string & error_str();
115        const std::string & warning_str();
116        const std::string & bold_ttycode();
117        const std::string & reset_font_ttycode();
118
119        std::string make_bold( const std::string & str );
120
121        struct bold {};
122        std::ostream & operator<<(std::ostream & os, bold);
123
124        struct reset_font {};
125        std::ostream & operator<<(std::ostream & os, reset_font);
126}
127
128// Local Variables: //
129// tab-width: 4 //
130// mode: c++ //
131// compile-command: "make install" //
132// End: //
Note: See TracBrowser for help on using the repository browser.