[cfe-dev] how to get loop boundary form ForStmt

Hugues de Lassus hugues.de-lassus at imag.fr
Tue Mar 18 10:04:06 PDT 2014


Hi,

On 03/18/2014 03:01 PM, Bo Zhao wrote:
> Hi,
> I'm new with clang. I want to know how to get loop boundary form 
> ForStmt. For example, for(int i = 0; i!=10;++i) , I want to get the 
> value 0 and 10. Then I can use those value in other template such as 
> intel TBB parallel_for.
>
> I already know that getCond(),getInit() method, but how to get the value?

I'm not an expert but I'd say that you have to make sure you have this 
very type of init and cond in your ForStmt's, _as well as your inc_. 
Then you can try and get the VarDecl's nodes, get their child, get the 
BinaryOperator nodes and get their RHS.

With this code:
int main() {
     for(int i = 0; i < 10; ++i) {
     }
}

See an AST-dump like this to help you:

clang -cc1 -fsyntax-only -ast-dump forstmt.c
TranslationUnitDecl 0x47880c0 <<invalid sloc>>
|-TypedefDecl 0x47885c0 <<invalid sloc>> __int128_t '__int128'
|-TypedefDecl 0x4788620 <<invalid sloc>> __uint128_t 'unsigned __int128'
|-TypedefDecl 0x4788970 <<invalid sloc>> __builtin_va_list 
'__va_list_tag [1]'
`-FunctionDecl 0x4788a10 <forstmt.c:1:1, line:4:1> main 'int ()'
   `-CompoundStmt 0x4788c78 <line:1:12, line:4:1>
     `-*ForStmt *0x4788c38 <line:2:5, line:3:5>
       |-DeclStmt 0x4788b38 <line:2:9, col:18>
       | `-*VarDecl* 0x4788ac0 <col:9, col:17> *i* 'int'
       |   `-*IntegerLiteral* 0x4788b18 <col:17> 'int' *0*
       |-<<<NULL>>>
       |-*BinaryOperator* 0x4788bb0 <col:20, col:24> 'int' '<'
       | |-ImplicitCastExpr 0x4788b98 <col:20> 'int' <LValueToRValue>
       | | `-DeclRefExpr 0x4788b50 <col:20> 'int' lvalue Var 0x4788ac0 
'i' 'int'
       | `-*IntegerLiteral* 0x4788b78 <col:24> 'int' *10*
       |-UnaryOperator 0x4788c00 <col:28, col:30> 'int' prefix '++'
       | `-DeclRefExpr 0x4788bd8 <col:30> 'int' lvalue Var 0x4788ac0 'i' 
'int'
       `-CompoundStmt 0x4788c20 <col:33, line:3:5>

>
> Thanks.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


-- 

Hugues de Lassus Saint-Geniès

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140318/2378ec6c/attachment.html>


More information about the cfe-dev mailing list