<div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;"><div><div class="im"><blockquote type="cite">
<div>+              // The variable is declared in the function scope which we are <br>+              // leaving. Keeping this variable's address in a global variable<br>+              // is dangerous.<br>+              // FIXME: Currently VarRegion does not carry context information.<br>
+              // So we cannot tell if the local variable instance is in the<br>+              // current stack frame.</div></blockquote><div><br></div></div><div>I don't think this is true.  I think you can look at the memory space of the VarRegion.  Is that not the case?</div>
<br></div></div></blockquote><div><br>From the memory space region of a VarRegion, we can only know if it's on the stack or global. We can't know which CallStackFrame it is in. Consider code:<br><br>int *p;<br><br>
void foo(int x) {<br>  if (x == 1) {<br>    p = &x;<br>    foo(2);<br>  }<br>}<br><br>void bar(void) {<br>  foo(1);<br>}<br><br>bar calls foo[1], foo calls foo[2]. When we leave the inner most call of foo[2], the value of 'p' is still valid. We should only emit one warning for this example. But now we emit two.<br>
<br></div></div>$ clang -cc1 -analyze -analyzer-check-objc-mem -analyzer-store region -analyze-function bar -analyzer-inline-call -analyzer-experimental-internal-checks stack2.c <br>stack2.c:6:5: warning: Stack address was saved into a global variable. This is dangerous because the address will become invalid after returning from the function.<br>
    foo(2);<br>    ^~~~~~<br>stack2.c:4:3: warning: Stack address was saved into a global variable. This is dangerous because the address will become invalid after returning from the function.<br>  if (x == 1) {<br>  ^<br>
2 warnings generated.<br><br>This also explains your questions above. I can't get the LocationContext from MemRegionVal. I can only get their enclosing DeclContext and match the DeclContext with the Decl of the LocationContext of the current program point.<br>