From 6564006a388abd6bcf4a2da76c2b50fe43deb70b Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 29 Nov 2016 22:32:05 +0000 Subject: [PATCH] [c++1z] PR31210: ignore exception specification when matching the type of a builtin with the type of an explicit declaration of the same function. llvm-svn: 288208 --- clang/lib/Sema/SemaDecl.cpp | 22 +++++++++++++++++++--- .../test/SemaCXX/cxx1z-noexcept-function-type.cpp | 10 ++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index e8764a0..e2951cf 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -9028,9 +9028,25 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, LookupPredefedObjCSuperType(*this, S, NewFD->getIdentifier()); QualType T = Context.GetBuiltinType(BuiltinID, Error); if (!T.isNull() && !Context.hasSameType(T, NewFD->getType())) { - // The type of this function differs from the type of the builtin, - // so forget about the builtin entirely. - Context.BuiltinInfo.forgetBuiltin(BuiltinID, Context.Idents); + auto WithoutExceptionSpec = [&](QualType T) -> QualType { + auto *Proto = T->getAs(); + if (!Proto) + return T; + return Context.getFunctionType( + Proto->getReturnType(), Proto->getParamTypes(), + Proto->getExtProtoInfo().withExceptionSpec(EST_None)); + }; + + // If the type of the builtin differs only in its exception + // specification, that's OK. + // FIXME: If the types do differ in this way, it would be better to + // retain the 'noexcept' form of the type. + if (!getLangOpts().CPlusPlus1z || + !Context.hasSameType(WithoutExceptionSpec(T), + WithoutExceptionSpec(NewFD->getType()))) + // The type of this function differs from the type of the builtin, + // so forget about the builtin entirely. + Context.BuiltinInfo.forgetBuiltin(BuiltinID, Context.Idents); } } diff --git a/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp b/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp index ae686d3..d9233e6 100644 --- a/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp +++ b/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp @@ -97,3 +97,13 @@ namespace ImplicitExceptionSpec { }; S::~S() {} } + +namespace Builtins { + // Pick two functions that ought to have the same noexceptness. + extern "C" int strcmp(const char *, const char *); + extern "C" int strncmp(const char *, const char *, decltype(sizeof(0))) noexcept; + + // Check we recognized both as builtins. + typedef int arr[strcmp("bar", "foo") + 4 * strncmp("foo", "bar", 4)]; + typedef int arr[3]; +} -- 2.7.4