| [51587aa] | 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 | // | 
|---|
| [3906301] | 7 | // SemanticError.h -- | 
|---|
| [51587aa] | 8 | // | 
|---|
| [af39199d] | 9 | // Author           : Thierry Delisle | 
|---|
| [51587aa] | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [9ed4f94] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [679e644] | 12 | // Last Modified On : Thu Jul 19 10:09:17 2018 | 
|---|
|  | 13 | // Update Count     : 31 | 
|---|
| [51587aa] | 14 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
| [6b0b624] | 16 | #pragma once | 
|---|
| [51b73452] | 17 |  | 
|---|
| [a16764a6] | 18 | #include "ErrorObjects.h" | 
|---|
| [77bfc80] | 19 | #include "AST/Node.hpp" | 
|---|
| [af39199d] | 20 | #include <cstring> | 
|---|
| [294647b] | 21 |  | 
|---|
| [d55d7a6] | 22 | //----------------------------------------------------------------------------- | 
|---|
|  | 23 | // Errors | 
|---|
| [138e29e] | 24 |  | 
|---|
| [4358c1e] | 25 | extern bool SemanticErrorThrow; | 
|---|
|  | 26 |  | 
|---|
| [a16764a6] | 27 | __attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error ); | 
|---|
| [51b73452] | 28 |  | 
|---|
|  | 29 | template< typename T > | 
|---|
| [a16764a6] | 30 | __attribute__((noreturn)) static inline void SemanticError( const T * obj, const std::string & error ) { | 
|---|
|  | 31 | SemanticError( obj->location, toString( error, obj ) ); | 
|---|
|  | 32 | } | 
|---|
| [d55d7a6] | 33 |  | 
|---|
|  | 34 | template< typename T > | 
|---|
| [a16764a6] | 35 | __attribute__((noreturn)) static inline void SemanticError( CodeLocation location, const T * obj, const std::string & error ) { | 
|---|
|  | 36 | SemanticError( location, toString( error, obj ) ); | 
|---|
|  | 37 | } | 
|---|
| [d55d7a6] | 38 |  | 
|---|
|  | 39 | //----------------------------------------------------------------------------- | 
|---|
|  | 40 | // Warnings | 
|---|
|  | 41 |  | 
|---|
| [6040e67d] | 42 | enum class Severity { | 
|---|
|  | 43 | Suppress, | 
|---|
|  | 44 | Warn, | 
|---|
|  | 45 | Error, | 
|---|
|  | 46 | Critical | 
|---|
|  | 47 | }; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | struct WarningData { | 
|---|
|  | 50 | const char * const name; | 
|---|
|  | 51 | const char * const message; | 
|---|
| [68e9ace] | 52 | const Severity default_severity; | 
|---|
| [6040e67d] | 53 | }; | 
|---|
|  | 54 |  | 
|---|
| [44bca7f] | 55 | constexpr WarningData WarningFormats[] = { | 
|---|
| [68e9ace] | 56 | {"self-assign"            , "self assignment of expression: %s"            , Severity::Warn}, | 
|---|
|  | 57 | {"reference-conversion"   , "rvalue to reference conversion of rvalue: %s" , Severity::Warn}, | 
|---|
| [c28afead] | 58 | {"qualifiers-zero_t-one_t", "questionable use of type qualifier %s with %s", Severity::Warn}, | 
|---|
| [2e02851] | 59 | {"aggregate-forward-decl" , "forward declaration of nested aggregate: %s"  , Severity::Warn}, | 
|---|
| [679e644] | 60 | {"superfluous-decl"       , "declaration does not allocate storage: %s"    , Severity::Warn}, | 
|---|
| [fd2debf] | 61 | {"gcc-attributes"         , "invalid attribute: %s"                        , Severity::Warn}, | 
|---|
| [a16764a6] | 62 | }; | 
|---|
| [d55d7a6] | 63 |  | 
|---|
| [a16764a6] | 64 | enum class Warning { | 
|---|
| [babeeda] | 65 | SelfAssignment, | 
|---|
| [ec6c040] | 66 | RvalueToReferenceConversion, | 
|---|
| [9dc31c10] | 67 | BadQualifiersZeroOne, | 
|---|
| [2e02851] | 68 | AggrForwardDecl, | 
|---|
| [679e644] | 69 | SuperfluousDecl, | 
|---|
| [fd2debf] | 70 | GccAttributes, | 
|---|
| [679e644] | 71 | NUMBER_OF_WARNINGS, // This MUST be the last warning | 
|---|
| [d55d7a6] | 72 | }; | 
|---|
|  | 73 |  | 
|---|
| [a16764a6] | 74 | static_assert( | 
|---|
|  | 75 | (sizeof(WarningFormats) / sizeof(WarningFormats[0])) == ((unsigned long)Warning::NUMBER_OF_WARNINGS), | 
|---|
|  | 76 | "Each warning format should have a corresponding warning enum value" | 
|---|
|  | 77 | ); | 
|---|
|  | 78 |  | 
|---|
| [6040e67d] | 79 | #define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[(int)id].message, __VA_ARGS__) | 
|---|
| [a16764a6] | 80 |  | 
|---|
|  | 81 | void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4))); | 
|---|
| [d55d7a6] | 82 |  | 
|---|
| [68e9ace] | 83 | void SemanticWarning_SuppressAll   (); | 
|---|
|  | 84 | void SemanticWarning_EnableAll     (); | 
|---|
|  | 85 | void SemanticWarning_WarningAsError(); | 
|---|
|  | 86 | void SemanticWarning_Set           (const char * const name, Severity s); | 
|---|
| [d55d7a6] | 87 |  | 
|---|
| [af39199d] | 88 | // SKULLDUGGERY: cfa.cc is built before SemanticError.cc but needs this routine. | 
|---|
|  | 89 | static inline bool SemanticWarning_Exist(const char * const name) { | 
|---|
|  | 90 | for ( const auto & w : WarningFormats ) { | 
|---|
|  | 91 | if ( std::strcmp( name, w.name ) == 0 ) return true; | 
|---|
|  | 92 | } | 
|---|
|  | 93 | return false; | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
| [d55d7a6] | 96 | //----------------------------------------------------------------------------- | 
|---|
|  | 97 | // Helpers | 
|---|
| [a16764a6] | 98 | namespace ErrorHelpers { | 
|---|
|  | 99 | const std::string & error_str(); | 
|---|
|  | 100 | const std::string & warning_str(); | 
|---|
|  | 101 | const std::string & bold_ttycode(); | 
|---|
|  | 102 | const std::string & reset_font_ttycode(); | 
|---|
| [d55d7a6] | 103 |  | 
|---|
| [a16764a6] | 104 | std::string make_bold( const std::string & str ); | 
|---|
| [d55d7a6] | 105 |  | 
|---|
| [a16764a6] | 106 | struct bold {}; | 
|---|
|  | 107 | std::ostream & operator<<(std::ostream & os, bold); | 
|---|
| [d55d7a6] | 108 |  | 
|---|
| [a16764a6] | 109 | struct reset_font {}; | 
|---|
|  | 110 | std::ostream & operator<<(std::ostream & os, reset_font); | 
|---|
| [d55d7a6] | 111 | } | 
|---|
|  | 112 |  | 
|---|
| [51587aa] | 113 | // Local Variables: // | 
|---|
|  | 114 | // tab-width: 4 // | 
|---|
|  | 115 | // mode: c++ // | 
|---|
|  | 116 | // compile-command: "make install" // | 
|---|
|  | 117 | // End: // | 
|---|