Fix the following Bug:
https://llvm.org/bugs/show_bug.cgi?id=28366
This patch teaches the constant expression evaluator to search the frame stack for a local variable (instead of assuming a local variable is on the current frame).
This has the following benefits:
- if the local variable from an enclosing scope was an error during parsing/instantiation - it remains an error (instead of a compiler crash) during constant expression evaluation
- a local variable that is a constant expression from an enclosing scope is now evaluatable
Additionally, fix SemaTemplateInstantiateDecl so that when it instantiates the enable_if attribute it uses the instantiated declaration (instead of the template) when evaluating the condition (so that the instantiated param's decl context is correctly found). This was done to fix the following regression:
template <typename T> T templatedBar(T m) attribute((enable_if(m > 0, ""))) { return T(); }
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.