Hello!
Recently I started using Clang for the C-code parsing, but I have some problems with find information on how to do Source-to-source transformations.
At the moment I can generate Abstract Syntax Tree and tree traversal trough the API, i.e. rewriting the ASTConsumer and DeclVisitor classes, but I don’t know how to do the transformations of the AST and write it back to the source code. Could you please give me some feedback on how to do such manipulations? I didn’t find anything useful in google, so I decided to write to mailing list.
I hope I was clear enough
Hello
I'd like to get ASTContext-s for multiple input files (by calling
ParseAST)
I've looked into web and clang's sources but I lost my way...
My understand is that each of FileManager, SourceManager, IdentifierTable,
SelectorTable and HeaderSearch is unique to MY PROGRAM,
then, each of Preprocessor and ASTContext is unique to each of INPUT FILES.
Is it correct?
My code:
{
...
SourceManager srcMgr(diag);
...
foreach input-files {
if (file-is-main) {
srcMgr.createMainFileID(file);
}
else {
srcMgr.createFileID(file, ...);
}
ASTContext context(langOpts, srcMgr, ...);
Preprocessor cpp(diag, langOpts, targetInfo, srcMgr, headerSearch);
ParseAST(cpp, consumer, context);
...
} parses only the main file.
Preprocessor and ASTContext must have a reference to its SourceManager
but never know any specific FileID. How can I tell my program to parse
other files?
Could anyone point to or suggest any solutions?
Thanks and regards,
Takei
Check out RewriteObjC.cpp, which uses the (textual) Rewriter class to perform AST transformations.
- Doug
Hello
I'd like to get ASTContext-s for multiple input files (by calling
ParseAST)
I've looked into web and clang's sources but I lost my way...
My understand is that each of FileManager, SourceManager, IdentifierTable,
SelectorTable and HeaderSearch is unique to MY PROGRAM,
then, each of Preprocessor and ASTContext is unique to each of INPUT FILES.
Is it correct?
To. All of these entities are unique to each translation unit.
My code:
{
...
SourceManager srcMgr(diag);
...
foreach input-files {
if (file-is-main) {
srcMgr.createMainFileID(file);
}
else {
srcMgr.createFileID(file, ...);
}
ASTContext context(langOpts, srcMgr, ...);
Preprocessor cpp(diag, langOpts, targetInfo, srcMgr, headerSearch);
ParseAST(cpp, consumer, context);
...
} parses only the main file.
Preprocessor and ASTContext must have a reference to its SourceManager
but never know any specific FileID. How can I tell my program to parse
other files?
Could anyone point to or suggest any solutions?
You'll need to create all of these entities from scratch each time to process a new translation unit.
However, I strongly suggest not trying to do all of this by creating contexts, preprocessors, file/source managers, etc., directly. Instead, use CompilerInvocation and CompilerInstance, with your own custom Action, to drive the compilation process.
- Doug
Thank you very much, Doug.
I try CompilerInvocation and CompilerInstance.
Regards,
Takei
> Hello
>
> I'd like to get ASTContext-s for multiple input files (by calling
> ParseAST)
> I've looked into web and clang's sources but I lost my way...
>
> My understand is that each of FileManager, SourceManager,
IdentifierTable,
> SelectorTable and HeaderSearch is unique to MY PROGRAM,
> then, each of Preprocessor and ASTContext is unique to each of INPUT
FILES.
>
> Is it correct?
To. All of these entities are unique to each translation unit.
> My code:
> {
> ...
> SourceManager srcMgr(diag);
> ...
> foreach input-files {
> if (file-is-main) {
> srcMgr.createMainFileID(file);
> }
> else {
> srcMgr.createFileID(file, ...);
> }
> ASTContext context(langOpts, srcMgr, ...);
> Preprocessor cpp(diag, langOpts, targetInfo, srcMgr, headerSearch);
> ParseAST(cpp, consumer, context);
> ...
> } parses only the main file.
>
> Preprocessor and ASTContext must have a reference to its SourceManager
> but never know any specific FileID. How can I tell my program to parse
> other files?
> Could anyone point to or suggest any solutions?
You'll need to create all of these entities from scratch each time to
process a new translation unit.
However, I strongly suggest not trying to do all of this by creating
contexts, preprocessors, file/source managers, etc., directly. Instead,
use CompilerInvocation and CompilerInstance, with your own custom Action,
to drive the compilation process.
Thank you for the answer. I’ll take a look at it.
2011/1/27 Douglas Gregor <dgregor@apple.com>