[clang] [clang][Sema] Handle alloc_align on all HasFunctionProto declarations (PR #210871)
Mimis Chlympatsos via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 20 21:08:57 PDT 2026
https://github.com/mimischly7 created https://github.com/llvm/llvm-project/pull/210871
AllocAlign's TableGen subject accepts any declaration satisfying `HasFunctionProto`, but `AddAllocAlignAttr` unconditionally casts the declaration to `FunctionDecl`. This asserts for recovery `FieldDecls` (seen for example in including the glibc header in the reproducer from the issue), and for valid declarations such as func pointers and Objective C methods.
Fixes #122058.
>From be2659958856b404d4c1272eee341205197eb65c Mon Sep 17 00:00:00 2001
From: Mimis Chlympatsos <mimischly at MIMIS-MAC-120.local>
Date: Mon, 20 Jul 2026 23:55:59 -0400
Subject: [PATCH] [clang][Sema] Handle alloc_align on all HasFunctionProto
declarations
---
clang/lib/Sema/SemaDeclAttr.cpp | 5 ++---
clang/test/CodeGen/xfail-alloc-align-fn-pointers.cpp | 10 ----------
clang/test/Sema/alloc-align-attr.c | 5 +++++
clang/test/SemaCXX/alloc-align-attr.cpp | 7 +++++++
clang/test/SemaObjC/alloc-align-attr.m | 8 ++++++++
5 files changed, 22 insertions(+), 13 deletions(-)
delete mode 100644 clang/test/CodeGen/xfail-alloc-align-fn-pointers.cpp
create mode 100644 clang/test/SemaObjC/alloc-align-attr.m
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 1b272b5416860..2716b5cf6528e 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1509,8 +1509,7 @@ void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
}
ParamIdx Idx;
- const auto *FuncDecl = cast<FunctionDecl>(D);
- if (!checkFunctionOrMethodParameterIndex(FuncDecl, CI,
+ if (!checkFunctionOrMethodParameterIndex(D, CI,
/*AttrArgNum=*/1, ParamExpr, Idx))
return;
@@ -1518,7 +1517,7 @@ void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
if (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
!Ty->isAlignValT()) {
Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
- << CI << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
+ << CI << getFunctionOrMethodParamRange(D, Idx.getASTIndex());
return;
}
diff --git a/clang/test/CodeGen/xfail-alloc-align-fn-pointers.cpp b/clang/test/CodeGen/xfail-alloc-align-fn-pointers.cpp
deleted file mode 100644
index 80067500284b1..0000000000000
--- a/clang/test/CodeGen/xfail-alloc-align-fn-pointers.cpp
+++ /dev/null
@@ -1,10 +0,0 @@
-
-// RUN: %clang_cc1 %s
-
-// FIXME: These should not crash!
-// XFAIL: *
-
-void aa_fn_ptr(char* (*member)(char*) __attribute__((alloc_align(1))));
-
-struct Test;
-void aa_member_fn_ptr(char* (Test::*member)(char*) __attribute__((alloc_align(1))));
diff --git a/clang/test/Sema/alloc-align-attr.c b/clang/test/Sema/alloc-align-attr.c
index 377c4387814d1..ac1df240d2c72 100644
--- a/clang/test/Sema/alloc-align-attr.c
+++ b/clang/test/Sema/alloc-align-attr.c
@@ -3,8 +3,13 @@
// return values
void test_void_alloc_align(void) __attribute__((alloc_align(1))); // expected-warning {{'alloc_align' attribute only applies to return values that are pointers}}
void *test_ptr_alloc_align(unsigned long long a) __attribute__((alloc_align(1))); // no-warning
+void *(*test_fn_ptr_alloc_align)(unsigned long long) __attribute__((alloc_align(1))); // no-warning
int j __attribute__((alloc_align(1))); // expected-warning {{'alloc_align' attribute only applies to non-K&R-style functions}}
+// GH122058
+struct InvalidFunctionField {
+ void *f(unsigned long long) __attribute__((alloc_align(1))); // expected-error {{field 'f' declared as a function}}
+};
void *test_no_params_zero(void) __attribute__((alloc_align(0))); // expected-error {{'alloc_align' attribute parameter 1 is out of bounds}}
void *test_no_params(void) __attribute__((alloc_align(1))); // expected-error {{'alloc_align' attribute parameter 1 is out of bounds}}
void *test_incorrect_param_type(float a) __attribute__((alloc_align(1))); // expected-error {{'alloc_align' attribute argument may only refer to a function parameter of integer type}}
diff --git a/clang/test/SemaCXX/alloc-align-attr.cpp b/clang/test/SemaCXX/alloc-align-attr.cpp
index ccc75369b8cfb..1cefa26fb076f 100644
--- a/clang/test/SemaCXX/alloc-align-attr.cpp
+++ b/clang/test/SemaCXX/alloc-align-attr.cpp
@@ -59,3 +59,10 @@ void foo() {
f<int>(0); // expected-note {{in instantiation of function template specialization 'GH26612::f<int>' requested here}}
}
} // namespace GH26612
+
+void test_function_pointer(
+ char *(*member)(char *) __attribute__((alloc_align(1)))); // expected-error {{'alloc_align' attribute argument may only refer to a function parameter of integer type}}
+
+struct Test;
+void test_member_function_pointer(
+ char *(Test::*member)(char *) __attribute__((alloc_align(1)))); // expected-error {{'alloc_align' attribute argument may only refer to a function parameter of integer type}}
diff --git a/clang/test/SemaObjC/alloc-align-attr.m b/clang/test/SemaObjC/alloc-align-attr.m
new file mode 100644
index 0000000000000..4180774c6e19d
--- /dev/null
+++ b/clang/test/SemaObjC/alloc-align-attr.m
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+ at interface AllocAlignMethod
+- (void *)allocate:(unsigned long)alignment
+ __attribute__((alloc_align(1)));
+ at end
More information about the cfe-commits
mailing list