-
Notifications
You must be signed in to change notification settings - Fork 1
/
py_cppmodel.py
270 lines (229 loc) · 9.84 KB
/
py_cppmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from typing import Any, List, Optional
from clang.cindex import AccessSpecifier
from clang.cindex import Cursor
from clang.cindex import CursorKind
from clang.cindex import Diagnostic
from clang.cindex import ExceptionSpecificationKind
from clang.cindex import SourceLocation
from clang.cindex import TranslationUnit
from clang.cindex import TypeKind
def _get_annotations(cursor: Cursor) -> List[str]:
return [
c.displayname
for c in cursor.get_children()
if c.kind == CursorKind.ANNOTATE_ATTR
]
class Unmodelled:
def __init__(self, cursor: Cursor):
self.location: SourceLocation = cursor.location
self.name: str = cursor.displayname
def __repr__(self) -> str:
return "<py_cppmodel.Unmodelled {} {}>".format(self.name, self.location)
class Type:
def __init__(self, cindex_type):
self.kind = cindex_type.kind
self.name = cindex_type.spelling
self.is_pointer: bool = self.kind == TypeKind.POINTER
self.is_reference: bool = self.kind == TypeKind.LVALUEREFERENCE
self.is_const: bool = cindex_type.is_const_qualified()
if self.is_pointer or self.is_reference:
self.pointee: Optional[Type] = Type(cindex_type.get_pointee())
else:
self.pointee = None
def __repr__(self) -> str:
return "<py_cppmodel.Type {}>".format(self.name)
class Member:
def __init__(self, cursor: Cursor):
self.type: Type = Type(cursor.type)
self.name: str = cursor.spelling
def __repr__(self) -> str:
return "<py_cppmodel.Member {} {}>".format(self.type, self.name)
class FunctionArgument:
def __init__(self, type: Type, name: Optional[str] = None):
self.type: Type = type
self.name: Optional[str] = name or None
def __repr__(self) -> str:
if self.name is None:
return "<py_cppmodel.FunctionArgument self.type.name>"
return "<py_cppmodel.FunctionArgument {} {}>".format(self.type, self.name)
class _Function:
def __init__(self, cursor):
self.name: str = cursor.spelling
arguments: List[Optional[str]] = [
str(x.spelling) or None for x in cursor.get_arguments()
]
argument_types: List[Type] = [Type(x) for x in cursor.type.argument_types()]
self.is_noexcept: bool = (
cursor.exception_specification_kind
== ExceptionSpecificationKind.BASIC_NOEXCEPT
)
self.return_type: Type = Type(cursor.type.get_result())
self.arguments: List[FunctionArgument] = []
self.annotations: List[str] = _get_annotations(cursor)
for t, n in zip(argument_types, arguments):
self.arguments.append(FunctionArgument(t, n))
def __repr__(self) -> str:
r = "{} {}({})".format(
self.return_type.name,
str(self.name),
", ".join([a.type.name for a in self.arguments]),
)
if self.is_noexcept:
r = r + " noexcept"
return r
class Function(_Function):
def __init__(self, cursor, namespaces=[]):
_Function.__init__(self, cursor)
self.namespace: str = "::".join(namespaces)
self.qualified_name: str = self.name
if self.namespace:
self.qualified_name = "::".join([self.namespace, self.name])
def __repr__(self) -> str:
s = _Function.__repr__(self)
return "<py_cppmodel.Function {}>".format(s)
def __eq__(self, f) -> bool:
if self.name != f.name:
return False
if self.namespace != f.namespace:
return False
if len(self.arguments) != len(f.arguments):
return False
for x, fx in zip(
[arg.type for arg in self.arguments], [arg.type for arg in f.arguments]
):
if x.name != fx.name:
return False
return True
class Method(_Function):
def __init__(self, cursor):
_Function.__init__(self, cursor)
self.is_const: bool = cursor.is_const_method()
self.is_virtual: bool = cursor.is_virtual_method()
self.is_pure_virtual: bool = cursor.is_pure_virtual_method()
self.is_public: bool = cursor.access_specifier == AccessSpecifier.PUBLIC
def __repr__(self) -> str:
s = _Function.__repr__(self)
if self.is_const:
s = "{} const".format(s)
if self.is_pure_virtual:
s = "virtual {} = 0".format(s)
elif self.is_virtual:
s = "virtual {}".format(s)
return "<py_cppmodel.Method {}>".format(s)
class Class:
def __init__(self, cursor: Cursor, namespaces: List[str]):
self.name: str = cursor.spelling
self.namespace: str = "::".join(namespaces)
self.qualified_name: str = self.name
if self.namespace:
self.qualified_name = "::".join([self.namespace, self.name])
self.constructors: List[Method] = []
self.methods: List[Method] = []
self.members: List[Member] = []
self.annotations = _get_annotations(cursor)
self.base_classes = []
# FIXME: populate these fields with AST info
self.source_file = str(cursor.location.file)
self.source_line = int(cursor.location.line)
self.source_column = int(cursor.location.column)
for c in cursor.get_children():
if (
c.kind == CursorKind.CXX_METHOD
and c.type.kind == TypeKind.FUNCTIONPROTO
):
self.methods.append(Method(c))
elif (
c.kind == CursorKind.CONSTRUCTOR
and c.type.kind == TypeKind.FUNCTIONPROTO
):
self.constructors.append(Method(c))
elif c.kind == CursorKind.FIELD_DECL:
self.members.append(Member(c))
elif c.kind == CursorKind.CXX_BASE_SPECIFIER:
self.base_classes.append(c.type.spelling)
def __repr__(self) -> str:
return "<py_cppmodel.Class {}>".format(self.name)
class Model:
def __init__(self, translation_unit: TranslationUnit):
"""Create a model from a translation unit."""
self.filename: str = translation_unit.spelling
self.functions: List[Function] = []
self.classes: List[Class] = []
self.unmodelled_nodes: List[Unmodelled] = []
# Keep a reference to the translation unit to prevent it from being garbage collected.
self.translation_unit: TranslationUnit = translation_unit
def is_error_in_current_file(diagnostic: Diagnostic) -> bool:
if str(diagnostic.location.file) != str(translation_unit.spelling):
return False
if diagnostic.severity == Diagnostic.Error:
return True
if diagnostic.severity == Diagnostic.Fatal:
return True
return False
errors: List[Diagnostic] = [
d for d in translation_unit.diagnostics if is_error_in_current_file(d)
]
if errors:
joined_errors = "\n".join(str(e) for e in errors)
raise ValueError(f"Errors in source file:{joined_errors}")
self._add_child_nodes(translation_unit.cursor, [])
def __repr__(self) -> str:
return "<py_cppmodel.Model filename={}, classes={}, functions={}>".format(
self.filename,
[c.name for c in self.classes],
[f.name for f in self.functions],
)
def extend(self, translation_unit: TranslationUnit):
# Extend an existing model with contents of a new translation unit.
m = Model(translation_unit)
# Check for duplicates and inconsistencies.
for new_class in m.classes:
is_new = True
for old_class in self.classes:
if new_class.qualified_name == old_class.qualified_name:
if new_class.source_file != old_class.source_file:
raise Exception(
"Class {} is defined in multiple locations: {} {}".format(
old_class.qualified_name,
old_class.source_file,
new_class.source_file,
)
)
# Move on as there can only be one match
is_new = False
break
if is_new:
self.classes.append(new_class)
# We only look at declarations for functions so won't raise exceptions
for new_function in m.functions:
is_new = True
for old_function in self.functions:
if new_function == old_function:
is_new = False
break
if is_new:
self.functions.append(new_function)
def _add_child_nodes(self, cursor: Any, namespaces: List[str] = []):
namespaces = namespaces or []
for c in cursor.get_children():
try:
c.kind
except ValueError: # Handle unknown cursor kind
# TODO(jbcoe): Fix cindex.py upstream to avoid needing to do this.
continue
if c.kind == CursorKind.CLASS_DECL or c.kind == CursorKind.STRUCT_DECL:
if c.location.file.name == self.filename:
self.classes.append(Class(c, namespaces))
elif (
c.kind == CursorKind.FUNCTION_DECL
and c.type.kind == TypeKind.FUNCTIONPROTO
):
if c.location.file.name == self.filename:
self.functions.append(Function(c, namespaces))
elif c.kind == CursorKind.NAMESPACE:
child_namespaces = list(namespaces)
child_namespaces.append(c.spelling)
self._add_child_nodes(c, child_namespaces)
else:
if c.location.file.name == self.filename:
self.unmodelled_nodes.append(Unmodelled(c))