diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -3445,3 +3445,11 @@ let Subjects = SubjectList<[ParmVar]>; let Documentation = [ReleaseHandleDocs]; } + +def Builtin : InheritableAttr { + let Spellings = []; + let Args = [UnsignedArgument<"ID">]; + let Subjects = SubjectList<[Function]>; + let SemaHandler = 0; + let Documentation = [Undocumented]; +} diff --git a/clang/include/clang/Basic/IdentifierTable.h b/clang/include/clang/Basic/IdentifierTable.h --- a/clang/include/clang/Basic/IdentifierTable.h +++ b/clang/include/clang/Basic/IdentifierTable.h @@ -225,7 +225,7 @@ } void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; } - /// True if setNotBuiltin() was called. + /// True if revertBuiltin() was called. bool hasRevertedBuiltin() const { return ObjCOrBuiltinID == tok::NUM_OBJC_KEYWORDS; } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3900,6 +3900,8 @@ ObjCInterfaceDecl *getObjCInterfaceDecl(IdentifierInfo *&Id, SourceLocation IdLoc, bool TypoCorrection = false); + FunctionDecl *CreateBuiltin(IdentifierInfo *II, unsigned ID, + SourceLocation Loc); NamedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S, bool ForRedeclaration, SourceLocation Loc); diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3162,44 +3162,24 @@ /// functions as their wrapped builtins. This shouldn't be done in general, but /// it's useful in Sema to diagnose calls to wrappers based on their semantics. unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { - unsigned BuiltinID; + unsigned BuiltinID = 0; if (const auto *ABAA = getAttr()) { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); - } else { - if (!getIdentifier()) - return 0; - - BuiltinID = getIdentifier()->getBuiltinID(); + } else if (const auto *A = getAttr()) { + BuiltinID = A->getID(); } if (!BuiltinID) return 0; - ASTContext &Context = getASTContext(); - if (Context.getLangOpts().CPlusPlus) { - const auto *LinkageDecl = - dyn_cast(getFirstDecl()->getDeclContext()); - // In C++, the first declaration of a builtin is always inside an implicit - // extern "C". - // FIXME: A recognised library function may not be directly in an extern "C" - // declaration, for instance "extern "C" { namespace std { decl } }". - if (!LinkageDecl) { - if (BuiltinID == Builtin::BI__GetExceptionInfo && - Context.getTargetInfo().getCXXABI().isMicrosoft()) - return Builtin::BI__GetExceptionInfo; - return 0; - } - if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c) - return 0; - } - // If the function is marked "overloadable", it has a different mangled name // and is not the C library function. if (!ConsiderWrapperFunctions && hasAttr() && !hasAttr()) return 0; + ASTContext &Context = getASTContext(); if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) return BuiltinID; diff --git a/clang/lib/Headers/intrin.h b/clang/lib/Headers/intrin.h --- a/clang/lib/Headers/intrin.h +++ b/clang/lib/Headers/intrin.h @@ -117,7 +117,7 @@ unsigned __int64 __readpmc(unsigned long); unsigned long __segmentlimit(unsigned long); void __sidt(void *); -static __inline__ +__inline__ void __stosb(unsigned char *, unsigned char, size_t); static __inline__ void __stosd(unsigned long *, unsigned long, size_t); @@ -146,11 +146,11 @@ void __writefsqword(unsigned long, unsigned __int64); void __writefsword(unsigned long, unsigned short); void __writemsr(unsigned long, unsigned __int64); -static __inline__ +__inline__ void *_AddressOfReturnAddress(void); -static __inline__ +__inline__ unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask); -static __inline__ +__inline__ unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask); unsigned char _bittest(long const *, long); unsigned char _bittestandcomplement(long *, long); @@ -169,10 +169,10 @@ long _InterlockedExchangeAdd_HLERelease(long volatile *, long); __int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64); __int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64); -static __inline__ void +__inline__ void __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead"))) _ReadBarrier(void); -static __inline__ void +__inline__ void __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead"))) _ReadWriteBarrier(void); unsigned int _rorx_u32(unsigned int, const unsigned int); @@ -185,7 +185,7 @@ void _Store_HLERelease(long volatile *, long); void _Store64_HLERelease(__int64 volatile *, __int64); void _StorePointer_HLERelease(void *volatile *, void *); -static __inline__ void +__inline__ void __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead"))) _WriteBarrier(void); unsigned __int32 xbegin(void); @@ -197,7 +197,7 @@ void __addgsdword(unsigned long, unsigned long); void __addgsqword(unsigned long, unsigned __int64); void __addgsword(unsigned long, unsigned short); -static __inline__ +__inline__ void __faststorefence(void); void __incgsbyte(unsigned long); void __incgsdword(unsigned long); diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2040,6 +2040,48 @@ llvm_unreachable("unhandled error kind"); } +FunctionDecl *Sema::CreateBuiltin(IdentifierInfo *II, unsigned ID, + SourceLocation Loc) { + DeclContext *Parent = Context.getTranslationUnitDecl(); + + if (getLangOpts().CPlusPlus) { + LinkageSpecDecl *CLinkageDecl = LinkageSpecDecl::Create( + Context, Parent, Loc, Loc, LinkageSpecDecl::lang_c, false); + CLinkageDecl->setImplicit(); + Parent->addDecl(CLinkageDecl); + Parent = CLinkageDecl; + } + + ASTContext::GetBuiltinTypeError Error; + QualType R = Context.GetBuiltinType(ID, Error); + + if (Error || R.isNull()) + return nullptr; + + FunctionDecl *New = + FunctionDecl::Create(Context, Parent, Loc, Loc, II, R, /*TInfo=*/nullptr, + SC_Extern, false, R->isFunctionProtoType()); + New->setImplicit(); + New->addAttr(BuiltinAttr::CreateImplicit(Context, ID)); + + // Create Decl objects for each parameter, adding them to the + // FunctionDecl. + if (const FunctionProtoType *FT = dyn_cast(R)) { + SmallVector Params; + for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { + ParmVarDecl *parm = ParmVarDecl::Create( + Context, New, SourceLocation(), SourceLocation(), nullptr, + FT->getParamType(i), /*TInfo=*/nullptr, SC_None, nullptr); + parm->setScopeInfo(0, i); + Params.push_back(parm); + } + New->setParams(Params); + } + + AddKnownFunctionAttributes(New); + return New; +} + /// LazilyCreateBuiltin - The specified Builtin-ID was first used at /// file scope. lazily create a decl for it. ForRedeclaration is true /// if we're creating this built-in in anticipation of redeclaring the @@ -2091,40 +2133,7 @@ if (R.isNull()) return nullptr; - DeclContext *Parent = Context.getTranslationUnitDecl(); - if (getLangOpts().CPlusPlus) { - LinkageSpecDecl *CLinkageDecl = - LinkageSpecDecl::Create(Context, Parent, Loc, Loc, - LinkageSpecDecl::lang_c, false); - CLinkageDecl->setImplicit(); - Parent->addDecl(CLinkageDecl); - Parent = CLinkageDecl; - } - - FunctionDecl *New = FunctionDecl::Create(Context, - Parent, - Loc, Loc, II, R, /*TInfo=*/nullptr, - SC_Extern, - false, - R->isFunctionProtoType()); - New->setImplicit(); - - // Create Decl objects for each parameter, adding them to the - // FunctionDecl. - if (const FunctionProtoType *FT = dyn_cast(R)) { - SmallVector Params; - for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { - ParmVarDecl *parm = - ParmVarDecl::Create(Context, New, SourceLocation(), SourceLocation(), - nullptr, FT->getParamType(i), /*TInfo=*/nullptr, - SC_None, nullptr); - parm->setScopeInfo(0, i); - Params.push_back(parm); - } - New->setParams(Params); - } - - AddKnownFunctionAttributes(New); + FunctionDecl *New = CreateBuiltin(II, ID, Loc); RegisterLocallyScopedExternCDecl(New, S); // TUScope is the translation-unit scope to insert this function into. @@ -2132,7 +2141,7 @@ // relate Scopes to DeclContexts, and probably eliminate CurContext // entirely, but we're not there yet. DeclContext *SavedContext = CurContext; - CurContext = Parent; + CurContext = New->getDeclContext(); PushOnScopeChains(New, TUScope); CurContext = SavedContext; return New; @@ -3330,7 +3339,11 @@ // there but not here. NewTypeInfo = NewTypeInfo.withCallingConv(OldTypeInfo.getCC()); RequiresAdjustment = true; - } else if (New->getBuiltinID()) { + } else if (Old->getBuiltinID()) { + // Builtin attribute isn't propagated to the new one yet at this point. + // Check if the old is a builtin. + // TODO: Maybe we should only warn if the redeclaration is compatible? + // Calling Conventions on a Builtin aren't really useful and setting a // default calling convention and cdecl'ing some builtin redeclarations is // common, so warn and ignore the calling convention on the redeclaration. @@ -9608,6 +9621,34 @@ } } + // In C builtins get merged with implicitly lazily created declarations. + // In C++ we need to check if it's a builtin and add the BuiltinAttr here. + if (getLangOpts().CPlusPlus) { + if (IdentifierInfo *II = Previous.getLookupName().getAsIdentifierInfo()) { + if (unsigned BuiltinID = II->getBuiltinID()) { + FunctionDecl *D = CreateBuiltin(II, BuiltinID, NewFD->getLocation()); + + // We want noexcept declarations to match. Create an identical function + // type, but remove the exception spec. + const FunctionProtoType *Type = + NewFD->getType()->getAs(); + FunctionProtoType::ExtProtoInfo Info = Type->getExtProtoInfo(); + Info.ExceptionSpec.Type = EST_None; + QualType NewTy = Context.getFunctionType(Type->getReturnType(), + Type->getParamTypes(), Info); + + // FIXME: A recognised library function may not be directly in an extern + // "C" declaration, for instance "extern "C" { namespace std { decl } + // }". + if ((D && Context.typesAreCompatible(NewTy, D->getType())) || + (BuiltinID == Builtin::BI__GetExceptionInfo && + Context.getTargetInfo().getCXXABI().isMicrosoft())) + NewFD->addAttr( + BuiltinAttr::CreateImplicit(Context, II->getBuiltinID())); + } + } + } + ProcessPragmaWeak(S, NewFD); checkAttributesAfterMerging(*this, *NewFD); diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -6051,6 +6051,7 @@ Params.push_back(Parm); } OverloadDecl->setParams(Params); + Sema->mergeDeclAttributes(OverloadDecl, FDecl); return OverloadDecl; } diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -894,10 +894,9 @@ Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) return false; - if (NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, - BuiltinID, TUScope, - R.isForRedeclaration(), - R.getNameLoc())) { + if (NamedDecl *D = + LazilyCreateBuiltin(II, BuiltinID, TUScope, + R.isForRedeclaration(), R.getNameLoc())) { R.addDecl(D); return true; } diff --git a/clang/test/AST/ast-dump-attr.cpp b/clang/test/AST/ast-dump-attr.cpp --- a/clang/test/AST/ast-dump-attr.cpp +++ b/clang/test/AST/ast-dump-attr.cpp @@ -109,6 +109,7 @@ extern "C" int printf(const char *format, ...); // CHECK: FunctionDecl{{.*}}printf // CHECK-NEXT: ParmVarDecl{{.*}}format{{.*}}'const char *' +// CHECK-NEXT: BuiltinAttr{{.*}}Implicit // CHECK-NEXT: FormatAttr{{.*}}Implicit printf 1 2 alignas(8) extern int x; diff --git a/clang/test/CodeGen/builtin-redeclaration.c b/clang/test/CodeGen/builtin-redeclaration.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/builtin-redeclaration.c @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -emit-llvm-only %s + +// PR45410 +// Ensure we mark local extern redeclarations with a different type as non-builtin. +void non_builtin() { + extern float exp(); + exp(); // Will crash due to wrong number of arguments if this calls the builtin. +} + +// PR45410 +// We mark exp() builtin as const with -fno-math-errno (default). +// We mustn't do that for extern redeclarations of builtins where the type differs. +float attribute() { + extern float exp(); + return exp(1); +} diff --git a/clang/test/CodeGen/callback_pthread_create.c b/clang/test/CodeGen/callback_pthread_create.c --- a/clang/test/CodeGen/callback_pthread_create.c +++ b/clang/test/CodeGen/callback_pthread_create.c @@ -14,6 +14,7 @@ typedef __darwin_pthread_t pthread_t; typedef __darwin_pthread_attr_t pthread_attr_t; +// FIXME: How to do builtin handling for this? int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *); //} diff --git a/clang/test/Sema/implicit-builtin-decl.c b/clang/test/Sema/implicit-builtin-decl.c --- a/clang/test/Sema/implicit-builtin-decl.c +++ b/clang/test/Sema/implicit-builtin-decl.c @@ -1,5 +1,4 @@ // RUN: %clang_cc1 -fsyntax-only -verify %s -// RUN: not %clang_cc1 -fsyntax-only -ast-dump %s | FileCheck %s void f() { int *ptr = malloc(sizeof(int) * 10); // expected-warning{{implicitly declaring library function 'malloc' with type}} \ @@ -63,9 +62,5 @@ struct __jmp_buf_tag {}; void sigsetjmp(struct __jmp_buf_tag[1], int); // expected-warning{{declaration of built-in function 'sigsetjmp' requires the declaration of the 'jmp_buf' type, commonly provided in the header .}} -// CHECK: FunctionDecl {{.*}} col:6 sigsetjmp ' -// CHECK-NOT: FunctionDecl -// CHECK: ReturnsTwiceAttr {{.*}} <{{.*}}> Implicit - // PR40692 void pthread_create(); // no warning expected diff --git a/clang/test/Sema/warn-fortify-source.c b/clang/test/Sema/warn-fortify-source.c --- a/clang/test/Sema/warn-fortify-source.c +++ b/clang/test/Sema/warn-fortify-source.c @@ -1,8 +1,6 @@ // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_PASS_OBJECT_SIZE // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS // RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_PASS_OBJECT_SIZE // RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS typedef unsigned long size_t; @@ -13,13 +11,7 @@ extern int sprintf(char *str, const char *format, ...); -#if defined(USE_PASS_OBJECT_SIZE) -void *memcpy(void *dst, const void *src, size_t c); -static void *memcpy(void *dst __attribute__((pass_object_size(1))), const void *src, size_t c) __attribute__((overloadable)) __asm__("merp"); -static void *memcpy(void *const dst __attribute__((pass_object_size(1))), const void *src, size_t c) __attribute__((overloadable)) { - return 0; -} -#elif defined(USE_BUILTINS) +#if defined(USE_BUILTINS) #define memcpy(x,y,z) __builtin_memcpy(x,y,z) #else void *memcpy(void *dst, const void *src, size_t c); @@ -45,14 +37,7 @@ }; struct pair p; char buf[20]; - memcpy(&p.first, buf, 20); -#ifdef USE_PASS_OBJECT_SIZE - // Use the more strict checking mode on the pass_object_size attribute: - // expected-warning@-3 {{memcpy' will always overflow; destination buffer has size 4, but size argument is 20}} -#else - // Or just fallback to type 0: - // expected-warning@-6 {{memcpy' will always overflow; destination buffer has size 8, but size argument is 20}} -#endif + memcpy(&p.first, buf, 20); // expected-warning {{memcpy' will always overflow; destination buffer has size 8, but size argument is 20}} } void call_strncat() { diff --git a/clang/test/SemaCXX/cxx11-compat.cpp b/clang/test/SemaCXX/cxx11-compat.cpp --- a/clang/test/SemaCXX/cxx11-compat.cpp +++ b/clang/test/SemaCXX/cxx11-compat.cpp @@ -31,7 +31,7 @@ s = { n }, // expected-warning {{non-constant-expression cannot be narrowed from type 'int' to 'char' in initializer list in C++11}} expected-note {{explicit cast}} t = { 1234 }; // expected-warning {{constant expression evaluates to 1234 which cannot be narrowed to type 'char' in C++11}} expected-warning {{changes value}} expected-note {{explicit cast}} -#define PRIuS "uS" +#define PRIuS "zu" int printf(const char *, ...); typedef __typeof(sizeof(int)) size_t; void h(size_t foo, size_t bar) { diff --git a/clang/test/SemaCXX/warn-unused-local-typedef.cpp b/clang/test/SemaCXX/warn-unused-local-typedef.cpp --- a/clang/test/SemaCXX/warn-unused-local-typedef.cpp +++ b/clang/test/SemaCXX/warn-unused-local-typedef.cpp @@ -67,10 +67,10 @@ void test() { typedef signed long int superint; // no diag - printf("%f", (superint) 42); + printf("%ld", (superint)42); typedef signed long int superint2; // no diag - printf("%f", static_cast(42)); + printf("%ld", static_cast(42)); #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-local-typedef"