Clang and AST Generation

Hello,

I am trying to use Clang to generate AST for C code in a different format. This format is specific to the term rewriting system that we use for source-to-source transformations.

Studying the ast generated by Clang using the “-ast-print” option to the clang-cc, I see that the pre-processor expands the code(expected). Is there a way to get around and not print all the information all the information and also keep the AST as close to the source as possible( avoiding constant folding).

Thanks,
Pushkar

Pushkar Ratnalikar wrote:

Hello,

This is more appropriate for cfe-dev, not llvmdev. I'm forward this there; please follow-up only to that list.

I am trying to use Clang to generate AST for C code in a different format. This format is specific to the term rewriting system that we use for source-to-source transformations.

Studying the ast generated by Clang using the "-ast-print" option to the clang-cc, I see that the pre-processor expands the code(expected). Is there a way to get around and not print all the information all the information and also keep the AST as close to the source as possible( avoiding constant folding).

Could you clarify what you mean by not printing all the information?

We have several different textual AST dumping mechanisms in various states of decay, but I didn't realize any of them did constant folding.

John.

Hi,

I tried printing the ast for a simple Hello world “C” program using clang-cc -ast-print hello.c.

the output that I get is

typedef signed char __int8_t;
typedef unsigned char __uint8_t;
typedef short __int16_t;
typedef unsigned short __uint16_t;
typedef int __int32_t;
typedef unsigned int __uint32_t;
typedef long long __int64_t;
typedef unsigned long long __uint64_t;
typedef long __darwin_intptr_t;
typedef unsigned int __darwin_natural_t;
.
.
.
.
int main(int argc, char **argv) (CompoundStmt 0x1806170 <hello.c:3:1, line:10:1>
(DeclStmt 0x180f350 <line:4:3, col:12>
0x180e6a0 “int x”
0x180f2f0 “int y”
0x180f320 “int z”
.
.
.

If I want get/print the AST starting from int main(int argc, char **argv) (CompoundStmt 0x1806170 <hello.c:3:1, line:10:1>
(DeclStmt 0x180f350 <line:4:3, col:12>,

How should I proceed ?

Also, I came across constant folding in the Clangs Internals Manual at http://clang.llvm.org/docs/InternalsManual.html#Constants
Since I just started looking at Clang’s code, I am not sure, if that really happens or not. But I need that the AST that I get be as close to the original source as possible.

Thanks for the help.

PS:-I wasn’t aware the cfe-dev list existed , sorry:)

-Pushkar

Pushkar Ratnalikar wrote:

Hi,

I tried printing the ast for a simple Hello world "C" program using *clang-cc -ast-print hello.c*.

the output that I get is

typedef signed char __int8_t;
typedef unsigned char __uint8_t;
typedef short __int16_t;
typedef unsigned short __uint16_t;
typedef int __int32_t;
typedef unsigned int __uint32_t;
typedef long long __int64_t;
typedef unsigned long long __uint64_t;
typedef long __darwin_intptr_t;
typedef unsigned int __darwin_natural_t;
.
int main(int argc, char **argv) (CompoundStmt 0x1806170 <hello.c:3:1, line:10:1>
  (DeclStmt 0x180f350 <line:4:3, col:12>
    0x180e6a0 "int x"
    0x180f2f0 "int y"
    0x180f320 "int z"

That looks like an AST dump, not a print.

.

If I want get/print the AST starting from *int main(int argc, char **argv) (CompoundStmt 0x1806170 <hello.c:3:1, line:10:1>*
* (DeclStmt 0x180f350 <line:4:3, col:12>*,

How should I proceed ?

For what do you need it? The printers (both -ast-dump and -ast-print) are primarily debugging tools.

Also, I came across constant folding in the Clangs Internals Manual at http://clang.llvm.org/docs/InternalsManual.html#Constants
Since I just started looking at Clang's code, I am not sure, if that really happens or not. But I need that the AST that I get be as close to the original source as possible.

The AST is not modified by constant evaluation.

Sebastian