Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] indexer? hierarchy? querying problem

> What I have in its simplest form is this as source code to be parsed 
> void setup() 
> { 
>      Serial.begin(123); 
> } 
>  
> What I need to get as result for this case is 123. 
>  
> I didn't get closer than this code 
> String variableName = "Serial"; 
>   IIndexBinding[] bindings =  
> index.findBindings(variableName.toCharArray(),  
> IndexFilter.ALL_DECLARED_OR_IMPLICIT, new NullProgressMonitor()); 
> When variableName is "Serial" or "setup" I get a list of bindings I can  
> inspect; but how do I get this for "Serial.begin"? 

Generally speaking, the index only stores information about declarations.
It doesn't store things like information about a function call expression
occuring inside a function body.

To get this type of information, you need to get an AST for the file that
contains the code. You can get one with ITranslationUnit.getAST().

Once you have an AST, there are several ways you can get the information
you want. I'll describe one way here:
  - Use IASTTranslationUnit.getScope() get the global scope
  - Assuming "Serial" is of some type "SerialT" which is defined in the 
    global scope, use IScope.find(String, IASTTranslationUnit) to find
    the binding representing this type. It should implement ICPPClassType.
  - Use ICPPClassType.getMethods() to get a list of methods of this type.
     Iterate through the list to find the one named "begin".
  - Use IASTTranslationUnit.getReferences() on the ICPPMethod representing
    the 'begin' method to find all the places where it's called.
  - Loop through the references, and examine the surrounding AST to
     find the value you need. In your case:
       - the reference to 'begin' will be an IASTName
       - its parent should be an IASTFieldReference representing 'Serial.begin'
       - its parent should be an IASTFunctionCallExpression representing
         'Serial.begin(123)'
       - the function call expression will have an argument expression of type
          IASTLiteralExpression
       - IASTLiteralExpression.getValue() will give you the '123'

Hope that helps,
Nate
     
 		 	   		  

Back to the top