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.hpp -- |
---|
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 2 15:14:13 2024 |
---|
13 | // Update Count : 75 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "ErrorObjects.hpp" |
---|
19 | #include "AST/Node.hpp" |
---|
20 | #include "AST/ParseNode.hpp" |
---|
21 | #include <cstring> |
---|
22 | |
---|
23 | //----------------------------------------------------------------------------- |
---|
24 | // Errors |
---|
25 | |
---|
26 | extern 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 | |
---|
46 | enum class Severity { |
---|
47 | Suppress, |
---|
48 | Warn, |
---|
49 | Error, |
---|
50 | Critical |
---|
51 | }; |
---|
52 | |
---|
53 | struct WarningData { |
---|
54 | const char * const name; |
---|
55 | const Severity default_severity; |
---|
56 | const char * const message; |
---|
57 | }; |
---|
58 | |
---|
59 | constexpr 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 | {"aggregate-forward-decl" , Severity::Warn, "forward declaration of nested aggregate: %s" }, |
---|
63 | {"superfluous-decl" , Severity::Warn, "declaration does not allocate storage: %s" }, |
---|
64 | {"superfluous-else" , Severity::Warn, "else clause never executed for empty loop conditional" }, |
---|
65 | {"gcc-attributes" , Severity::Warn, "invalid attribute: %s" }, |
---|
66 | {"c++-like-copy" , Severity::Warn, "Constructor from reference is not a valid copy constructor" }, |
---|
67 | {"depreciated-trait-syntax" , Severity::Warn, "trait type-parameters are now specified using the forall clause" }, |
---|
68 | }; |
---|
69 | |
---|
70 | enum class Warning { |
---|
71 | SelfAssignment, |
---|
72 | RvalueToReferenceConversion, |
---|
73 | AggrForwardDecl, |
---|
74 | SuperfluousDecl, |
---|
75 | SuperfluousElse, |
---|
76 | GccAttributes, |
---|
77 | CppCopy, |
---|
78 | DeprecTraitSyntax, |
---|
79 | NUMBER_OF_WARNINGS, // MUST be last warning |
---|
80 | }; |
---|
81 | |
---|
82 | static_assert( |
---|
83 | (sizeof(WarningFormats) / sizeof(WarningFormats[0])) == ((unsigned long)Warning::NUMBER_OF_WARNINGS), |
---|
84 | "Each warning format should have a corresponding warning enum value" |
---|
85 | ); |
---|
86 | |
---|
87 | void SemanticWarning( CodeLocation loc, Warning warn, ... ); |
---|
88 | |
---|
89 | void SemanticWarning_SuppressAll(); |
---|
90 | void SemanticWarning_EnableAll(); |
---|
91 | void SemanticWarning_WarningAsError(); |
---|
92 | void SemanticWarning_Set(const char * const name, Severity s); |
---|
93 | |
---|
94 | // SKULLDUGGERY: cfa.cc is built before SemanticError.cc but needs this routine. |
---|
95 | static inline bool SemanticWarning_Exist(const char * const name) { |
---|
96 | for ( const auto & w : WarningFormats ) { |
---|
97 | if ( std::strcmp( name, w.name ) == 0 ) return true; |
---|
98 | } |
---|
99 | return false; |
---|
100 | } |
---|
101 | |
---|
102 | //----------------------------------------------------------------------------- |
---|
103 | // Helpers |
---|
104 | namespace ErrorHelpers { |
---|
105 | enum class Colors { |
---|
106 | Never = false, |
---|
107 | Always = true, |
---|
108 | Auto, |
---|
109 | }; |
---|
110 | |
---|
111 | extern Colors colors; |
---|
112 | |
---|
113 | const std::string & error_str(); |
---|
114 | const std::string & warning_str(); |
---|
115 | const std::string & bold_ttycode(); |
---|
116 | const std::string & reset_font_ttycode(); |
---|
117 | |
---|
118 | std::string make_bold( const std::string & str ); |
---|
119 | |
---|
120 | struct bold {}; |
---|
121 | std::ostream & operator<<(std::ostream & os, bold); |
---|
122 | |
---|
123 | struct reset_font {}; |
---|
124 | std::ostream & operator<<(std::ostream & os, reset_font); |
---|
125 | } |
---|
126 | |
---|
127 | // Local Variables: // |
---|
128 | // tab-width: 4 // |
---|
129 | // mode: c++ // |
---|
130 | // compile-command: "make install" // |
---|
131 | // End: // |
---|