https://github.com/AmrDeveloper updated 
https://github.com/llvm/llvm-project/pull/166055

>From 8173191f053c4eb6d60c81a69682489fd50ff5d7 Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Sun, 2 Nov 2025 12:00:40 +0100
Subject: [PATCH 1/2] [clang] Report Diagnostic when builtin vector has
 negative size

---
 clang/docs/ReleaseNotes.rst                   |  3 +++
 .../clang/Basic/DiagnosticSemaKinds.td        |  2 ++
 clang/lib/Sema/SemaType.cpp                   | 24 ++++++++++++++++---
 clang/test/SemaCXX/vector.cpp                 |  5 ++++
 4 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 92fc9381a5868..f3a8367ad16e5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -390,6 +390,9 @@ Improvements to Clang's diagnostics
   that were previously incorrectly accepted in case of other irrelevant
   conditions are now consistently diagnosed, identical to C++ mode.
 
+- Clang now emits a diagnostic in case `vector_size` or `ext_vector_type`
+  attributes are used with a negative size (#GH165463).
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 4e369be0bbb92..fa509536bf021 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3510,6 +3510,8 @@ def err_init_method_bad_return_type : Error<
   "init methods must return an object pointer type, not %0">;
 def err_attribute_invalid_size : Error<
   "vector size not an integral multiple of component size">;
+def err_attribute_vec_negative_size
+    : Error<"vector must have non-negative size">;
 def err_attribute_zero_size : Error<"zero %0 size">;
 def err_attribute_size_too_large : Error<"%0 size too large">;
 def err_typecheck_sve_rvv_ambiguous : Error<
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 280b3c92cce14..3345ed0dbbc0f 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -8285,10 +8285,20 @@ static void HandleVectorSizeAttr(QualType &CurType, 
const ParsedAttr &Attr,
 
   Expr *SizeExpr = Attr.getArgAsExpr(0);
   QualType T = S.BuildVectorType(CurType, SizeExpr, Attr.getLoc());
-  if (!T.isNull())
-    CurType = T;
-  else
+  if (T.isNull()) {
+    Attr.setInvalid();
+    return;
+  }
+
+  std::optional<llvm::APSInt> VecSize =
+      SizeExpr->getIntegerConstantExpr(S.Context);
+  if (VecSize && VecSize->isNegative()) {
+    S.Diag(SizeExpr->getExprLoc(), diag::err_attribute_vec_negative_size);
     Attr.setInvalid();
+    return;
+  }
+
+  CurType = T;
 }
 
 /// Process the OpenCL-like ext_vector_type attribute when it occurs on
@@ -8306,6 +8316,14 @@ static void HandleExtVectorTypeAttr(QualType &CurType, 
const ParsedAttr &Attr,
   QualType T = S.BuildExtVectorType(CurType, SizeExpr, Attr.getLoc());
   if (!T.isNull())
     CurType = T;
+
+  std::optional<llvm::APSInt> VecSize =
+      SizeExpr->getIntegerConstantExpr(S.Context);
+  if (VecSize && VecSize->isNegative()) {
+    S.Diag(SizeExpr->getExprLoc(), diag::err_attribute_vec_negative_size);
+    Attr.setInvalid();
+    return;
+  }
 }
 
 static bool isPermittedNeonBaseType(QualType &Ty, VectorKind VecKind, Sema &S) 
{
diff --git a/clang/test/SemaCXX/vector.cpp b/clang/test/SemaCXX/vector.cpp
index 808bdb679b09c..eea0b4756fae3 100644
--- a/clang/test/SemaCXX/vector.cpp
+++ b/clang/test/SemaCXX/vector.cpp
@@ -786,3 +786,8 @@ const long long e = *0; // expected-error {{indirection 
requires pointer operand
 double f = a - e;       // expected-error {{cannot initialize a variable of 
type 'double' with an rvalue of type '__attribute__((__vector_size__(1 * 
sizeof(double)))) double' (vector of 1 'double' value)}}
 int h = c - e;          // expected-error {{cannot initialize a variable of 
type 'int' with an rvalue of type '__attribute__((__vector_size__(1 * 
sizeof(long)))) long' (vector of 1 'long' value)}}
 }
+
+typedef int v_neg_size __attribute__((vector_size(-8))); // 
expected-error{{vector must have non-negative size}}
+typedef int v_neg_size_2 __attribute__((vector_size(-1 * 8))); // 
expected-error{{vector must have non-negative size}}
+typedef int v_ext_neg_size __attribute__((ext_vector_type(-8))); // 
expected-error{{vector must have non-negative size}}
+typedef int v_ext_neg_size2 __attribute__((ext_vector_type(-1 * 8))); // 
expected-error{{vector must have non-negative size}}

