[clang] [HLSL] error on out of bounds vector accesses (PR #128952)

Chris B via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 27 07:07:12 PST 2025


================
@@ -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-beanz wrote:

I'd be curious for @aaronballman and @svenvh's feedback here. It seems to me like this warning should be enabled by default for all languages.

>From reading the OpenCL C spec I don't believe OpenCL supports subscript operators on vectors, but I certainly think that we would want to warn in C/C++ for compile-time provable out-of-bounds access.

We know this is an out-of-bounds access 100% of the time, so if this new warning suddenly breaks a lot of code (which I doubt), it will be breaking a lot of code that does something obviously wrong.

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


More information about the cfe-commits mailing list