From 6403e937d65dbd3c48ff52f1df6eb579f76d53e6 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Fri, 14 Nov 2014 00:37:55 +0000 Subject: [PATCH] PR21565 Add an egregious hack to support broken libstdc++ headers that declare 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. llvm-svn: 221955 --- clang/include/clang/Sema/Sema.h | 4 ++ clang/lib/Parse/ParseDecl.cpp | 3 +- clang/lib/Sema/SemaExceptionSpec.cpp | 27 +++++++++++++ ...r.cpp => libstdcxx_explicit_init_list_hack.cpp} | 0 clang/test/SemaCXX/libstdcxx_pair_swap_hack.cpp | 47 ++++++++++++++++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) rename clang/test/SemaCXX/{cxx0x-initializer-stdinitializerlist-system-header.cpp => libstdcxx_explicit_init_list_hack.cpp} (100%) create mode 100644 clang/test/SemaCXX/libstdcxx_pair_swap_hack.cpp diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index e5bdce4..a7d8a24 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -4109,6 +4109,10 @@ public: SmallVectorImpl &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. diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 8fbe783..2e30c52 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -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, diff --git a/clang/lib/Sema/SemaExceptionSpec.cpp b/clang/lib/Sema/SemaExceptionSpec.cpp index c35de6b..7175c01 100644 --- a/clang/lib/Sema/SemaExceptionSpec.cpp +++ b/clang/lib/Sema/SemaExceptionSpec.cpp @@ -35,6 +35,33 @@ static const FunctionProtoType *GetUnderlyingFunction(QualType T) return T->getAs(); } +/// 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(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(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/clang/test/SemaCXX/cxx0x-initializer-stdinitializerlist-system-header.cpp b/clang/test/SemaCXX/libstdcxx_explicit_init_list_hack.cpp similarity index 100% rename from clang/test/SemaCXX/cxx0x-initializer-stdinitializerlist-system-header.cpp rename to clang/test/SemaCXX/libstdcxx_explicit_init_list_hack.cpp diff --git a/clang/test/SemaCXX/libstdcxx_pair_swap_hack.cpp b/clang/test/SemaCXX/libstdcxx_pair_swap_hack.cpp new file mode 100644 index 0000000..8c7c782 --- /dev/null +++ b/clang/test/SemaCXX/libstdcxx_pair_swap_hack.cpp @@ -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 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 void swap(T &, T &); + + template struct pair { + void swap(pair &other) noexcept(noexcept(swap(*this, other))); + }; +} + +#else + +#define BE_THE_HEADER +#include __FILE__ + +struct X {}; +using PX = std::pair; +using PI = std::pair; +void swap(PX &, PX &) noexcept; +PX px; +PI pi; + +static_assert(noexcept(px.swap(px)), ""); +static_assert(!noexcept(pi.swap(pi)), ""); + +namespace sad { + template void swap(T &, T &); + + template struct pair { + void swap(pair &other) noexcept(noexcept(swap(*this, other))); // expected-error {{too many arguments}} expected-note {{declared here}} + }; + + pair pi; + + static_assert(!noexcept(pi.swap(pi)), ""); // expected-note {{in instantiation of}} +} + +#endif -- 2.7.4