diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-06-14 07:33:30 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-06-14 07:33:30 +0000 |
commit | 5e9f35c7cb61aea46f56d46c77cbcf47c0cf28ba (patch) | |
tree | fadba68028102e812ff9c549c9398350fd81aaca /include/clang/Basic | |
parent | 625b5864b5ba0552046d69830dacb951f02159ad (diff) |
Update LLVM.
Implement support for C++ Substitution Failure Is Not An Error
(SFINAE), which says that errors that occur during template argument
deduction do *not* produce diagnostics and do not necessarily make a
program ill-formed. Instead, template argument deduction silently
fails. This is currently implemented for template argument deduction
during matching of class template partial specializations, although
the mechanism will also apply to template argument deduction for
function templates. The scheme is simple:
- If we are in a template argument deduction context, any diagnostic
that is considered a SFINAE error (or warning) will be
suppressed. The error will be propagated up the call stack via the
normal means.
- By default, all warnings and errors are SFINAE errors. Add the
NoSFINAE class to a diagnostic in the .td file to make it a hard
error (e.g., for access-control violations).
Note that, to make this fully work, every place in Sema that emits an
error *and then immediately recovers* will need to check
Sema::isSFINAEContext() to determine whether it must immediately
return an error rather than recovering.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73332 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/clang/Basic')
-rw-r--r-- | include/clang/Basic/Diagnostic.h | 55 | ||||
-rw-r--r-- | include/clang/Basic/Diagnostic.td | 3 | ||||
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 13 |
3 files changed, 55 insertions, 16 deletions
diff --git a/include/clang/Basic/Diagnostic.h b/include/clang/Basic/Diagnostic.h index 22e7fb3f85..207710bdff 100644 --- a/include/clang/Basic/Diagnostic.h +++ b/include/clang/Basic/Diagnostic.h @@ -50,7 +50,7 @@ namespace clang { // Get typedefs for common diagnostics. enum { -#define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP) ENUM, +#define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP,SFINAE) ENUM, #include "clang/Basic/DiagnosticCommonKinds.inc" NUM_BUILTIN_COMMON_DIAGNOSTICS #undef DIAG @@ -313,6 +313,16 @@ public: /// the diagnostic, this returns null. static const char *getWarningOptionForDiag(unsigned DiagID); + /// \brief Determines whether the given built-in diagnostic ID is + /// for an error that is suppressed if it occurs during C++ template + /// argument deduction. + /// + /// When an error is suppressed due to SFINAE, the template argument + /// deduction fails but no diagnostic is emitted. Certain classes of + /// errors, such as those errors that involve C++ access control, + /// are not SFINAE errors. + static bool isBuiltinSFINAEDiag(unsigned DiagID); + /// getDiagnosticLevel - Based on the way the client configured the Diagnostic /// object, classify the specified diagnostic ID into a Level, consumable by /// the DiagnosticClient. @@ -409,7 +419,10 @@ private: /// ProcessDiag - This is the method used to report a diagnostic that is /// finally fully formed. - void ProcessDiag(); + /// + /// \returns true if the diagnostic was emitted, false if it was + /// suppressed. + bool ProcessDiag(); }; //===----------------------------------------------------------------------===// @@ -448,14 +461,26 @@ public: NumCodeModificationHints = D.NumCodeModificationHints; } + /// \brief Simple enumeration value used to give a name to the + /// suppress-diagnostic constructor. + enum SuppressKind { Suppress }; + + /// \brief Create an empty DiagnosticBuilder object that represents + /// no actual diagnostic. + explicit DiagnosticBuilder(SuppressKind) + : DiagObj(0), NumArgs(0), NumRanges(0), NumCodeModificationHints(0) { } + /// \brief Force the diagnostic builder to emit the diagnostic now. /// /// Once this function has been called, the DiagnosticBuilder object /// should not be used again before it is destroyed. - void Emit() { + /// + /// \returns true if a diagnostic was emitted, false if the + /// diagnostic was suppressed. + bool Emit() { // If DiagObj is null, then its soul was stolen by the copy ctor // or the user called Emit(). - if (DiagObj == 0) return; + if (DiagObj == 0) return false; // When emitting diagnostics, we set the final argument count into // the Diagnostic object. @@ -465,13 +490,15 @@ public: // Process the diagnostic, sending the accumulated information to the // DiagnosticClient. - DiagObj->ProcessDiag(); + bool Emitted = DiagObj->ProcessDiag(); // Clear out the current diagnostic object. DiagObj->Clear(); // This diagnostic is dead. DiagObj = 0; + + return Emitted; } /// Destructor - The dtor emits the diagnostic if it hasn't already @@ -486,28 +513,34 @@ public: void AddString(const std::string &S) const { assert(NumArgs < Diagnostic::MaxArguments && "Too many arguments to diagnostic!"); - DiagObj->DiagArgumentsKind[NumArgs] = Diagnostic::ak_std_string; - DiagObj->DiagArgumentsStr[NumArgs++] = S; + if (DiagObj) { + DiagObj->DiagArgumentsKind[NumArgs] = Diagnostic::ak_std_string; + DiagObj->DiagArgumentsStr[NumArgs++] = S; + } } void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const { assert(NumArgs < Diagnostic::MaxArguments && "Too many arguments to diagnostic!"); - DiagObj->DiagArgumentsKind[NumArgs] = Kind; - DiagObj->DiagArgumentsVal[NumArgs++] = V; + if (DiagObj) { + DiagObj->DiagArgumentsKind[NumArgs] = Kind; + DiagObj->DiagArgumentsVal[NumArgs++] = V; + } } void AddSourceRange(const SourceRange &R) const { assert(NumRanges < sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) && "Too many arguments to diagnostic!"); - DiagObj->DiagRanges[NumRanges++] = &R; + if (DiagObj) + DiagObj->DiagRanges[NumRanges++] = &R; } void AddCodeModificationHint(const CodeModificationHint &Hint) const { assert(NumCodeModificationHints < Diagnostic::MaxCodeModificationHints && "Too many code modification hints!"); - DiagObj->CodeModificationHints[NumCodeModificationHints++] = Hint; + if (DiagObj) + DiagObj->CodeModificationHints[NumCodeModificationHints++] = Hint; } }; diff --git a/include/clang/Basic/Diagnostic.td b/include/clang/Basic/Diagnostic.td index 67d8eaafc9..6aa3b438ab 100644 --- a/include/clang/Basic/Diagnostic.td +++ b/include/clang/Basic/Diagnostic.td @@ -45,6 +45,7 @@ class Diagnostic<string text, DiagClass DC, DiagMapping defaultmapping> { string Component = ?; string Text = text; DiagClass Class = DC; + bit SFINAE = 1; DiagMapping DefaultMapping = defaultmapping; DiagGroup Group; } @@ -61,6 +62,8 @@ class DefaultWarn { DiagMapping DefaultMapping = MAP_WARNING; } class DefaultError { DiagMapping DefaultMapping = MAP_ERROR; } class DefaultFatal { DiagMapping DefaultMapping = MAP_FATAL; } +class NoSFINAE { bit SFINAE = 0; } + // Definitions for Diagnostics. include "DiagnosticASTKinds.td" include "DiagnosticAnalysisKinds.td" diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 26c5fc80b8..fa4f430591 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -331,7 +331,7 @@ def note_overridden_virtual_function : Note< def err_covariant_return_inaccessible_base : Error< "return type of virtual function %2 is not covariant with the return type " "of the function it overrides " - "(conversion from %0 to inaccessible base class %1)">; + "(conversion from %0 to inaccessible base class %1)">, NoSFINAE; def err_covariant_return_ambiguous_derived_to_base_conv : Error< "return type of virtual function %3 is not covariant with the return type of " "the function it overrides (ambiguous conversion from derived class " @@ -782,7 +782,8 @@ def unsup_template_partial_spec_ordering : Error< // C++ Template Instantiation def err_template_recursion_depth_exceeded : Error< - "recursive template instantiation exceeded maximum depth of %0">,DefaultFatal; + "recursive template instantiation exceeded maximum depth of %0">, + DefaultFatal, NoSFINAE; def note_template_recursion_depth : Note< "use -ftemplate-depth-N to increase recursive template instantiation depth">; @@ -1604,7 +1605,7 @@ def err_memptr_conv_via_virtual : Error< // C++ access control def err_conv_to_inaccessible_base : Error< - "conversion from %0 to inaccessible base class %1">; + "conversion from %0 to inaccessible base class %1">, NoSFINAE; def note_inheritance_specifier_here : Note< "'%0' inheritance specifier here">; def note_inheritance_implicitly_private_here : Note< @@ -1851,8 +1852,10 @@ def warn_ivar_use_hidden : Warning< "local declaration of %0 hides instance variable">; def error_ivar_use_in_class_method : Error< "instance variable %0 accessed in class method">; -def error_private_ivar_access : Error<"instance variable %0 is private">; -def error_protected_ivar_access : Error<"instance variable %0 is protected">; +def error_private_ivar_access : Error<"instance variable %0 is private">, + NoSFINAE; +def error_protected_ivar_access : Error<"instance variable %0 is protected">, + NoSFINAE; def warn_maynot_respond : Warning<"%0 may not respond to %1">; def warn_attribute_method_def : Warning< "method attribute can only be specified on method declarations">; |