https://github.com/spall updated 
https://github.com/llvm/llvm-project/pull/128952

>From 656d6e87466bfc85246f7abaedfe6549e566717b Mon Sep 17 00:00:00 2001
From: Sarah Spall <sarahsp...@microsoft.com>
Date: Wed, 26 Feb 2025 14:37:06 -0800
Subject: [PATCH 1/4] error on out of bounds vector accesses

---
 .../clang/Basic/DiagnosticSemaKinds.td        |  5 ++++
 clang/include/clang/Sema/Sema.h               |  1 +
 clang/lib/Sema/SemaChecking.cpp               | 24 +++++++++++++++++++
 .../Language/VectorOutOfRange-errors.hlsl     | 19 +++++++++++++++
 4 files changed, 49 insertions(+)
 create mode 100644 clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 51301d95e55b9..0989b4c05b86f 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10635,6 +10635,11 @@ def err_block_on_vm : Error<
 def err_sizeless_nonlocal : Error<
   "non-local variable with sizeless type %0">;
 
+def err_vector_index_out_of_range : Error<
+  "vector element index %0 is out of bounds">;
+def warn_vector_index_out_of_range : Warning<
+  "vector element index %0 is out of bounds">;
+
 def err_vec_builtin_non_vector : Error<
  "%select{first two|all}1 arguments to %0 must be vectors">;
 def err_vec_builtin_incompatible_vector : Error<
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 476abe86cb2d2..fad3cbddc555b 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -2468,6 +2468,7 @@ class Sema final : public SemaBase {
                         const ArraySubscriptExpr *ASE = nullptr,
                         bool AllowOnePastEnd = true, bool IndexNegated = 
false);
   void CheckArrayAccess(const Expr *E);
+  void CheckVectorAccess(const Expr *BaseExpr, const Expr *IndexExpr);
 
   bool CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
                         const FunctionProtoType *Proto);
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f9926c6b4adab..1cbf428f18366 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14017,6 +14017,24 @@ void Sema::CheckCastAlign(Expr *Op, QualType T, 
SourceRange TRange) {
     << TRange << Op->getSourceRange();
 }
 
