Missing statements in the Clang print AST

Hi,

I am trying to inspect the AST generated by clang of a simple C++ program. However in the body of the printed function are missing statements and functions call with respect to the original code. Can you tell me what parameters are missing while calling clang?

The clang command line that I am using is:

clang++ -cc1 -undef -ast-print pc/consumer.cc -I/yapi-2.1.1/src/api

The output function is:

void main() {
     int n, j;
     for (int i = 0; i < n; i++) {
     }
}

The original code is:

void Consumer::main()
{
   int n,j,s;

   printf("Consumer started");

   // Simple test statement
   s = 5;

   read(in, n);

   for (int i=0; i<n; i++)
   {
     read(in, j);
     assert(i==j);
   }
}

Thanks,

Miguel

Hello Miguel,

The parameters are correct. The AST printer in Clang is a bit broken.
Patches welcome!

If you just need to inspect the AST, try -ast-dump. It displays all AST nodes.

Dmitri

Hi Dimitri,

I wanted to be sure that the functions in the AST have the complete body of the original code. By using the -ast-dump I got the following:

void main() (CompoundStmt 0x57fca90 <pc/consumer.cc:28:1, line:42:1>
   (DeclStmt 0x57fc110 <line:29:3, col:10>
     (0x57fc030 "int n")
     (0x57fc0a0 "int j"))
   (ForStmt 0x57fca50 <line:37:3, line:41:3>
     (DeclStmt 0x57fc358 <line:37:8, col:15>
       (0x57fc2e0 "int i =
         (IntegerLiteral 0x57fc338 <col:14> 'int' 0)"))
     (<<<NULL>>>)
     (BinaryOperator 0x57fc3f0 <col:17, col:19> '_Bool' '<'
       (ImplicitCastExpr 0x57fc3c0 <col:17> 'int' <LValueToRValue>
         (DeclRefExpr 0x57fc370 <col:17> 'int' lvalue Var 0x57fc2e0 'i' 'int'))
       (ImplicitCastExpr 0x57fc3d8 <col:19> 'int' <LValueToRValue>
         (DeclRefExpr 0x57fc398 <col:19> 'int' lvalue Var 0x57fc030 'n' 'int')))
     (UnaryOperator 0x57fc440 <col:22, col:23> 'int' postfix '++'
       (DeclRefExpr 0x57fc418 <col:22> 'int' lvalue Var 0x57fc2e0 'i' 'int'))
     (CompoundStmt 0x57fc5d0 <line:38:3, line:41:3>)))

However, here I don't see the function call to the "printf" and the statement "s = 5". Any ideas?

Miguel

Did you show us the complete source code file? Do you get any errors
if you remove -ast-dump? (For example, errors about undeclared
'printf'?) If there are errors, then what you see is the AST after
error recovery.

Dmitri

Dimitri,

I think my mistake is that I was trying to have a look into the AST but the code is still not completely compiling due to errors while searching for headers. The standard c/c++ libraries are already set by default in clang or should I specify the paths to the system headers (e.g /usr/include ..)?

Thanks,

Miguel

Please note that there is a difference between the driver and the
frontend. It is described here:

http://clang.llvm.org/docs/FAQ.html#id2

Dmitri

Hi Dimitri,

Thanks for the clarification. Anyway I added more include paths to the command line in the frontend mode (-cc1) and now I see the output almost as expect with the -ast-print option. Still the printf function is not there.

Miguel

Dimitri,

One last question, Is it possible to call only the frontend such that the proper options of the system are set, just like the driver does.

Thanks,

Miguel

You can run "clang -### <other options>" to see the frontend
invocation command line.

Or you can ask the driver to add pass an option to the driver:
clang -Xclang -ast-dump ...

Dmitri