[clang] [Clang] Fix crash for _Countof(void) by correcting extension handling (PR #181169)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 12 07:55:15 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Aadarsh Keshri (Aadarsh-Keshri)
<details>
<summary>Changes</summary>
### Summary
Fix a crash for _Countof(void) caused by incorrect extension handling in CheckExtensionTraitOperandType.
Fixes #<!-- -->180893
---
Full diff: https://github.com/llvm/llvm-project/pull/181169.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaExpr.cpp (+16-15)
- (modified) clang/test/C/C2y/n3369.c (+2)
``````````diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index fce803fcdd395..6f4a2219c666a 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'}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/181169
More information about the cfe-commits
mailing list