aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
authorSarah Spall <sarahspall@microsoft.com>2025-03-11 07:12:18 -0700
committerGitHub <noreply@github.com>2025-03-11 07:12:18 -0700
commitf1e36759d2e6c26d2d5825f955c51fd595909b52 (patch)
tree8075d50ab449a1fb2faaa1f4771c21812d130254 /clang/lib/Sema/SemaChecking.cpp
parente858b10917046b83234bf1931485df414fcded3c (diff)
downloadllvm-f1e36759d2e6c26d2d5825f955c51fd595909b52.zip
llvm-f1e36759d2e6c26d2d5825f955c51fd595909b52.tar.gz
llvm-f1e36759d2e6c26d2d5825f955c51fd595909b52.tar.bz2
[HLSL] error on out of bounds vector accesses (#128952)
Add Sema checking and diagnostics to error on out of bounds vector accesses Add tests Closes #91640 --------- Co-authored-by: Chris B <beanz@abolishcrlf.org> Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r--clang/lib/Sema/SemaChecking.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index a6cdbbaf..89932b5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14054,6 +14054,23 @@ void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
<< TRange << Op->getSourceRange();
}
+void Sema::CheckVectorAccess(const Expr *BaseExpr, const Expr *IndexExpr) {
+ const auto *VTy = BaseExpr->getType()->getAs<VectorType>();
+ if (!VTy)
+ return;
+
+ Expr::EvalResult Result;
+ if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects))
+ return;
+
+ unsigned DiagID = diag::err_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) {
@@ -14068,6 +14085,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());