]> granicus.if.org Git - clang/commitdiff
Add a workaround for a libstdc++-4.2 <tr1/hashtable> bug. This header uses
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 20 Jun 2013 02:18:31 +0000 (02:18 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 20 Jun 2013 02:18:31 +0000 (02:18 +0000)
  return false;

in a function returning a pointer. 'false' was a null pointer constant in C++98
but is not in C++11. Punch a very small hole in the initialization rules in
C++11 mode to allow this specific case in system headers.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@184395 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaInit.cpp
test/SemaCXX/libstdcxx_pointer_return_false_hack.cpp [new file with mode: 0644]

index 4e55da328a9680b8c49a710a792f7b38099a59a6..9d7fcda8fddda5ba584d8b7c788b6c804261bc41 100644 (file)
@@ -4135,6 +4135,24 @@ static void TryUserDefinedConversion(Sema &S,
   }
 }
 
+/// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>,
+/// a function with a pointer return type contains a 'return false;' statement.
+/// In C++11, 'false' is not a null pointer, so this breaks the build of any
+/// code using that header.
+///
+/// Work around this by treating 'return false;' as zero-initializing the result
+/// if it's used in a pointer-returning function in a system header.
+static bool isLibstdcxxPointerReturnFalseHack(Sema &S,
+                                              const InitializedEntity &Entity,
+                                              const Expr *Init) {
+  return S.getLangOpts().CPlusPlus11 &&
+         Entity.getKind() == InitializedEntity::EK_Result &&
+         Entity.getType()->isPointerType() &&
+         isa<CXXBoolLiteralExpr>(Init) &&
+         !cast<CXXBoolLiteralExpr>(Init)->getValue() &&
+         S.getSourceManager().isInSystemHeader(Init->getExprLoc());
+}
+
 /// The non-zero enum values here are indexes into diagnostic alternatives.
 enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar };
 
@@ -4571,9 +4589,11 @@ InitializationSequence::InitializationSequence(Sema &S,
     AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy);
   } else if (ICS.isBad()) {
     DeclAccessPair dap;
-    if (Initializer->getType() == Context.OverloadTy && 
-          !S.ResolveAddressOfOverloadedFunction(Initializer
-                      , DestType, false, dap))
+    if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) {
+      AddZeroInitializationStep(Entity.getType());
+    } else if (Initializer->getType() == Context.OverloadTy &&
+               !S.ResolveAddressOfOverloadedFunction(Initializer, DestType,
+                                                     false, dap))
       SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
     else
       SetFailed(InitializationSequence::FK_ConversionFailed);
diff --git a/test/SemaCXX/libstdcxx_pointer_return_false_hack.cpp b/test/SemaCXX/libstdcxx_pointer_return_false_hack.cpp
new file mode 100644 (file)
index 0000000..ad93609
--- /dev/null
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -fsyntax-only %s -std=c++11 -verify
+
+// This is a test for an egregious hack in Clang that works around
+// an issue with GCC's <type_traits> implementation. std::common_type
+// relies on pre-standard rules for decltype(), in which it doesn't
+// produce reference types so frequently.
+
+#ifdef BE_THE_HEADER
+
+#pragma GCC system_header
+namespace std {
+  namespace tr1 {
+    template<typename T> struct hashnode;
+    template<typename T> struct hashtable {
+      typedef hashnode<T> node;
+      node *find_node() {
+        // This is ill-formed in C++11, per core issue 903, but we accept
+        // it anyway in a system header.
+        return false;
+      }
+    };
+  }
+}
+
+#else
+
+#define BE_THE_HEADER
+#include "libstdcxx_pointer_return_false_hack.cpp"
+
+auto *test1 = std::tr1::hashtable<int>().find_node();
+
+void *test2() { return false; } // expected-error {{cannot initialize}}
+
+#endif