>From 98f492394472298bdfd3534da67ca295697fd7bd Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Mon, 3 Nov 2025 18:24:58 +0100
Subject: [PATCH 2/2] Address code review comments

---
 clang/lib/Sema/SemaType.cpp   | 34 +++++++++++++---------------------
 clang/test/SemaCXX/vector.cpp |  8 ++++++++
 2 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 3345ed0dbbc0f..682fd258eccf2 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -2358,6 +2358,11 @@ QualType Sema::BuildVectorType(QualType CurType, Expr 
*SizeExpr,
     return QualType();
   }
 
+  if (VecSize->isNegative()) {
+    Diag(SizeExpr->getExprLoc(), diag::err_attribute_vec_negative_size);
+    return QualType();
+  }
+
   if (CurType->isDependentType())
     return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc,
                                           VectorKind::Generic);
@@ -2427,6 +2432,11 @@ QualType Sema::BuildExtVectorType(QualType T, Expr 
*ArraySize,
       return QualType();
     }
 
+    if (vecSize->isNegative()) {
+      Diag(ArraySize->getExprLoc(), diag::err_attribute_vec_negative_size);
+      return QualType();
+    }
+
     if (!vecSize->isIntN(32)) {
       Diag(AttrLoc, diag::err_attribute_size_too_large)
           << ArraySize->getSourceRange() << "vector";
@@ -8285,20 +8295,10 @@ static void HandleVectorSizeAttr(QualType &CurType, 
const ParsedAttr &Attr,
 
   Expr *SizeExpr = Attr.getArgAsExpr(0);
   QualType T = S.BuildVectorType(CurType, SizeExpr, Attr.getLoc());
-  if (T.isNull()) {
-    Attr.setInvalid();
-    return;
-  }
-
-  std::optional<llvm::APSInt> VecSize =
-      SizeExpr->getIntegerConstantExpr(S.Context);
-  if (VecSize && VecSize->isNegative()) {
-    S.Diag(SizeExpr->getExprLoc(), diag::err_attribute_vec_negative_size);
+  if (!T.isNull())
+    CurType = T;
+  else
     Attr.setInvalid();
-    return;
-  }
-
-  CurType = T;
 }
 
 /// Process the OpenCL-like ext_vector_type attribute when it occurs on
@@ -8316,14 +8316,6 @@ static void HandleExtVectorTypeAttr(QualType &CurType, 
const ParsedAttr &Attr,
   QualType T = S.BuildExtVectorType(CurType, SizeExpr, Attr.getLoc());
   if (!T.isNull())
     CurType = T;
-
-  std::optional<llvm::APSInt> VecSize =
-      SizeExpr->getIntegerConstantExpr(S.Context);
-  if (VecSize && VecSize->isNegative()) {
-    S.Diag(SizeExpr->getExprLoc(), diag::err_attribute_vec_negative_size);
-    Attr.setInvalid();
-    return;
-  }
 }
 
 static bool isPermittedNeonBaseType(QualType &Ty, VectorKind VecKind, Sema &S) 
{
diff --git a/clang/test/SemaCXX/vector.cpp b/clang/test/SemaCXX/vector.cpp
index eea0b4756fae3..06195f039cd92 100644
--- a/clang/test/SemaCXX/vector.cpp
+++ b/clang/test/SemaCXX/vector.cpp
@@ -791,3 +791,11 @@ typedef int v_neg_size __attribute__((vector_size(-8))); 
// expected-error{{vect
 typedef int v_neg_size_2 __attribute__((vector_size(-1 * 8))); // 
expected-error{{vector must have non-negative size}}
 typedef int v_ext_neg_size __attribute__((ext_vector_type(-8))); // 
expected-error{{vector must have non-negative size}}
 typedef int v_ext_neg_size2 __attribute__((ext_vector_type(-1 * 8))); // 
expected-error{{vector must have non-negative size}}
+
+
+#if __cplusplus >= 201103L
+
+template <int N> using templated_v_size = int  
__attribute__((vector_size(N))); // expected-error{{vector must have 
non-negative size}}
+templated_v_size<-8> templated_v_neg_size; //expected-note{{in instantiation 
of template type alias 'templated_v_size' requested here}}
+
+#endif

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

Reply via email to