[clang] [clang] Improve diagnostics for `__builtin_align` builtins with floating/member pointer operands (PR #192650)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 17 08:47:36 PDT 2026
https://github.com/StepfenShawn updated https://github.com/llvm/llvm-project/pull/192650
>From 5fa6a7a478969437faf73ffc5c27a3a27a6def9e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 21:13:35 +0800
Subject: [PATCH 1/9] Add notes for invalid alignment types and member pointers
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 96c87b2fecad8..45552e497c93e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -156,6 +156,10 @@ def err_ice_explicit_conversion : Error<
"integral constant expression requires explicit conversion from %0 to %1">;
def note_ice_conversion_here : Note<
"conversion to %select{integral|enumeration}0 type %1 declared here">;
+def note_alignment_invalid_type : Note<
+ "floating point types are not allowed here">;
+def note_alignment_invalid_member_pointer : Note<
+ "member pointers are not allowed here">;
def err_ice_ambiguous_conversion : Error<
"ambiguous conversion from type %0 to an integral or unscoped "
"enumeration type">;
>From c2eef704eb764f03bb9d4eb3c15956238502eeca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 21:15:01 +0800
Subject: [PATCH 2/9] Enhance type checking error diagnostics
Add specific notes for floating and member pointer types in type checking errors.
---
clang/lib/Sema/SemaChecking.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3f6e44b45a0a4..2ecefa19421ab 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -336,10 +336,12 @@ static bool BuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) {
}
if ((!SrcTy->isPointerType() && !IsValidIntegerType(SrcTy)) ||
SrcTy->isFunctionPointerType()) {
- // FIXME: this is not quite the right error message since we don't allow
- // floating point types, or member pointers.
S.Diag(Source->getExprLoc(), diag::err_typecheck_expect_scalar_operand)
<< SrcTy;
+ if (SrcTy->isFloatingType())
+ S.Diag(Source->getExprLoc(), diag::note_alignment_invalid_type);
+ else if (SrcTy->isMemberPointerType())
+ S.Diag(Source->getExprLoc(), diag::note_alignment_invalid_member_pointer);
return true;
}
>From 15106f1b12cae9b1fa80f364e7430ce5ee8ecc4d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 21:16:11 +0800
Subject: [PATCH 3/9] Enhance tests for ALIGN_BUILTIN with floating point cases
Added tests for floating point type errors in ALIGN_BUILTIN.
---
clang/test/Sema/builtin-align.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang/test/Sema/builtin-align.c b/clang/test/Sema/builtin-align.c
index bf9a89234fea5..97c30caf608de 100644
--- a/clang/test/Sema/builtin-align.c
+++ b/clang/test/Sema/builtin-align.c
@@ -25,6 +25,10 @@ void test_parameter_types(char *ptr, size_t size) {
(void)ALIGN_BUILTIN((int)e, 2); // but with a cast it is fine
(void)ALIGN_BUILTIN((int)b, 2); // but with a cast it is fine
+ // Floating point types are not allowed:
+ (void)ALIGN_BUILTIN(1.0, 4); // expected-error {{operand of type 'double' where arithmetic or pointer type is required}} expected-note {{floating point types are not allowed here}}
+ (void)ALIGN_BUILTIN(1.0f, 4); // expected-error {{operand of type 'float' where arithmetic or pointer type is required}} expected-note {{floating point types are not allowed here}}
+
// The second parameter must be an integer type (but not enum or _Bool):
(void)ALIGN_BUILTIN(ptr, size);
(void)ALIGN_BUILTIN(ptr, ptr); // expected-error {{used type 'char *' where integer is required}}
>From 2c13213beb231365dd3205b3ade5dd784035e7e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 21:17:58 +0800
Subject: [PATCH 4/9] Update error messages for member pointer alignment
builtins
---
clang/test/SemaCXX/builtin-align-cxx.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/test/SemaCXX/builtin-align-cxx.cpp b/clang/test/SemaCXX/builtin-align-cxx.cpp
index 806bb660e0042..213a285e23eb2 100644
--- a/clang/test/SemaCXX/builtin-align-cxx.cpp
+++ b/clang/test/SemaCXX/builtin-align-cxx.cpp
@@ -85,9 +85,9 @@ class MemPtr {
virtual void vfunc();
};
void test_member_ptr() {
- __builtin_align_up(&MemPtr::data, 64); // expected-error{{operand of type 'int MemPtr::*' where arithmetic or pointer type is required}}
- __builtin_align_down(&MemPtr::func, 64); // expected-error{{operand of type 'void (MemPtr::*)()' where arithmetic or pointer type is required}}
- __builtin_is_aligned(&MemPtr::vfunc, 64); // expected-error{{operand of type 'void (MemPtr::*)()' where arithmetic or pointer type is required}}
+ __builtin_align_up(&MemPtr::data, 64); // expected-error{{operand of type 'int MemPtr::*' where arithmetic or pointer type is required}} expected-note{{member pointers are not allowed here}}
+ __builtin_align_down(&MemPtr::func, 64); // expected-error{{operand of type 'void (MemPtr::*)()' where arithmetic or pointer type is required}} expected-note{{member pointers are not allowed here}}
+ __builtin_is_aligned(&MemPtr::vfunc, 64); // expected-error{{operand of type 'void (MemPtr::*)()' where arithmetic or pointer type is required}} expected-note{{member pointers are not allowed here}}
}
void test_references(Foo &i) {
>From 9151523c7fde2386d2351f71cbe47240d14c8b3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 23:32:50 +0800
Subject: [PATCH 5/9] Add note for invalid function pointer alignment
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 45552e497c93e..5841ec3c253a1 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -160,6 +160,8 @@ def note_alignment_invalid_type : Note<
"floating point types are not allowed here">;
def note_alignment_invalid_member_pointer : Note<
"member pointers are not allowed here">;
+def note_alignment_invalid_function_pointer : Note<
+ "function pointers are not allowed here">;
def err_ice_ambiguous_conversion : Error<
"ambiguous conversion from type %0 to an integral or unscoped "
"enumeration type">;
>From e62e9ef59032abe0d5df115beb3487c1ba84e876 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 23:33:56 +0800
Subject: [PATCH 6/9] Add diagnostic for invalid function pointer alignment
---
clang/lib/Sema/SemaChecking.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 2ecefa19421ab..423abcbac53b2 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -342,6 +342,8 @@ static bool BuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) {
S.Diag(Source->getExprLoc(), diag::note_alignment_invalid_type);
else if (SrcTy->isMemberPointerType())
S.Diag(Source->getExprLoc(), diag::note_alignment_invalid_member_pointer);
+ else if (SrcTy->isFunctionPointerType())
+ S.Diag(Source->getExprLoc(), diag::note_alignment_invalid_function_pointer);
return true;
}
>From f14bd4395d8a00d4579070f424778c08f3ed3165 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 23:37:32 +0800
Subject: [PATCH 7/9] Fix expected error message for function pointer alignment
---
clang/test/Sema/builtin-align.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/Sema/builtin-align.c b/clang/test/Sema/builtin-align.c
index 97c30caf608de..c33ad8d1ad0ef 100644
--- a/clang/test/Sema/builtin-align.c
+++ b/clang/test/Sema/builtin-align.c
@@ -133,5 +133,5 @@ char *test_array_and_fnptr(void) {
(void)(ALIGN_BUILTIN(buf, 16));
// But not on functions and function pointers:
(void)(ALIGN_BUILTIN(test_array_and_fnptr, 16)); // expected-error{{operand of type 'char *(void)' where arithmetic or pointer type is required}}
- (void)(ALIGN_BUILTIN(&test_array_and_fnptr, 16)); // expected-error{{operand of type 'char *(*)(void)' where arithmetic or pointer type is required}}
+ (void)(ALIGN_BUILTIN(&test_array_and_fnptr, 16)); // expected-error{{operand of type 'char *(*)(void)' where arithmetic or pointer type is required}} expected-note{{function pointers are not allowed here}}
}
>From 3bb988189206de7743915052000cbcdee98780a9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 23:42:53 +0800
Subject: [PATCH 8/9] Fix clang-format issue
---
clang/lib/Sema/SemaChecking.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 423abcbac53b2..3df56bcf86e01 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -343,7 +343,8 @@ static bool BuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) {
else if (SrcTy->isMemberPointerType())
S.Diag(Source->getExprLoc(), diag::note_alignment_invalid_member_pointer);
else if (SrcTy->isFunctionPointerType())
- S.Diag(Source->getExprLoc(), diag::note_alignment_invalid_function_pointer);
+ S.Diag(Source->getExprLoc(),
+ diag::note_alignment_invalid_function_pointer);
return true;
}
>From 423c4839d6600af1a1f53b0679d3a3fda1307f00 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=F0=9F=8D=8CShawn?= <m18824909883 at 163.com>
Date: Fri, 17 Apr 2026 23:47:22 +0800
Subject: [PATCH 9/9] Fix clang-format issue again
---
clang/lib/Sema/SemaChecking.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3df56bcf86e01..7caae4e66f5b7 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -343,7 +343,7 @@ static bool BuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) {
else if (SrcTy->isMemberPointerType())
S.Diag(Source->getExprLoc(), diag::note_alignment_invalid_member_pointer);
else if (SrcTy->isFunctionPointerType())
- S.Diag(Source->getExprLoc(),
+ S.Diag(Source->getExprLoc(),
diag::note_alignment_invalid_function_pointer);
return true;
}
More information about the cfe-commits
mailing list