[cfe-dev] clang AST printer

Manuel Klimek klimek at google.com
Fri Sep 21 13:37:13 PDT 2012


On Fri, Sep 21, 2012 at 10:29 PM, Sean Silva <silvas at purdue.edu> wrote:
>> we had a discussion around the strategy for handling ast dumping
>> fairly recently. I think the common agreement in the end was that
>> instead of having an extra tool, we want to make clang's -ast-dump
>> awesome. I don't know what your ultimate goal is here, but from my
>> side any work that goes towards making clang -ast-dump better would be
>> highly appreciated :)
>
> Here's an idea:
>
> $ clang -ast-dump="<arbitrary ASTMatcher expression>"

Yep, once the dynamic matcher stuff is in mainline that'll be possible ;)

>
> --Sean Silva
>
> On Fri, Sep 21, 2012 at 5:49 AM, Manuel Klimek <klimek at google.com> wrote:
>> Hi Philip,
>>
>> we had a discussion around the strategy for handling ast dumping
>> fairly recently. I think the common agreement in the end was that
>> instead of having an extra tool, we want to make clang's -ast-dump
>> awesome. I don't know what your ultimate goal is here, but from my
>> side any work that goes towards making clang -ast-dump better would be
>> highly appreciated :)
>>
>> Cheers,
>> /Manuel
>>
>> On Fri, Sep 21, 2012 at 1:17 AM, Philip Craig <philipjcraig at gmail.com> wrote:
>>> Hi,
>>>
>>> I've been writing a tool to print a clang AST. You can find it at
>>> https://github.com/philipc/clang-ast. I've been mostly writing this as
>>> a learning exercise, but I would like to see if there is any wider
>>> interest in the tool.
>>>
>>> I'm new to clang, and interested in working on tooling. For this task,
>>> it is important to have an understanding of the AST. I've found the
>>> documentation at http://clang.llvm.org/docs/InternalsManual.html and
>>> http://clang.llvm.org/docs/IntroductionToTheClangAST.html (is there
>>> any more?), but I think it would be helpful to be able to print the
>>> AST for source code to see how it is used in practice. I've found two
>>> ways to do this currently, which aren't quite what I wanted:
>>>
>>> clang --ast-dump
>>> - pretty prints some parts, has too much internal info, more suited
>>> for debugging use by clang developers
>>>
>>> clang --ast-dump-xml
>>> - incomplete and XML is too verbose for this purpose
>>>
>>> Since RecursiveASTVisitor seems to be the API that will be used by
>>> tool developers, I've been using RAV to print the AST. I've found a
>>> few minor bugs in RAV as a result of this, which I'm in the process of
>>> submitting.
>>>
>>> One limitation of RAV is that the Visit methods aren't given
>>> information about their relationship with their parent, so the tool
>>> can only list all the children of a node, without distinguishing
>>> between the LHS and RHS of an operator, for example. Is there any
>>> desire to extend RAV to be able to do this?
>>>
>>> I've been writing small code snippets to test printing the various
>>> parts of the AST. If you want to see examples of the output of the
>>> tool, this is included in the test cases. As a quick example, "void
>>> foo() {}" is printed as:
>>>
>>>   FunctionDecl
>>>     DeclarationName foo
>>>     FunctionNoProtoType
>>>       BuiltinType void
>>>     CompoundStmt
>>>
>>> Finally, I've been trying to give a textual description of the AST
>>> grammar in https://github.com/philipc/clang-ast/blob/master/ast.txt.
>>> This is an attempt to give something similar to
>>> http://docs.python.org/py3k/library/ast.html#abstract-grammar. I'm not
>>> sure if it is turning out to be that useful though.
>>>
>>> Any comments welcome!
>>>
>>> Thanks,
>>> Philip
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list