[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 07:07:47 PDT 2026
=?utf-8?b?8J+NjFNoYXdu?= <m18824909883 at 163.com>,
=?utf-8?b?8J+NjFNoYXdu?= <m18824909883 at 163.com>,
=?utf-8?b?8J+NjFNoYXdu?= <m18824909883 at 163.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/192650 at github.com>
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: 🍌Shawn (StepfenShawn)
<details>
<summary>Changes</summary>
Improve diagnostics for `__builtin_align_up`, `__builtin_align_down`, and `__builtin_is_aligned` when the first operand has an invalid type.
Clang already emits `err_typecheck_expect_scalar_operand` for unsupported operands, but the message is generic. This patch adds follow-up notes to clarify two common invalid cases:
* floating point operands (“floating point types are not allowed here”)
* C++ member pointer operands (“member pointers are not allowed here”)
---
Full diff: https://github.com/llvm/llvm-project/pull/192650.diff
4 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+4)
- (modified) clang/lib/Sema/SemaChecking.cpp (+4-2)
- (modified) clang/test/Sema/builtin-align.c (+4)
- (modified) clang/test/SemaCXX/builtin-align-cxx.cpp (+3-3)
``````````diff
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">;
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;
}
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}}
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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/192650
More information about the cfe-commits
mailing list