Local variable information in scope

Hi all,

Using “CodeGen/LexicalScopes” pass, I could collect scope information i.e. start and end line numbers.

I also want to collect information of variables within the scope.

For the below example:

int global;
int func( int t)
{
//scope 1
{
int a;

}
return x;
}

For “scope 1”, I should be able to collect that “a” is a local variable in scope1 and should also be able to identify whether “a” is static or non-static.

Regards,
Pankaj

Hi,

I want to list some additional information on this.

The variable collection I am looking at is, “variables ‘declared’ in scope”.

  1. When I traverse the MachineInstructions in the LexicalScopes ranges, and check for variables, I get variables used in this scope.
    The variables listed include variables which may not have been declared in the scope. (for example “int t”, which is not declared in scope1, is listed.)
    I do this by checking if the instruction uses memoperands then I collect name of value.

So, for the example below
int func( int t)
{
int x;
//scope 1
{
int a;

}
return x;
}

what I am expecting is as below:
:
func scope: t, x
scope 1: a

  1. I checked the “CodeGen/AsmPrinter/DwarfDebug.cpp” as reference, to find mapping of “MachineInstruction” to “llvm.dbg.declare”, to extract variables declared in scope.

I am not sure whether there is any other way to get to variables declared in scope.

Regards,
Pankaj

With ToT clang I get output that looks something like this:

0x00000051: TAG_subprogram [2] *
AT_sibling( {0x000000be} )
AT_name( “func” )
AT_decl_file( “/Users/echristo/bar.c” )
AT_decl_line( 2 )
AT_prototyped( 0x01 )
AT_type( {0x000000be} ( int ) )
AT_external( 0x01 )
AT_low_pc( 0x0000000000000000 )
AT_high_pc( 0x0000000000000017 )
AT_frame_base( rbp )

0x00000075: TAG_formal_parameter [3]
AT_name( “t” )
AT_decl_file( “/Users/echristo/bar.c” )
AT_decl_line( 1 )
AT_type( {0x000000be} ( int ) )
AT_location( fbreg -4 )

0x00000081: TAG_lexical_block [4] *
AT_low_pc( 0x0000000000000007 )
AT_high_pc( 0x0000000000000017 )

0x00000092: TAG_variable [5]
AT_name( “x” )
AT_decl_file( “/Users/echristo/bar.c” )
AT_decl_line( 3 )
AT_type( {0x000000be} ( int ) )
AT_location( fbreg -8 )

0x0000009e: TAG_lexical_block [4] *
AT_low_pc( 0x0000000000000007 )
AT_high_pc( 0x0000000000000012 )

0x000000af: TAG_variable [5]
AT_name( “a” )
AT_decl_file( “/Users/echristo/bar.c” )
AT_decl_line( 6 )
AT_type( {0x000000be} ( int ) )
AT_location( fbreg -12 )

using your test case from below slightly modified to make sure that a was actually used.

Which appears to be exactly what you were looking for? And yes, it matches the dbg.declare instruction.

-eric