[llvm-dev] typedef not present in the python AST

David Wiberg via llvm-dev llvm-dev at lists.llvm.org
Tue May 10 12:02:28 PDT 2016


Hi,

It should work. I tested according to below (admittedly this was with
latest and not 3.8). Are you doing something different?

>type test.py
import clang.cindex

def printNodeAndChildren(cursor):
        print(cursor.kind)
        for child in cursor.get_children():
                printNodeAndChildren(child)

index = clang.cindex.Index.create()

tu = index.parse('Input.cpp', ['-x', 'c++', '-std=c++11',
'-fsyntax-only', '-I', '.'])
printNodeAndChildren(tu.cursor)

>python test.py
CursorKind.TRANSLATION_UNIT
CursorKind.TYPEDEF_DECL
CursorKind.FUNCTION_DECL
CursorKind.COMPOUND_STMT
CursorKind.DECL_STMT
CursorKind.TYPEDEF_DECL

>type Input.cpp
typedef int myModuleType;

void foo()
{
  typedef int myFunctionType;
}


Regards
David

2016-05-08 6:13 GMT+02:00 Valentin Tolmer via llvm-dev
<llvm-dev at lists.llvm.org>:
> Hello,
>
> I'm trying to use the python libclang bindings to write a C++
> style-checker, and I'd like to detect all the typedefs to recommend
> switching to using. I'm using libclang 3.8, with the python bindings
> provided with it.
>
> When I parse a file with
> index = clang.cindex.Index.create()
> tu = index.parse(f, ['-x', 'c++', '-std=c++11', '-fsyntax-only', '-I', '.'])
>
> and then I display the node kinds of every node in the AST
> (recursively calling get_children() on every node), I get a node of
> type CursorKind.TYPEDEF_DECL, but only if it is top-level (or inside a
> namespace or class).
>
> If the typedef is in a function body, it doesn't get reported at all
> (even though variable declarations before and after are reported).
>
> Why are they not reported?
>
> Thanks,
>
> --
> Valentin Tolmer
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


More information about the llvm-dev mailing list