[clang] [HLSL] Diagnose dynamic indexing of struct arrays for resource access (PR #187132)

Joshua Batista via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 3 10:26:27 PDT 2026


================
@@ -3068,6 +3068,50 @@ void SemaHLSL::ActOnEndOfTranslationUnit(TranslationUnitDecl *TU) {
   diagnoseAvailabilityViolations(TU);
 }
 
+// For resource member access through a global struct array, verify that the
+// array index selecting the struct element is a constant integer expression.
+// Returns false if the member expression is invalid.
+bool SemaHLSL::ActOnResourceMemberAccessExpr(MemberExpr *ME) {
+  assert((ME->getType()->isHLSLResourceRecord() ||
+          ME->getType()->isHLSLResourceRecordArray()) &&
+         "expected member expr to have resource record type or array of them");
+
+  // Walk the AST from MemberExpr to the VarDecl of the parent struct instance
+  // and take note of any non-constant array indexing along the way. If the
+  // VarDecl we find is a global variable, report error if there was any
+  // non-constant array index in the resource member access along the way.
+  const Expr *NonConstIndexExpr = nullptr;
+  const Expr *E = ME->getBase();
+  while (E) {
+    if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
+      if (!NonConstIndexExpr)
----------------
bob80905 wrote:

Understood, but is it ever possible for DeclRefExpr to appear first? 
My comment is that if we encounter a DRE immediately, then we are bound to immediately return true, regardless of whether there is a nonconstindexexpr, since it's been just set to nullptr and there's no opportunity to reset it.

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


More information about the cfe-commits mailing list