[clang] [clang][Sema] Diagnose passing function pointer to `__builtin_assume_aligned` (PR #153552)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 14 00:59:57 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Victor Chernyakin (localspook)
<details>
<summary>Changes</summary>
The first parameter of the builtin is `const void *`, and function pointers aren't convertible to that. [GCC diagnoses this](https://godbolt.org/z/67njxGE61).
---
Full diff: https://github.com/llvm/llvm-project/pull/153552.diff
3 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2-1)
- (modified) clang/lib/Sema/SemaChecking.cpp (+3-2)
- (modified) clang/test/Sema/builtin-assume-aligned.c (+9-8)
``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 116341f4b66d5..f7f26ed6062b3 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -12829,7 +12829,8 @@ def err_builtin_launder_invalid_arg : Error<
"%select{non-pointer|function pointer|void pointer}0 argument to "
"'__builtin_launder' is not allowed">;
def err_builtin_assume_aligned_invalid_arg : Error<
- "non-pointer argument to '__builtin_assume_aligned' is not allowed">;
+ "%select{non-pointer|function pointer}0 argument to "
+ "'__builtin_assume_aligned' is not allowed">;
def err_builtin_is_within_lifetime_invalid_arg : Error<
"%select{non-|function }0pointer argument to '__builtin_is_within_lifetime' "
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 9ecee18661340..d8ad9f65c1995 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -5660,9 +5660,10 @@ bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) {
{
ExprResult FirstArgResult =
DefaultFunctionArrayLvalueConversion(FirstArg);
- if (!FirstArgResult.get()->getType()->isPointerType()) {
+ QualType FirstArgType = FirstArgResult.get()->getType();
+ if (!FirstArgType->isObjectPointerType()) {
Diag(TheCall->getBeginLoc(), diag::err_builtin_assume_aligned_invalid_arg)
- << TheCall->getSourceRange();
+ << FirstArgType->isFunctionPointerType() << TheCall->getSourceRange();
return true;
}
TheCall->setArg(0, FirstArgResult.get());
diff --git a/clang/test/Sema/builtin-assume-aligned.c b/clang/test/Sema/builtin-assume-aligned.c
index 57378a3426524..c6d1197fe8ab4 100644
--- a/clang/test/Sema/builtin-assume-aligned.c
+++ b/clang/test/Sema/builtin-assume-aligned.c
@@ -77,18 +77,19 @@ int test14(int *a, int b) {
a = (int *)__builtin_assume_aligned(b, 32); // expected-error {{non-pointer argument to '__builtin_assume_aligned' is not allowed}}
}
-int test15(int *b) {
- int arr[3] = {1, 2, 3};
- b = (int *)__builtin_assume_aligned(arr, 32);
- return b[0];
+void test15(void (*f)()) {
+ f = (void (*)())__builtin_assume_aligned(f, 32); // expected-error {{function pointer argument to '__builtin_assume_aligned' is not allowed}}
}
-int val(int x) {
- return x;
+void foo();
+
+void test16(void (*f)()) {
+ f = (void (*)())__builtin_assume_aligned(foo, 32); // expected-error {{function pointer argument to '__builtin_assume_aligned' is not allowed}}
}
-int test16(int *b) {
- b = (int *)__builtin_assume_aligned(val, 32);
+int test17(int *b) {
+ int arr[3] = {1, 2, 3};
+ b = (int *)__builtin_assume_aligned(arr, 32);
return b[0];
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/153552
More information about the cfe-commits
mailing list