Joel de Vahl
2012-Sep-13 09:06 UTC
[LLVMdev] Parsing C++ template parameters using cindex.py
Hi,
I am parsing a C++ file using cindex.py and want to get the template
parameters to a specific node. However, the tree seems to be different
depending on if the template parameter is a struct/class or a simple
type such as int or float. In the first case the template type is
appended as a child to the VAR_DECL node (the TYPE_REF node seen in
the example below), but this is not the case with for example int. Any
hint on where the template type is located for int/float/etc?
/joel de vahl
Example code:
template <typename T>
class Test
{
T test;
};
struct S
{
};
Test<int> test_int;
Test<S> test_struct;
Tree dump script:
def DeepPrintType(node, depth = 0):
tabs = " " * depth
print tabs + "type"
print tabs + " kind", node.kind
def DeepPrint(node, depth = 0):
tabs = " " * depth
print tabs + "cursor", node.spelling
print tabs + " location", node.location
node_type_res = node.type.get_result()
print tabs + " result", node_type_res.kind
print tabs + " kind", node.kind
DeepPrintType(node.type, depth + 1)
print tabs + " xdata", node.xdata
print tabs + " data", node.data[0], node.data[1], node.data[2]
print tabs + " children"
children = [c for c in node.get_children()]
for c in children:
DeepPrint(c, depth + 2)
index = Index.create()
tu = index.parse(None, ['testfile.cpp'])
children = [c for c in tu.cursor.get_children() if c.kind =CursorKind.VAR_DECL]
for c in children:
DeepPrint(c)
Dump output:
cursor test_int
location <SourceLocation file 'testfile.cpp', line 11, column 11>
result TypeKind.INVALID
kind CursorKind.VAR_DECL
type
kind TypeKind.UNEXPOSED
xdata 0
data 48356112 1 4106544
children
cursor None
location <SourceLocation file 'testfile.cpp', line 11, column
1>
result TypeKind.INVALID
kind CursorKind.TEMPLATE_REF
type
kind TypeKind.INVALID
xdata 0
data 48201424 75 4106544
children
cursor None
location <SourceLocation file 'testfile.cpp', line 11, column
11>
result TypeKind.INVALID
kind CursorKind.CALL_EXPR
type
kind TypeKind.UNEXPOSED
xdata 0
data 48356112 48357480 4106544
children
cursor test_struct
location <SourceLocation file 'testfile.cpp', line 12, column 9>
result TypeKind.INVALID
kind CursorKind.VAR_DECL
type
kind TypeKind.UNEXPOSED
xdata 0
data 48358032 1 4106544
children
cursor None
location <SourceLocation file 'testfile.cpp', line 12, column
1>
result TypeKind.INVALID
kind CursorKind.TEMPLATE_REF
type
kind TypeKind.INVALID
xdata 0
data 48201424 96 4106544
children
cursor None
location <SourceLocation file 'testfile.cpp', line 12, column
6>
result TypeKind.INVALID
kind CursorKind.TYPE_REF
type
kind TypeKind.RECORD
xdata 0
data 48355056 101 4106544
children
cursor None
location <SourceLocation file 'testfile.cpp', line 12, column
9>
result TypeKind.INVALID
kind CursorKind.CALL_EXPR
type
kind TypeKind.UNEXPOSED
xdata 0
data 48358032 48415208 4106544
children
Duncan Sands
2012-Sep-14 07:25 UTC
[LLVMdev] Parsing C++ template parameters using cindex.py
Hi Joel, this looks like a question for the clang mailing list rather than this one. Ciao, Duncan. On 13/09/12 11:06, Joel de Vahl wrote:> Hi, > > I am parsing a C++ file using cindex.py and want to get the template > parameters to a specific node. However, the tree seems to be different > depending on if the template parameter is a struct/class or a simple > type such as int or float. In the first case the template type is > appended as a child to the VAR_DECL node (the TYPE_REF node seen in > the example below), but this is not the case with for example int. Any > hint on where the template type is located for int/float/etc? > > /joel de vahl > > Example code: > > template <typename T> > class Test > { > T test; > }; > > struct S > { > }; > > Test<int> test_int; > Test<S> test_struct; > > Tree dump script: > > def DeepPrintType(node, depth = 0): > tabs = " " * depth > print tabs + "type" > print tabs + " kind", node.kind > > def DeepPrint(node, depth = 0): > tabs = " " * depth > print tabs + "cursor", node.spelling > print tabs + " location", node.location > node_type_res = node.type.get_result() > > print tabs + " result", node_type_res.kind > print tabs + " kind", node.kind > DeepPrintType(node.type, depth + 1) > print tabs + " xdata", node.xdata > print tabs + " data", node.data[0], node.data[1], node.data[2] > > print tabs + " children" > children = [c for c in node.get_children()] > for c in children: > DeepPrint(c, depth + 2) > > index = Index.create() > tu = index.parse(None, ['testfile.cpp']) > children = [c for c in tu.cursor.get_children() if c.kind => CursorKind.VAR_DECL] > for c in children: > DeepPrint(c) > > Dump output: > > cursor test_int > location <SourceLocation file 'testfile.cpp', line 11, column 11> > result TypeKind.INVALID > kind CursorKind.VAR_DECL > type > kind TypeKind.UNEXPOSED > xdata 0 > data 48356112 1 4106544 > children > cursor None > location <SourceLocation file 'testfile.cpp', line 11, column 1> > result TypeKind.INVALID > kind CursorKind.TEMPLATE_REF > type > kind TypeKind.INVALID > xdata 0 > data 48201424 75 4106544 > children > cursor None > location <SourceLocation file 'testfile.cpp', line 11, column 11> > result TypeKind.INVALID > kind CursorKind.CALL_EXPR > type > kind TypeKind.UNEXPOSED > xdata 0 > data 48356112 48357480 4106544 > children > > cursor test_struct > location <SourceLocation file 'testfile.cpp', line 12, column 9> > result TypeKind.INVALID > kind CursorKind.VAR_DECL > type > kind TypeKind.UNEXPOSED > xdata 0 > data 48358032 1 4106544 > children > cursor None > location <SourceLocation file 'testfile.cpp', line 12, column 1> > result TypeKind.INVALID > kind CursorKind.TEMPLATE_REF > type > kind TypeKind.INVALID > xdata 0 > data 48201424 96 4106544 > children > cursor None > location <SourceLocation file 'testfile.cpp', line 12, column 6> > result TypeKind.INVALID > kind CursorKind.TYPE_REF > type > kind TypeKind.RECORD > xdata 0 > data 48355056 101 4106544 > children > cursor None > location <SourceLocation file 'testfile.cpp', line 12, column 9> > result TypeKind.INVALID > kind CursorKind.CALL_EXPR > type > kind TypeKind.UNEXPOSED > xdata 0 > data 48358032 48415208 4106544 > children > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
Seemingly Similar Threads
- [LLVMdev] Parsing C++ headers with Clang bindings for Python
- [LLVMdev] Parsing C++ headers with Clang bindings for Python
- [LLVMdev] Parsing C++ headers with Clang bindings for Python
- typedef not present in the python AST
- [LLVMdev] How to make Polly ignore some non-affine memory accesses