[PATCH] D108482: [Clang] Fixes instantiation of OpaqueValueExprs (Bug #45964)

Jason Rice via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 20 13:26:02 PDT 2021


ricejasonf created this revision.
ricejasonf added a reviewer: aaron.ballman.
ricejasonf added a project: clang.
ricejasonf requested review of this revision.
Herald added a subscriber: cfe-commits.

The structured bindings decomposition of a non-dependent array in a dependent context (a template) were, upon instantiation, creating nested OpaqueValueExprs that would trigger assertions in CodeGen. Additionally the OpaqueValuesExpr's contained SourceExpr is being emitted in CodeGen, but there was no code for its transform in template instantiation. This would trigger other assertions such as when emitting a DeclRefExpr that refers to a VarDecl that is not marked as ODR-used.

This is all based on cursory deduction, but with the way the code flows from SemaTemplateInstantiate back to SemaInit, it is apparent that the nesting of OpaqueValueExpr is unintentional.

This commit fixes https://bugs.llvm.org/show_bug.cgi?id=45964 and possible other issues involving OpaqueValueExprs in template instantiations might be resolved.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108482

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/lib/Sema/TreeTransform.h


Index: clang/lib/Sema/TreeTransform.h
===================================================================
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -10511,7 +10511,19 @@
 TreeTransform<Derived>::TransformOpaqueValueExpr(OpaqueValueExpr *E) {
   assert((!E->getSourceExpr() || getDerived().AlreadyTransformed(E->getType())) &&
          "opaque value expression requires transformation");
-  return E;
+
+  // Note that SourceExpr can be nullptr
+  ExprResult SourceExpr = TransformExpr(E->getSourceExpr());
+  if (SourceExpr.isInvalid()) return ExprError();
+  if (SourceExpr.get() == E->getSourceExpr() && !getDerived().AlwaysRebuild()) {
+    return E;
+  }
+
+  OpaqueValueExpr *New =
+    new (SemaRef.Context) OpaqueValueExpr(E->getExprLoc(), E->getType(),
+                                          E->getValueKind(), E->getObjectKind(),
+                                          SourceExpr.get());
+  return New;
 }
 
 template<typename Derived>
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -8643,9 +8643,13 @@
 
     case SK_ArrayLoopIndex: {
       Expr *Cur = CurInit.get();
-      Expr *BaseExpr = new (S.Context)
-          OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(),
-                          Cur->getValueKind(), Cur->getObjectKind(), Cur);
+      // prevent nested OpaqueValueExprs
+      Expr *BaseExpr = dyn_cast<OpaqueValueExpr>(Cur);
+      if (!BaseExpr) {
+        BaseExpr = new (S.Context)
+            OpaqueValueExpr(Cur->getExprLoc(), Cur->getType(),
+                            Cur->getValueKind(), Cur->getObjectKind(), Cur);
+      }
       Expr *IndexExpr =
           new (S.Context) ArrayInitIndexExpr(S.Context.getSizeType());
       CurInit = S.CreateBuiltinArraySubscriptExpr(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108482.367874.patch
Type: text/x-patch
Size: 1878 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210820/2012ba2d/attachment-0001.bin>


More information about the cfe-commits mailing list