[cfe-dev] Statements that end with ;
Yang Chen
chenyang at cs.utah.edu
Sat Nov 17 00:10:16 PST 2012
On 11/16/2012 6:09 PM, Mohammad Adil wrote:
> 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
>
> Regards,
> Adil
>
>
> On Sat, Nov 17, 2012 at 2:09 AM, Mohammad Adil <madil90 at gmail.com
> <mailto:madil90 at gmail.com>> wrote:
>
> 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
>
>
> On Sat, Nov 17, 2012 at 1:59 AM, Yang Chen <chenyang at cs.utah.edu
> <mailto:chenyang at cs.utah.edu>> wrote:
>
> 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
> <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
> <http://clang.llvm.org/doxygen/classclang_1_1BinaryOperator.html>
>
> - Yang
>
> Regards,
> Adil
>
>
>
> --
> View this message in context:
> http://clang-developers.42468.__n3.nabble.com/Statements-that-__end-with-tp4028311.html
> <http://clang-developers.42468.n3.nabble.com/Statements-that-end-with-tp4028311.html>
> Sent from the Clang Developers mailing list archive at
> Nabble.com.
> _________________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu <mailto:cfe-dev at cs.uiuc.edu>
> http://lists.cs.uiuc.edu/__mailman/listinfo/cfe-dev
> <http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev>
>
>
>
>
>
> --
> Mohammad Adil
> LUMS SSE
>
>
>
>
> --
> Mohammad Adil
> LUMS SSE
>
More information about the cfe-dev
mailing list