Check whether a variable has scope at an arbitrary Source Location using clang llvm
<p>I am a newbie in clang libtooling and am struggling with the following problem.</p>

<p>I want to perform source-to-source transformation of a given c/c++ program so that 'transformed' program prints all the 'in scope' variable values at different program points.</p>

<p>For Example, In the below program I want to print variable values that have scope at various SourceLocation (for example at ending of line numbered 8 and 10).  For this i need to know that variables c doesn't have scope at the end of line 8, similarly variable i and c don't have scope at the end of line 10.</p>

<p>Is there any API that can help finding if a variable has scope at a given SourceLocation? How do I check if a variable has scope at a particular SourceLocation in the program?</p>

<pre><code> 1:void f(){
 2:    int a=10;
 3:    for(int i=0; i&lt;10;i++){
 4:        if(a&gt;10){
 5:            int b = a+20;
 6:            {
 7:                int c = b+10;
 8:            }
 9:        }
10:    }
11:}
</code></pre>

<p>The transformed program(for this example) will look like this:  </p>

<pre><code> 1:void f(){
 2:    int a=10;
 3:    for(int i=0; i&lt;10;i++){
 4:        if(a&gt;10){
 5:            int b = a+20;
 6:            {
 7:                int c = b+10;
 8:            };printf("%d %d %d\n", a, b, i);
 9:        }
10:    };printf("%d\n", a);
11:}
</code></pre>

<p>I thought of implementing it, but a huge tool like clang should have implemented this API already!! or should be having some efficient/easy way to do this.</p>

<p>What is the best strategy to do this using clang-libtooling?</p>

<p>Thanks.</p>