https://github.com/Aadarsh-Keshri created 
https://github.com/llvm/llvm-project/pull/181169

### Summary

Fix a crash for _Countof(void) caused by incorrect extension handling in 
CheckExtensionTraitOperandType.

Fixes #180893 

>From 112655ff9a52df366e3cb7b3316435cf6a160842 Mon Sep 17 00:00:00 2001
From: Aadarsh-Keshri <[email protected]>
Date: Thu, 12 Feb 2026 20:48:12 +0530
Subject: [PATCH] [Clang] Fix crash for _Countof(void) by correcting extension
 handling

---
 clang/lib/Sema/SemaExpr.cpp | 31 ++++++++++++++++---------------
 clang/test/C/C2y/n3369.c    |  2 ++
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 233f9ff297608..1d16962cd9b5f 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -4225,24 +4225,25 @@ static bool CheckExtensionTraitOperandType(Sema &S, 
QualType T,
     return true;
 
   // C99 6.5.3.4p1:
-  if (T->isFunctionType() &&
-      (TraitKind == UETT_SizeOf || TraitKind == UETT_AlignOf ||
-       TraitKind == UETT_PreferredAlignOf)) {
+  if (TraitKind == UETT_SizeOf || TraitKind == UETT_AlignOf ||
+      TraitKind == UETT_PreferredAlignOf) {
+
     // sizeof(function)/alignof(function) is allowed as an extension.
-    S.Diag(Loc, diag::ext_sizeof_alignof_function_type)
-        << getTraitSpelling(TraitKind) << ArgRange;
-    return false;
-  }
+    if (T->isFunctionType()) {
+      S.Diag(Loc, diag::ext_sizeof_alignof_function_type)
+          << getTraitSpelling(TraitKind) << ArgRange;
+      return false;
+    }
 
-  // Allow sizeof(void)/alignof(void) as an extension, unless in OpenCL where
-  // this is an error (OpenCL v1.1 s6.3.k)
-  if (T->isVoidType()) {
-    unsigned DiagID = S.LangOpts.OpenCL ? diag::err_opencl_sizeof_alignof_type
-                                        : diag::ext_sizeof_alignof_void_type;
-    S.Diag(Loc, DiagID) << getTraitSpelling(TraitKind) << ArgRange;
-    return false;
+    // Allow sizeof(void)/alignof(void) as an extension, unless in OpenCL where
+    // this is an error (OpenCL v1.1 s6.3.k)
+    if (T->isVoidType()) {
+      unsigned DiagID = S.LangOpts.OpenCL ? 
diag::err_opencl_sizeof_alignof_type
+                                          : diag::ext_sizeof_alignof_void_type;
+      S.Diag(Loc, DiagID) << getTraitSpelling(TraitKind) << ArgRange;
+      return false;
+    }
   }
-
   return true;
 }
 
diff --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index dd3125709279d..eee723bbb33b7 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -35,6 +35,8 @@ void test_semantic_failures() {
   int non_array;
   (void)_Countof non_array;  // expected-error {{'_Countof' requires an 
argument of array type; 'int' invalid}}  
   (void)_Countof(int);       // expected-error {{'_Countof' requires an 
argument of array type; 'int' invalid}}  
+  (void)_Countof(void);      // expected-error {{invalid application of 
'_Countof' to an incomplete type 'void'}}
+  int arr2[_Countof(void)];  // expected-error {{invalid application of 
'_Countof' to an incomplete type 'void'}}
   (void)_Countof(test_semantic_failures); // expected-error {{invalid 
application of '_Countof' to a function type}}
   (void)_Countof(struct S);  // expected-error {{invalid application of 
'_Countof' to an incomplete type 'struct S'}} \
                                 expected-note {{forward declaration of 'struct 
S'}}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to