[PATCH] D23493: Fix PR28366: Teach the const-expression evaluator to be more fault tolerant with non-const enclosing local variables, or otherwise fold them if const.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 15 13:19:03 PDT 2016


rsmith added inline comments.

================
Comment at: lib/AST/ExprConstant.cpp:4791-4802
@@ -4790,1 +4790,14 @@
 
+CallStackFrame *getNearestContainingCallFrame(CallStackFrame *CurFrame,
+                                              const VarDecl *VD) {
+  if (auto *FD =
+            dyn_cast<FunctionDecl>(VD->getDeclContext()->getRedeclContext())) {
+    FD = FD->getFirstDecl();
+    for (CallStackFrame *It = CurFrame; It; It = It->Caller) {
+      if (It->Callee && FD == It->Callee->getFirstDecl())
+        return It;
+    }
+  }
+  return nullptr;
+}
+
----------------
This doesn't seem right to me: in the case where the variable is not in the expected frame, there seems to be no reason to expect it would be in a caller's frame rather than in some unrelated place, nor to special-case that situation. (And this does the wrong thing for constexpr lambdas, where the captured variable need not be in the *innermost* call frame for the function in which the variable was declared.)

Instead, how about changing the `VD->hasLocalStorage() && ...` condition below to also check that the `DeclContext` of the `VarDecl` is the callee of the current call, and leave `Frame` null otherwise.


Repository:
  rL LLVM

https://reviews.llvm.org/D23493





More information about the cfe-commits mailing list