+void Sema::CheckVectorAccess(const Expr *BaseExpr, const Expr *IndexExpr) {
+  const VectorType *VTy = BaseExpr->getType()->getAs<VectorType>();
+  if (!VTy)
+    return;
+
+  Expr::EvalResult Result;
+  if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects))
+    return;
+
+  unsigned DiagID = getLangOpts().HLSL ? diag::err_vector_index_out_of_range
+                                       : diag::warn_vector_index_out_of_range;
+
+  llvm::APSInt index = Result.Val.getInt();
+  if (index.isNegative() || index >= VTy->getNumElements())
+    Diag(BaseExpr->getBeginLoc(), DiagID) << toString(index, 10, true);
+  return;
+}
+
 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
                             const ArraySubscriptExpr *ASE,
                             bool AllowOnePastEnd, bool IndexNegated) {
@@ -14031,6 +14049,12 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, 
const Expr *IndexExpr,
   const Type *EffectiveType =
       BaseExpr->getType()->getPointeeOrArrayElementType();
   BaseExpr = BaseExpr->IgnoreParenCasts();
+
+  if (BaseExpr->getType()->isVectorType()) {
+    CheckVectorAccess(BaseExpr, IndexExpr);
+    return;
+  }
+
   const ConstantArrayType *ArrayTy =
       Context.getAsConstantArrayType(BaseExpr->getType());
 
diff --git a/clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl 
b/clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl
new file mode 100644
index 0000000000000..02b5693f4076c
--- /dev/null
+++ b/clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.6-library %s -verify
+
+export void fn1() {
+  int2 A = {1,2};
+  int X = A[-1];
+  // expected-error@-1 {{vector element index '-1' is out of bounds}}
+}
+
+export void fn2() {
+  int4 A = {1,2,3,4};
+  int X = A[4];
+  // expected-error@-1 {{vector element index '4' is out of bounds}}
+}
+
+export void fn3() {
+  bool2 A = {true,true};
+  bool X = A[-1];
+  // expected-error@-1 {{vector element index '-1' is out of bounds}}
+}

>From f44e697beb1dfa6a028a8015f3d9587968f3600c Mon Sep 17 00:00:00 2001
From: Sarah Spall <sarahsp...@microsoft.com>
Date: Wed, 26 Feb 2025 15:58:44 -0800
Subject: [PATCH 2/4] add warning flag for vector out of bounds warning. fix
 test

---
 clang/include/clang/Basic/DiagnosticGroups.td             | 1 +
 clang/include/clang/Basic/DiagnosticSemaKinds.td          | 3 ++-
 clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl | 6 +++---
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 05e39899e6f25..d36f6086edd40 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -930,6 +930,7 @@ def SuperSubClassMismatch : 
DiagGroup<"super-class-method-mismatch">;
 def OverridingMethodMismatch : DiagGroup<"overriding-method-mismatch">;
 def VariadicMacros : DiagGroup<"variadic-macros">;
 def VectorConversion : DiagGroup<"vector-conversion">;      // clang specific
+def VectorOutOfRange : DiagGroup<"vector-out-of-range">;
 def VexingParse : DiagGroup<"vexing-parse">;
 def VLAUseStaticAssert : DiagGroup<"vla-extension-static-assert">;
 def VLACxxExtension : DiagGroup<"vla-cxx-extension", [VLAUseStaticAssert]>;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0989b4c05b86f..7ff37406b459a 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10638,7 +10638,8 @@ def err_sizeless_nonlocal : Error<
 def err_vector_index_out_of_range : Error<
   "vector element index %0 is out of bounds">;
 def warn_vector_index_out_of_range : Warning<
-  "vector element index %0 is out of bounds">;
+  "vector element index %0 is out of bounds">,
+  InGroup<VectorOutOfRange>, DefaultIgnore;
 
 def err_vec_builtin_non_vector : Error<
  "%select{first two|all}1 arguments to %0 must be vectors">;
diff --git a/clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl 
b/clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl
index 02b5693f4076c..b4d423fb24bd2 100644
--- a/clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl
+++ b/clang/test/SemaHLSL/Language/VectorOutOfRange-errors.hlsl
@@ -3,17 +3,17 @@
 export void fn1() {
   int2 A = {1,2};
   int X = A[-1];
-  // expected-error@-1 {{vector element index '-1' is out of bounds}}
+  // expected-error@-1 {{vector element index -1 is out of bounds}}
 }
 
 export void fn2() {
   int4 A = {1,2,3,4};
   int X = A[4];
-  // expected-error@-1 {{vector element index '4' is out of bounds}}
+  // expected-error@-1 {{vector element index 4 is out of bounds}}
 }
 
 export void fn3() {
   bool2 A = {true,true};
   bool X = A[-1];
-  // expected-error@-1 {{vector element index '-1' is out of bounds}}
+  // expected-error@-1 {{vector element index -1 is out of bounds}}
 }

>From 8449ac1ed8f53c0cc744d89d6948b1e2deefae32 Mon Sep 17 00:00:00 2001
From: Sarah Spall <sp...@planetbauer.com>
Date: Thu, 27 Feb 2025 07:36:48 -0800
Subject: [PATCH 3/4] Update clang/include/clang/Basic/DiagnosticSemaKinds.td

Co-authored-by: Chris B <be...@abolishcrlf.org>
---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 7ff37406b459a..383aafc283603 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10638,7 +10638,7 @@ def err_sizeless_nonlocal : Error<
 def err_vector_index_out_of_range : Error<
   "vector element index %0 is out of bounds">;
 def warn_vector_index_out_of_range : Warning<
-  "vector element index %0 is out of bounds">,
+  err_vector_index_out_of_range.Summary>,
   InGroup<VectorOutOfRange>, DefaultIgnore;
 
 def err_vec_builtin_non_vector : Error<

>From 14f60af4389828b34a2dab948bf8ad9033180d25 Mon Sep 17 00:00:00 2001
From: Sarah Spall <sarahsp...@microsoft.com>
Date: Thu, 27 Feb 2025 15:05:08 -0800
Subject: [PATCH 4/4] make new warning not ignored by default

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 383aafc283603..2f19a3707cb5c 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10639,7 +10639,7 @@ def err_vector_index_out_of_range : Error<
   "vector element index %0 is out of bounds">;
 def warn_vector_index_out_of_range : Warning<
   err_vector_index_out_of_range.Summary>,
-  InGroup<VectorOutOfRange>, DefaultIgnore;
+  InGroup<VectorOutOfRange>;
 
 def err_vec_builtin_non_vector : Error<
  "%select{first two|all}1 arguments to %0 must be vectors">;

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to