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

Chen Miao via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 4 05:32:43 PDT 2026


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

>From 72c05eb80da5a65c4846cfd5a902fd7bee3cdea7 Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Sun, 4 Oct 2026 16:50:04 +0800
Subject: [PATCH 1/2] [clang] Reject enum element types in ext_vector_type

`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
---
 clang/lib/Sema/SemaType.cpp   |  2 +-
 clang/test/Sema/types.c       |  5 +++++
 clang/test/SemaCXX/vector.cpp | 28 ++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)

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 {

>From e6815973808c957bb5534fbafe6089795ed9f1e3 Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Sun, 4 Oct 2026 20:32:30 +0800
Subject: [PATCH 2/2] [clang] Trim redundant enum vector tests

---
 clang/test/SemaCXX/vector.cpp | 17 -----------------
 1 file changed, 17 deletions(-)

diff --git a/clang/test/SemaCXX/vector.cpp b/clang/test/SemaCXX/vector.cpp
index 0eb053229a59e..d5e106d9fd674 100644
--- a/clang/test/SemaCXX/vector.cpp
+++ b/clang/test/SemaCXX/vector.cpp
@@ -338,12 +338,6 @@ typedef bool bad __attribute__((__vector_size__(16)));  // expected-error {{inva
 
 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.
@@ -351,17 +345,6 @@ template <typename T> struct Vector {
 };
 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 {



More information about the cfe-commits mailing list