source: src/Common/SemanticError.h @ e8b3717

Last change on this file since e8b3717 was 610354a, checked in by Peter A. Buhr <pabuhr@…>, 11 months ago

first attempt at simplifying SemanticWarning?, inline SemanticError? routine

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