[cfe-dev] Need Help Printing Preprocessed Code and Modifying CFG

David Blaikie dblaikie at gmail.com
Thu Jul 4 10:52:49 PDT 2013


On Thu, Jul 4, 2013 at 10:46 AM, Kevin Boos <kevinaboos at gmail.com> wrote:
> Hi Ted,
>
> Thanks for the reply. Now I think I understand the relationship between the
> AST and CFG. I won't attempt to directly modify the CFG, which I had
> previously assumed to be immutable due to all the const usage throughout its
> methods.
>
> So, if I was examining a CFGBlock, for example, I could access the
> underlying AST Stmt nodes and operate on them.
>
> Now, once I have access to the AST node, what is a good way to remove it
> from the AST? (Ideally in a clean manner)....  I assume this is possible
> because I have seen several prior works that use Clang for refactoring, and
> many refactoring operations involve deleting or at least
> replacing/reordering nodes.

Refactoring tools (such as the ASTMatchers) are generally encouraged
to do so by using the AST's source locations to modify the original
source code text, not by mutating the AST & generating new source.

(as someone's already mentioned, some kinds of AST mutation can be
done with things such as TreeTransform - but it's not usually
recommended as a tool for doing refactoring (TreeTransform exists
mostly for handling template instantiation))

>
> Best,
> Kevin
>
> On Jul 4, 2013 12:23 PM, "Ted Kremenek" <kremenek at apple.com> wrote:
>>
>> On Jul 3, 2013, at 11:00 AM, Kevin Boos <kevinaboos at gmail.com> wrote:
>>
>> I think I've narrowed down what I really need from Clang -- the ability to
>> delete a note from the AST/CFG and then output that modified AST as source
>> code.
>>
>>
>> Hi Kevin,
>>
>> Responding to just the comment, the CFG should be viewed as a data
>> structure that is lazily constructed from the AST.  It is a “view” on the
>> AST, but it shouldn’t be viewed as something you can mutate.  Also, the CFG
>> may contain AST elements not in the original AST (particularly DeclStmts)
>> for purposes of easier analysis.  This latter part is something we may wish
>> to change in the future by adding new CFGElements, but that’s something that
>> is currently done now.
>>
>> Ted




More information about the cfe-dev mailing list