| 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 Jul 19 10:09:17 2018 | 
|---|
| 13 | // Update Count     : 31 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include "ErrorObjects.h" | 
|---|
| 19 | #include "AST/Node.hpp" | 
|---|
| 20 | #include <cstring> | 
|---|
| 21 |  | 
|---|
| 22 | //----------------------------------------------------------------------------- | 
|---|
| 23 | // Errors | 
|---|
| 24 |  | 
|---|
| 25 | extern bool SemanticErrorThrow; | 
|---|
| 26 |  | 
|---|
| 27 | __attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error ); | 
|---|
| 28 |  | 
|---|
| 29 | template< 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 |  | 
|---|
| 34 | template< 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 |  | 
|---|
| 42 | enum class Severity { | 
|---|
| 43 | Suppress, | 
|---|
| 44 | Warn, | 
|---|
| 45 | Error, | 
|---|
| 46 | Critical | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 | struct WarningData { | 
|---|
| 50 | const char * const name; | 
|---|
| 51 | const Severity default_severity; | 
|---|
| 52 | const char * const message; | 
|---|
| 53 | }; | 
|---|
| 54 |  | 
|---|
| 55 | constexpr 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 | {"gcc-attributes"         , Severity::Warn    , "invalid attribute: %s"                                      }, | 
|---|
| 62 | {"c++-like-copy"          , Severity::Warn    , "Constructor from reference is not a valid copy constructor" }, | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 | enum class Warning { | 
|---|
| 66 | SelfAssignment, | 
|---|
| 67 | RvalueToReferenceConversion, | 
|---|
| 68 | BadQualifiersZeroOne, | 
|---|
| 69 | AggrForwardDecl, | 
|---|
| 70 | SuperfluousDecl, | 
|---|
| 71 | GccAttributes, | 
|---|
| 72 | CppCopy, | 
|---|
| 73 | NUMBER_OF_WARNINGS, // This MUST be the last warning | 
|---|
| 74 | }; | 
|---|
| 75 |  | 
|---|
| 76 | static_assert( | 
|---|
| 77 | (sizeof(WarningFormats) / sizeof(WarningFormats[0])) == ((unsigned long)Warning::NUMBER_OF_WARNINGS), | 
|---|
| 78 | "Each warning format should have a corresponding warning enum value" | 
|---|
| 79 | ); | 
|---|
| 80 |  | 
|---|
| 81 | #define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[(int)id].message, __VA_ARGS__) | 
|---|
| 82 |  | 
|---|
| 83 | void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4))); | 
|---|
| 84 |  | 
|---|
| 85 | void SemanticWarning_SuppressAll   (); | 
|---|
| 86 | void SemanticWarning_EnableAll     (); | 
|---|
| 87 | void SemanticWarning_WarningAsError(); | 
|---|
| 88 | void SemanticWarning_Set           (const char * const name, Severity s); | 
|---|
| 89 |  | 
|---|
| 90 | // SKULLDUGGERY: cfa.cc is built before SemanticError.cc but needs this routine. | 
|---|
| 91 | static inline bool SemanticWarning_Exist(const char * const name) { | 
|---|
| 92 | for ( const auto & w : WarningFormats ) { | 
|---|
| 93 | if ( std::strcmp( name, w.name ) == 0 ) return true; | 
|---|
| 94 | } | 
|---|
| 95 | return false; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | //----------------------------------------------------------------------------- | 
|---|
| 99 | // Helpers | 
|---|
| 100 | namespace ErrorHelpers { | 
|---|
| 101 | enum class Colors { | 
|---|
| 102 | Never = false, | 
|---|
| 103 | Always = true, | 
|---|
| 104 | Auto, | 
|---|
| 105 | }; | 
|---|
| 106 |  | 
|---|
| 107 | extern Colors colors; | 
|---|
| 108 |  | 
|---|
| 109 | const std::string & error_str(); | 
|---|
| 110 | const std::string & warning_str(); | 
|---|
| 111 | const std::string & bold_ttycode(); | 
|---|
| 112 | const std::string & reset_font_ttycode(); | 
|---|
| 113 |  | 
|---|
| 114 | std::string make_bold( const std::string & str ); | 
|---|
| 115 |  | 
|---|
| 116 | struct bold {}; | 
|---|
| 117 | std::ostream & operator<<(std::ostream & os, bold); | 
|---|
| 118 |  | 
|---|
| 119 | struct reset_font {}; | 
|---|
| 120 | std::ostream & operator<<(std::ostream & os, reset_font); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | // Local Variables: // | 
|---|
| 124 | // tab-width: 4 // | 
|---|
| 125 | // mode: c++ // | 
|---|
| 126 | // compile-command: "make install" // | 
|---|
| 127 | // End: // | 
|---|