Clang beginner - how to parse multiple files?

I have a program which is composed of multiple .h and .cpp files.
I tried adding all the .cpp files by calling createMainFileID multiple times, but got an exception saying that the main file has already been defined.

Thanks,

You need to parse different .cpp files with different instances of the compiler.

  - Doug

Can the different compilers use the same ASTContext?
E.g. say a.cpp contains Foo::bar() and b.cpp contains Foo::bar2(); will the Foo type in the semantic DeclContext contain both foo and foo2 after parsing both files?

Could you please provide an example how to setup the parsers?

Many thanks,

Can the different compilers use the same ASTContext?

No, they must have different ASTContexts.

E.g. say a.cpp contains Foo::bar() and b.cpp contains Foo::bar2(); will the Foo type in the semantic DeclContext contain both foo and foo2 after parsing both files?

No, it won't. Separate translation units are separate until something comes in to link them together.

Could you please provide an example how to setup the parsers?

Sorry, I don't have time to write up a complete example now, but this kind of thing will get a lot easier soon with the current work on Tooling infrastructure.

  - Doug

OK, thanks for the detailed info, I’ll look into the linking process.

Hi,

OK, thanks for the detailed info, I’ll look into the linking process.

I think that if you just want to hack something together quickly, you could look into doing something like generating a cpp file in memory that #includes all of the cpp files you care about.

Depending on how your cpp files are written, this might let you sail by. The (admittedly major) catch is that the cpp files can’t depend on anonymous namespaces, static functions, etc. to compile correctly.

Just my 2 cents.

-Joshua

Yup, that’s a major limitation…

BTW, can someone share details on the infrastructure changes Douglas mentioned? Is there a dedicated SVN branch or some doc describing them?

OK, thanks for the detailed info, I'll look into the linking process.

I might be able to give some tips if you can tell me more about what
you're actually trying to do.

If you want to run over the ASTs of multiple TUs in one file, you can
do that with the tooling infrastructure that's in. See
http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-check/ClangCheck.cpp?revision=154008&view=markup
for an example of how to set it up.

The unit tests for the tooling:
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/ToolingTest.cpp?revision=154008&view=markup
give you ideas of how to implement ASTConsumers and ASTFrontendActions
to get you started.

The ASTContext for each run will be provided if you implement
HandleTranslationUnit in the ASTConsumer.

Cheers,
/Manuel

I have a c++ application which I wish to convert to C#.
I can figure out some basic transformation rules which will do most of the work (wrap static constants and functions, expand macros and typedefs, remove pointers, etc.), then I’ll do the rest by hand.

Where are the ClangTool and CompilationDatabase classes defined? I was unable to locate them in the trunk.

OK, found it :slight_smile:

I have a c++ application which I wish to convert to C#.
I can figure out some basic transformation rules which will do most of the
work (wrap static constants and functions, expand macros and typedefs,
remove pointers, etc.), then I'll do the rest by hand.

I think using a RecursiveASTVisitor from an ASTConsumer's
HandleTranslationUnit sounds like a good approach for this.

Cheers,
/Manuel