[PATCH] Use enable_if to prevent operator<<(DiagnosticBuilder&, bool) from being called for pointer arguments

Stephan Tolksdorf st at quanttec.com
Wed Mar 12 11:04:33 PDT 2014


This commit also fixes three incorrect uses of this operator uncovered by this
change.

Regarding the change in Driver.cpp:
Note that << has a higher precedence than ?:, so that Diag(...) << a ? b : c is
parsed as (Diag(...) << a) ? b : c. Since DiagnosticBuilder is convertible to
bool (which IMHO is a problematic design) this will compile if DiagnosticBuilder
has an operator<< accepting the a argument.

http://llvm-reviews.chandlerc.com/D3057

Files:
  include/clang/Basic/Diagnostic.h
  lib/Driver/Driver.cpp
  lib/Lex/ModuleMap.cpp
  lib/Sema/SemaDeclCXX.cpp

Index: include/clang/Basic/Diagnostic.h
===================================================================
--- include/clang/Basic/Diagnostic.h
+++ include/clang/Basic/Diagnostic.h
@@ -1014,8 +1014,13 @@
   return DB;
 }
 
-inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
-                                           bool I) {
+// We use enable_if here to prevent that this overload is selected for
+// pointers or other arguments that are implicitly convertible to bool.
+template <typename T>
+inline
+typename std::enable_if<std::is_same<T, bool>::value,
+                        const DiagnosticBuilder &>::type
+operator<<(const DiagnosticBuilder &DB, T I) {
   DB.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
   return DB;
 }
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -1192,13 +1192,13 @@
         Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
           << InputArg->getAsString(Args)
           << !!FinalPhaseArg
-          << FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "";
+          << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
       else
         Diag(clang::diag::warn_drv_input_file_unused)
           << InputArg->getAsString(Args)
           << getPhaseName(InitialPhase)
           << !!FinalPhaseArg
-          << FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "";
+          << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
       continue;
     }
 
Index: lib/Lex/ModuleMap.cpp
===================================================================
--- lib/Lex/ModuleMap.cpp
+++ lib/Lex/ModuleMap.cpp
@@ -1282,7 +1282,8 @@
       
       if (ActiveModule) {
         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
-          << Id[I].first << ActiveModule->getTopLevelModule();
+          << Id[I].first
+          << ActiveModule->getTopLevelModule()->getFullModuleName();
       } else {
         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
       }
Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp
+++ lib/Sema/SemaDeclCXX.cpp
@@ -6564,8 +6564,7 @@
     S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline)
       << FixItHint::CreateInsertion(KeywordLoc, "inline ");
   else
-    S.Diag(Loc, diag::err_inline_namespace_mismatch)
-      << IsInline;
+    S.Diag(Loc, diag::err_inline_namespace_mismatch) << true;
 
   S.Diag(PrevNS->getLocation(), diag::note_previous_definition);
   *IsInline = PrevNS->isInline();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3057.1.patch
Type: text/x-patch
Size: 2667 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140312/c5d9bdb2/attachment.bin>


More information about the cfe-commits mailing list