[clang] [clang] Reject enum element types in ext_vector_type (PR #228863)

via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 4 02:03:24 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Chen Miao (ChenMiaoi)

<details>
<summary>Changes</summary>

`BuildExtVectorType` checks element types with `isIntegerType()`, which also accepts complete unscoped enums. However,
`ASTContext::getExtVectorType` does not support enum elements.

For example:

```c
typedef enum { A } E;
typedef E V __attribute__((ext_vector_type(4)));
```

Before this change, compiling with `-fsyntax-only` in an assertion-enabled build crashes in `ASTContext::getExtVectorType`:

```text
clang: clang/lib/AST/ASTContext.cpp:4839:
QualType clang::ASTContext::getExtVectorType(QualType, unsigned int) const:
Assertion `vecType->isBuiltinType() || vecType->isDependentType() ||
(vecType->isBitIntType() && llvm::isPowerOf2_32(vecType->castAs<BitIntType>()->getNumBits()))' failed.
```

Explicitly reject enum element types in `BuildExtVectorType` so that the same code produces a diagnostic instead:

```text
error: invalid vector element type 'E'
```

Fixes #<!-- -->225037

---
Full diff: https://github.com/llvm/llvm-project/pull/228863.diff


3 Files Affected:

- (modified) clang/lib/Sema/SemaType.cpp (+1-1) 
- (modified) clang/test/Sema/types.c (+5) 
- (modified) clang/test/SemaCXX/vector.cpp (+28) 


``````````diff
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 037583575d19f..01b9ea18d5705 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -2470,7 +2470,7 @@ QualType Sema::BuildExtVectorType(QualType T, Expr *SizeExpr,
   bool IsNoBoolVecLang = getLangOpts().OpenCL || getLangOpts().OpenCLCPlusPlus;
   if ((!T->isDependentType() && !T->isIntegerType() &&
        !T->isRealFloatingType()) ||
-      (IsNoBoolVecLang && T->isBooleanType())) {
+      T->isEnumeralType() || (IsNoBoolVecLang && T->isBooleanType())) {
     Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
     return QualType();
   }
diff --git a/clang/test/Sema/types.c b/clang/test/Sema/types.c
index 858e2433b5629..bf495e655f8ef 100644
--- a/clang/test/Sema/types.c
+++ b/clang/test/Sema/types.c
@@ -90,6 +90,11 @@ int __attribute__((vector_size(0x10000001))) v7; // expected-error {{vector size
 // no support for vector enum type
 enum { e_2 } x3 __attribute__((vector_size(64))); // expected-error {{invalid vector element type}}
 
+// GH225037: reject enum elements before constructing an extended vector type.
+typedef enum { enum_value } EnumElement;
+typedef EnumElement EnumVector __attribute__((ext_vector_type(4))); // expected-error {{invalid vector element type 'EnumElement'}}
+typedef enum e TaggedEnumVector __attribute__((ext_vector_type(4))); // expected-error {{invalid vector element type 'enum e'}}
+
 int x4 __attribute__((ext_vector_type(64)));
 
 typedef __attribute__ ((ext_vector_type(32),__aligned__(32))) unsigned char uchar32;
diff --git a/clang/test/SemaCXX/vector.cpp b/clang/test/SemaCXX/vector.cpp
index 581d829560ea6..0eb053229a59e 100644
--- a/clang/test/SemaCXX/vector.cpp
+++ b/clang/test/SemaCXX/vector.cpp
@@ -336,6 +336,34 @@ const int &reference_to_vec_element = vi4(1).x;
 // PR12649
 typedef bool bad __attribute__((__vector_size__(16)));  // expected-error {{invalid vector element type 'bool'}}
 
+namespace GH225037 {
+enum E { A };
+typedef E EnumVector __attribute__((ext_vector_type(4))); // expected-error {{invalid vector element type 'E'}}
+
+#if __cplusplus >= 201103L
+enum class ScopedE { A };
+typedef ScopedE ScopedEnumVector __attribute__((ext_vector_type(4))); // expected-error {{invalid vector element type 'ScopedE'}}
+#endif
+
+template <typename T> struct Vector {
+  // Rebuilding a fixed-size extended vector currently loses the attribute location.
+  typedef T type __attribute__((ext_vector_type(4))); // expected-error@*:* {{invalid vector element type 'GH225037::E'}}
+};
+Vector<E> enum_vector; // expected-note {{in instantiation of template class 'GH225037::Vector<GH225037::E>' requested here}}
+Vector<int> int_vector;
+Vector<bool> bool_vector;
+
+template <typename T, int N> struct DependentSizeVector {
+  typedef T type __attribute__((ext_vector_type(N))); // expected-error {{invalid vector element type 'GH225037::E'}}
+};
+DependentSizeVector<E, 4> dependent_enum_vector; // expected-note {{in instantiation of template class 'GH225037::DependentSizeVector<GH225037::E, 4>' requested here}}
+DependentSizeVector<float, 4> float_vector;
+
+template <int N> struct EnumVectorWithDependentSize {
+  typedef E type __attribute__((ext_vector_type(N))); // expected-error {{invalid vector element type 'E'}}
+};
+} // namespace GH225037
+
 namespace Templates {
 template <typename Elt, unsigned long long Size>
 struct TemplateVectorType {

``````````

</details>


https://github.com/llvm/llvm-project/pull/228863


More information about the cfe-commits mailing list