]> granicus.if.org Git - clang/commitdiff
PR21565 Add an egregious hack to support broken libstdc++ headers that declare
authorRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 14 Nov 2014 00:37:55 +0000 (00:37 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 14 Nov 2014 00:37:55 +0000 (00:37 +0000)
a member named 'swap' and then expect unqualified lookup for the name 'swap' in
its exception specification to find anything else.

Without delay-parsed exception specifications, this was ill-formed (NDR) by
[basic.scope.class]p1, rule 2. With delay-parsed exception specifications, the
call to 'swap' unambiguously finds the function being declared, which then
fails because the arguments don't work for that function.

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

include/clang/Sema/Sema.h
lib/Parse/ParseDecl.cpp
lib/Sema/SemaExceptionSpec.cpp
test/SemaCXX/libstdcxx_explicit_init_list_hack.cpp [moved from test/SemaCXX/cxx0x-initializer-stdinitializerlist-system-header.cpp with 100% similarity]
test/SemaCXX/libstdcxx_pair_swap_hack.cpp [new file with mode: 0644]

index e5bdce4e6c90069bf3db4f2b1c4878b335b8ef63..a7d8a241acc29810fd21810de8753e7d426a7e99 100644 (file)
@@ -4109,6 +4109,10 @@ public:
                                    SmallVectorImpl<QualType> &Exceptions,
                                    FunctionProtoType::ExceptionSpecInfo &ESI);
 
+  /// \brief Determine if we're in a case where we need to (incorrectly) eagerly
+  /// parse an exception specification to work around a libstdc++ bug.
+  bool isLibstdcxxEagerExceptionSpecHack(const Declarator &D);
+
   /// \brief Add an exception-specification to the given member function
   /// (or member function template). The exception-specification was parsed
   /// after the method itself was declared.
index 8fbe7833a1ffe8183b5650c019e4c0678f3bfdea..2e30c52df9393b47e2b42513ef74b02345fad694 100644 (file)
@@ -5281,7 +5281,8 @@ void Parser::ParseFunctionDeclarator(Declarator &D,
 
       // Parse exception-specification[opt].
       bool Delayed = D.isFirstDeclarationOfMember() &&
-                     D.isFunctionDeclaratorAFunctionDeclaration();
+                     D.isFunctionDeclaratorAFunctionDeclaration() &&
+                     !Actions.isLibstdcxxEagerExceptionSpecHack(D);
       ESpecType = tryParseExceptionSpecification(Delayed,
                                                  ESpecRange,
                                                  DynamicExceptions,
index c35de6b8edaa7a64315f245e6dec4ab73a90481a..7175c016734f809b89fdc857dbeca6c08f207fc7 100644 (file)
@@ -35,6 +35,33 @@ static const FunctionProtoType *GetUnderlyingFunction(QualType T)
   return T->getAs<FunctionProtoType>();
 }
 
+/// HACK: libstdc++ has a bug where it shadows std::swap with a member
+/// swap function then tries to call std::swap unqualified from the exception
+/// specification of that function. This function detects whether we're in
+/// such a case and turns off delay-parsing of exception specifications.
+bool Sema::isLibstdcxxEagerExceptionSpecHack(const Declarator &D) {
+  auto *RD = dyn_cast<CXXRecordDecl>(CurContext);
+
+  // All the problem cases are member functions named "swap" within class
+  // templates declared directly within namespace std.
+  if (!RD || RD->getEnclosingNamespaceContext() != getStdNamespace() ||
+      !RD->getIdentifier() || !RD->getDescribedClassTemplate() ||
+      !D.getIdentifier() || !D.getIdentifier()->isStr("swap"))
+    return false;
+
+  // Only apply this hack within a system header.
+  if (!Context.getSourceManager().isInSystemHeader(D.getLocStart()))
+    return false;
+
+  return llvm::StringSwitch<bool>(RD->getIdentifier()->getName())
+      .Case("array", true)
+      .Case("pair", true)
+      .Case("priority_queue", true)
+      .Case("stack", true)
+      .Case("queue", true)
+      .Default(false);
+}
+
 /// CheckSpecifiedExceptionType - Check if the given type is valid in an
 /// exception specification. Incomplete types, or pointers to incomplete types
 /// other than void are not allowed.
diff --git a/test/SemaCXX/libstdcxx_pair_swap_hack.cpp b/test/SemaCXX/libstdcxx_pair_swap_hack.cpp
new file mode 100644 (file)
index 0000000..8c7c782
--- /dev/null
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -fsyntax-only %s -std=c++11 -verify -fexceptions -fcxx-exceptions
+
+// This is a test for an egregious hack in Clang that works around
+// an issue with GCC's <utility> implementation. std::pair::swap
+// has an exception specification that makes an unqualified call to
+// swap. This is invalid, because it ends up calling itself with
+// the wrong number of arguments.
+
+#ifdef BE_THE_HEADER
+
+#pragma GCC system_header
+namespace std {
+  template<typename T> void swap(T &, T &);
+
+  template<typename A, typename B> struct pair {
+    void swap(pair &other) noexcept(noexcept(swap(*this, other)));
+  };
+}
+
+#else
+
+#define BE_THE_HEADER
+#include __FILE__
+
+struct X {};
+using PX = std::pair<X, X>;
+using PI = std::pair<int, int>;
+void swap(PX &, PX &) noexcept;
+PX px;
+PI pi;
+
+static_assert(noexcept(px.swap(px)), "");
+static_assert(!noexcept(pi.swap(pi)), "");
+
+namespace sad {
+  template<typename T> void swap(T &, T &);
+
+  template<typename A, typename B> struct pair {
+    void swap(pair &other) noexcept(noexcept(swap(*this, other))); // expected-error {{too many arguments}} expected-note {{declared here}}
+  };
+
+  pair<int, int> pi;
+
+  static_assert(!noexcept(pi.swap(pi)), ""); // expected-note {{in instantiation of}}
+}
+
+#endif