[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 06:22:59 PDT 2026


https://github.com/StepfenShawn created https://github.com/llvm/llvm-project/pull/192650

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”)

>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/4] 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/4] 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/4] 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/4] 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) {



More information about the cfe-commits mailing list