[cfe-dev] A howto or template for adding a transformation pass
kremenek at apple.com
Sat Aug 9 11:00:10 PDT 2008
On Aug 8, 2008, at 6:38 AM, Hiren Patel wrote:
> I'd like to find the best method of adding a transformation pass
> that uses the AST and the CFG. Is there a template or a how-to on
> My goal in summary is to:
> 1. Determine declarations of certain new statements (beginD and
> endD) in the C source (without changing the front-end; they are
> simple macros).
I admit that I'm a little confused. I'm not quite certain what you
mean by "determine declarations of certain new statements." Are you
talking about determining where in a function/method body you would
want to insert code, or are you scanning for particular statements to
> 2. Replace the statements with certain macros with additional labels.
Which statements are you replacing? Inserting macros should be fairly
straightforward by using the rewriter (this is a textual translation
by editing the source code, not editing the ASTs).
> 4. Output the changed C file.
If you are using the rewriter to do your edits, this is very
straightforward. I would like at how the Objective-C -> C translator
(RewriteObjC) uses the Rewriter to make edits to the source file and
then blast the new source to disk.
> I've successfully tried the following:
> - Added an Analysis consumer, but I noticed that this Analysis
> consumer method is invoked twice; once for each function in the C
> source. I'd like to have access to the full Analysis and then walk
> it using walker methods.
> - Added an ASTConsumer, but again, this is invoked twice. Once per
I'm not certain what you mean by the "invoked twice" part.
ASTConsumer implements an action/visitor interface where it receives
callbacks for each top level declaration (HandleTopLevelDecl) as well
as a single call back once the entire translation unit has been parsed
I don't have enough context for what you are trying to accomplish, so
it's difficult for me to advise you on the best course of action. For
example, I'm not certain what you need the CFGs for, so it's hard for
me to say whether you should just implement another callback function
for AnalysisConsumer (to implement a new analysis) or implement a new
ASTConsumer. The Objective-C rewriter, for example, is implemented as
an ASTConsumer, where the major rewriting work is done in
My gut feeling is that you want to use the Objective-C rewriter as
your model, and that you want to rewrite pieces of the C source file
by removing some statements and inserting others. You're probably
just going to want to write a fresh ASTConsumer that contains a
Rewriter object. You can either scan for functions/methods to edit
using HandleTopLevelDecl, or you can wait until HandleTranslationUnit
is called to scan all the declarations at once. In
HandleTranlsationUnit you would also call the appropriate methods on
the Rewriter to emit your changed source code to disk. If you need
CFGs, it's very easy to create a CFG using a single method:
CFG::BuildCFG (just pass as an argument the Stmt* that represents the
body of the function or method you want to analyze).
More information about the cfe-dev