Statements that end with ;

Hi,
  I need to identify statements that end with ";". I mean, how does clang
represent a statement that is executed independently e.g.

int a; // what type of statement is this?

a = a + 2; // or this?

Regards,
Adil

madil90 wrote:

Hi,
  I need to identify statements that end with ";". I mean, how does clang
represent a statement that is executed independently e.g.
  
You can check all statement classes here:

http://clang.llvm.org/doxygen/classclang_1_1Stmt.html

int a; // what type of statement is this?
  
I assume you define ``a'' as a local variable, then ``int a;'' is a DeclStmt.

a = a + 2; // or this?
  
It's a BinaryOperator -

http://clang.llvm.org/doxygen/classclang_1_1BinaryOperator.html

- Yang

Hi,
I think I didn’t state my question correctly. I meant that is there some way to visit statements that end with “;” and are executed in one execution cycle. I just want to identify whether a statement started after a “;” and ended with a “;”. e.g.

int a;

a = a + 2;

I only want to visit these two lines. How can I tell these two statements apart from things like DeclRefExpr which need to have a parent statement that includes them (DeclRefExpr is not a statement type that can be executed)

Regards,
Adil

Hi,
I need to rewrite at the end of line. In order for the edited code to compile, I must ensure that I write after a “;” character. I just need to find that character. I have a ParentMap that has all the statements. How can I figure out which type of statements have a “;” at the end and which don’t? (I just need to differentiate these two types)

Regards,
Adil

Hi,
    I need to rewrite at the end of line. In order for the edited code
to compile, I must ensure that I write after a ";" character. I just
need to find that character. I have a ParentMap that has all the
statements. How can I figure out which type of statements have a ";" at
the end and which don't? (I just need to differentiate these two types)

I am not sure your purpose of using ParentMap, but for discriminating between two kinds of statements you mentioned, one approach would be to write an AST Visitor derived from RecursiveASTVisitor:

http://clang.llvm.org/doxygen/classclang_1_1RecursiveASTVisitor.html

And then you implement a couple of VisitXXXStmt methods -

bool YourVisitor::VisitCompoundStmt(CompoundStmt *CS)
{
   // here you iterate the body of CS;

   // for each semi-colon-ended statement with respect to C's syntax rules, you can get the end location of it using getLocEnd method;

   // from the end location, you can find the location of ';' (for the debugging purpose, you might also want to dump and check the string starting from the end location using SourceManager::getCharacterData(Loc) method);

   // Last, get a Rewriter instance and do rewriting
}

bool YourVisitor::VisitIfStmt(IfStmt *IS)
{
   // here you check the "Then" Stmt and "Else" Stmt of IS;

   // you only need to handle the case where ThenStmt and ElseStmt are semi-colon-ended statements; (Note that you don't want to iterate these two statements if they are CompoundStmt[s], which will be visited from VisitCompoundStmt)
}

bool YourVisitor::VisitWhileStmt(WhileStmt *WS)
{
   // similar to VisitIfStmt, but you only need to handle the "body" Stmt of WS;
}

...

Note that there could be other ways, e.g., using ASTMatcher, which I never tried, but it should be easy to use.

In any case, you will still need to follow C's syntax rules while analyzing C code, assuming you are dealing with C programs.

- Yang