declRefExpr clang

Hi,
  I have a DeclRefExpr which tells me wherever a variable is used. I want to
find out the previous or the next ";" so that I can go to the start of the
command in which this variable was used e.g.

int a,b;

a = b + 3;
     > >

   In this case the DeclRefExpr for b will start and end at the locations
shown. How can I find the previous ";" (in this case at the end of the line
int a,b;))

Regards,
Adil

There isn't any existing code to handle this, but it isn't too hard:
find the CompoundStmt which contains the given expression, then get
the end location of previous statement in that CompoundStmt. (This is
ignoring various edge cases, but that's the fundamental algorithm.)
The ParentMap class might be useful for implementing this.

-Eli

How can I get the CompundStmt or the Stmt for this expression? Is there any way to move token by token forward from a SourceLocation?

How can I get the CompundStmt or the Stmt for this expression?

ParentMap (or some equivalent).

Is there any
way to move token by token forward from a SourceLocation?

No.

-Eli

Can you please give an example of how ParentMap can be used in this situation?

Hi,
There doesn’t seem to be any way to extract a ParentMap from DeclRefExpr, Expr or Stmt. How do I build a ParentMap for a DeclRefExpr to find it’s parent. I know that ParentMap can be extracted from LocationContext but there doesn’t seem to be any way to find the LocationContext either. There must be some way to do this because the static analyzer in clang does this. Please at least tell me if this is possible or not.

Regards,
Adil

You have to have gotten the DeclRefExpr from somewhere. A quick search for “ParentMap” shows that you can get one from an AnalysisDeclContext, which you can build trivially from the FunctionDecl (or whatever) containing your statement. Moreover, though, you can make your own ParentMap, constructing it with the top-level Stmt in your function.

Jordan