[clang] 7119f76 - [clang] added allocsize attribute to allocation functions
Dávid Bolvanský via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 5 05:27:59 PST 2022
Author: Dávid Bolvanský
Date: 2022-02-05T14:26:35+01:00
New Revision: 7119f76c47797391fddcfcb60f29e70a2b424713
URL: https://github.com/llvm/llvm-project/commit/7119f76c47797391fddcfcb60f29e70a2b424713
DIFF: https://github.com/llvm/llvm-project/commit/7119f76c47797391fddcfcb60f29e70a2b424713.diff
LOG: [clang] added allocsize attribute to allocation functions
Added:
clang/test/CodeGen/allocs-fns-allocsize.c
Modified:
clang/lib/Sema/SemaDecl.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f2871d82b0409..fce33991c4aa4 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15340,6 +15340,26 @@ void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) {
default:
break;
}
+
+ // Add allocsize attribute for allocation functions.
+ switch (BuiltinID) {
+ case Builtin::BIcalloc:
+ FD->addAttr(AllocSizeAttr::CreateImplicit(
+ Context, ParamIdx(1, FD), ParamIdx(2, FD), FD->getLocation()));
+ break;
+ case Builtin::BImemalign:
+ case Builtin::BIaligned_alloc:
+ case Builtin::BIrealloc:
+ FD->addAttr(AllocSizeAttr::CreateImplicit(Context, ParamIdx(2, FD),
+ ParamIdx(), FD->getLocation()));
+ break;
+ case Builtin::BImalloc:
+ FD->addAttr(AllocSizeAttr::CreateImplicit(Context, ParamIdx(1, FD),
+ ParamIdx(), FD->getLocation()));
+ break;
+ default:
+ break;
+ }
}
AddKnownFunctionAttributesForReplaceableGlobalAllocationFunction(FD);
diff --git a/clang/test/CodeGen/allocs-fns-allocsize.c b/clang/test/CodeGen/allocs-fns-allocsize.c
new file mode 100644
index 0000000000000..27133b252372e
--- /dev/null
+++ b/clang/test/CodeGen/allocs-fns-allocsize.c
@@ -0,0 +1,39 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm < %s | FileCheck %s
+
+typedef __SIZE_TYPE__ size_t;
+
+void *malloc(size_t);
+void *calloc(size_t, size_t);
+void *realloc(void *, size_t);
+void *aligned_alloc(size_t, size_t);
+void *memalign(size_t, size_t);
+
+void *malloc_test(size_t n) {
+ return malloc(n);
+}
+
+void *calloc_test(size_t e, size_t n) {
+ return calloc(e, n);
+}
+
+void *realloc_test(void *p, size_t n) {
+ return realloc(p, n);
+}
+
+void *aligned_alloc_test(size_t n, size_t a) {
+ return aligned_alloc(a, n);
+}
+
+void *memalign_test(size_t n, size_t a) {
+ return memalign(a, n);
+}
+
+// CHECK: @malloc(i64 noundef) #1
+// CHECK: @calloc(i64 noundef, i64 noundef) #2
+// CHECK: @realloc(i8* noundef, i64 noundef) #3
+// CHECK: @aligned_alloc(i64 noundef, i64 noundef) #3
+// CHECK: @memalign(i64 noundef, i64 noundef) #3
+
+// CHECK: attributes #1 = { allocsize(0)
+// CHECK: attributes #2 = { allocsize(0,1)
+// CHECK: attributes #3 = { allocsize(1)
More information about the cfe-commits
mailing list