Skip to content

Commit 396b3f8

Browse files
author
serge-sans-paille
committed
Allow system header to provide their own implementation of some builtin
1 parent 4c5993d commit 396b3f8

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,9 @@ class FunctionDecl : public DeclaratorDecl,
22722272
/// true through IsAligned.
22732273
bool isReplaceableGlobalAllocationFunction(bool *IsAligned = nullptr) const;
22742274

2275+
/// Determine if this function provides an inline implementation of a builtin.
2276+
bool isInlineBuiltinDeclaration() const;
2277+
22752278
/// Determine whether this is a destroying operator delete.
22762279
bool isDestroyingOperatorDelete() const;
22772280

clang/lib/AST/Decl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,6 +3003,14 @@ bool FunctionDecl::isReplaceableGlobalAllocationFunction(bool *IsAligned) const
30033003
return Params == FPT->getNumParams();
30043004
}
30053005

3006+
bool FunctionDecl::isInlineBuiltinDeclaration() const {
3007+
if (!getBuiltinID())
3008+
return false;
3009+
3010+
const FunctionDecl *Definition;
3011+
return hasBody(Definition) && Definition->isInlineSpecified();
3012+
}
3013+
30063014
bool FunctionDecl::isDestroyingOperatorDelete() const {
30073015
// C++ P0722:
30083016
// Within a class C, a single object deallocation function with signature

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4596,8 +4596,15 @@ RValue CodeGenFunction::EmitSimpleCallExpr(const CallExpr *E,
45964596
}
45974597

45984598
static CGCallee EmitDirectCallee(CodeGenFunction &CGF, const FunctionDecl *FD) {
4599+
45994600
if (auto builtinID = FD->getBuiltinID()) {
4600-
return CGCallee::forBuiltin(builtinID, FD);
4601+
// Replaceable builtin provide their own implementation of a builtin. Unless
4602+
// we are in the builtin implementation itself, don't call the actual
4603+
// builtin. If we are in the builtin implementation, avoid trivial infinite
4604+
// recursion.
4605+
if (!FD->isInlineBuiltinDeclaration() ||
4606+
CGF.CurFn->getName() == FD->getName())
4607+
return CGCallee::forBuiltin(builtinID, FD);
46014608
}
46024609

46034610
llvm::Constant *calleePtr = EmitFunctionDeclPointer(CGF.CGM, FD);

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,11 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
18311831
else if (const auto *SA = FD->getAttr<SectionAttr>())
18321832
F->setSection(SA->getName());
18331833

1834+
if (FD->isInlineBuiltinDeclaration()) {
1835+
F->addAttribute(llvm::AttributeList::FunctionIndex,
1836+
llvm::Attribute::NoBuiltin);
1837+
}
1838+
18341839
if (FD->isReplaceableGlobalAllocationFunction()) {
18351840
// A replaceable global allocation function does not act like a builtin by
18361841
// default, only if it is invoked by a new-expression or delete-expression.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -verify -S -emit-llvm -o- %s -isystem %S -DWITH_DECL | FileCheck --check-prefix=CHECK-WITH-DECL %s
2+
// RUN: %clang_cc1 -verify -S -emit-llvm -o- %s -isystem %S -UWITH_DECL | FileCheck --check-prefix=CHECK-NO-DECL %s
3+
// RUN: %clang_cc1 -verify -S -emit-llvm -o- %s -isystem %S -DWITH_SELF_REFERENCE_DECL | FileCheck --check-prefix=CHECK-SELF-REF-DECL %s
4+
//
5+
// CHECK-WITH-DECL-NOT: @llvm.memcpy
6+
// CHECK-NO-DECL: @llvm.memcpy
7+
// CHECK-SELF-REF-DECL: @llvm.memcpy
8+
//
9+
#include <memcpy-nobuiltin.inc>
10+
void test(void *dest, void const *from, size_t n) {
11+
memcpy(dest, from, n);
12+
13+
static char buffer[1];
14+
memcpy(buffer, from, 2); // expected-warning {{'memcpy' will always overflow; destination buffer has size 1, but size argument is 2}}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stddef.h>
2+
extern void *memcpy(void *dest, void const *from, size_t n);
3+
4+
#ifdef WITH_DECL
5+
inline void *memcpy(void *dest, void const *from, size_t n) {
6+
char const *ifrom = from;
7+
char *idest = dest;
8+
while (n--)
9+
*idest++ = *ifrom++;
10+
return dest;
11+
}
12+
#endif
13+
#ifdef WITH_SELF_REFERENCE_DECL
14+
inline void *memcpy(void *dest, void const *from, size_t n) {
15+
if (n != 0)
16+
memcpy(dest, from, n);
17+
return dest;
18+
}
19+
#endif

0 commit comments

Comments
 (0)