source: src/Common/SemanticError.h @ 598dc68

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 598dc68 was b6ae4fb, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

add SuperfluousElse? warning, allow empty vararg to macro SemanticWarning?

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