Skip to content

Commit eeb0921

Browse files
jbcoeimsanghaar
andauthored
Add (string-matching based) constexpr support to functions and methods
Co-authored-by: Gemini CLI <gemini-cli@google.com>
1 parent 49d2c5c commit eeb0921

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/xyz/cppmodel.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,26 @@ def __init__(self, cursor):
7676
self.return_type: Type = Type(cursor.type.get_result())
7777
self.arguments: List[FunctionArgument] = []
7878
self.annotations: List[str] = _get_annotations(cursor)
79+
self.is_constexpr: bool = "constexpr" in [t.spelling for t in cursor.get_tokens()]
7980

8081
for t, n in zip(argument_types, arguments, strict=False):
8182
self.arguments.append(FunctionArgument(t, n))
8283

83-
def __repr__(self) -> str:
84+
def __str__(self) -> str:
8485
r = "{} {}({})".format(
8586
self.return_type.name,
8687
str(self.name),
8788
", ".join([a.type.name for a in self.arguments]),
8889
)
90+
if self.is_constexpr:
91+
r = "constexpr " + r
8992
if self.is_noexcept:
9093
r = r + " noexcept"
9194
return r
9295

96+
def __repr__(self) -> str:
97+
return self.__str__()
98+
9399

94100
class Function(_Function):
95101
def __init__(self, cursor, namespaces: list[str] | None = None):
@@ -100,10 +106,13 @@ def __init__(self, cursor, namespaces: list[str] | None = None):
100106
if self.namespace:
101107
self.qualified_name = "::".join([self.namespace, self.name])
102108

103-
def __repr__(self) -> str:
104-
s = _Function.__repr__(self)
109+
def __str__(self) -> str:
110+
s = _Function.__str__(self)
105111
return "<xyz.cppmodel.Function {}>".format(s)
106112

113+
def __repr__(self) -> str:
114+
return self.__str__()
115+
107116
def __eq__(self, f) -> bool:
108117
if self.name != f.name:
109118
return False
@@ -125,8 +134,8 @@ def __init__(self, cursor):
125134
self.is_pure_virtual: bool = cursor.is_pure_virtual_method()
126135
self.is_public: bool = cursor.access_specifier == AccessSpecifier.PUBLIC
127136

128-
def __repr__(self) -> str:
129-
s = _Function.__repr__(self)
137+
def __str__(self) -> str:
138+
s = _Function.__str__(self)
130139
if self.is_const:
131140
s = "{} const".format(s)
132141
if self.is_pure_virtual:
@@ -135,6 +144,9 @@ def __repr__(self) -> str:
135144
s = "virtual {}".format(s)
136145
return "<xyz.cppmodel.Method {}>".format(s)
137146

147+
def __repr__(self) -> str:
148+
return self.__str__()
149+
138150

139151
class Class:
140152
def __init__(self, cursor: Cursor, namespaces: List[str]):

tests/test_cppmodel.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
char c[8];
2121
2222
__attribute__((annotate("foo"))) int foo(int);
23+
constexpr int constexpr_method() const;
2324
};
2425
2526
template <class T>
@@ -29,6 +30,7 @@ class B {
2930
};
3031
3132
double bar(double);
33+
constexpr int constexpr_func();
3234
3335
int main() {}
3436
"""
@@ -49,9 +51,10 @@ def test_filename(model):
4951

5052

5153
def test_functions(model):
52-
assert len(model.functions) == 2
54+
assert len(model.functions) == 3
5355
assert str(model.functions[0]) == "<xyz.cppmodel.Function double bar(double)>"
54-
assert str(model.functions[1]) == "<xyz.cppmodel.Function int main()>"
56+
assert str(model.functions[1]) == "<xyz.cppmodel.Function constexpr int constexpr_func()>"
57+
assert str(model.functions[2]) == "<xyz.cppmodel.Function int main()>"
5558

5659

5760
def test_classes(model):
@@ -68,10 +71,11 @@ def test_class_members(model):
6871
assert str(model.classes[0].members[1]) == "<xyz.cppmodel.Member <xyz.cppmodel.Type double> b>"
6972
assert str(model.classes[0].members[2]) == "<xyz.cppmodel.Member <xyz.cppmodel.Type char[8]> c>"
7073

71-
assert len(model.classes[0].methods) == 1
74+
assert len(model.classes[0].methods) == 2
7275
assert str(model.classes[0].methods[0]) == "<xyz.cppmodel.Method int foo(int)>"
7376
assert len(model.classes[0].methods[0].annotations) == 1
7477
assert model.classes[0].methods[0].annotations[0] == "foo"
78+
assert str(model.classes[0].methods[1]) == "<xyz.cppmodel.Method constexpr int constexpr_method() const>"
7579

7680

7781
def test_unmodelled_nodes(model):
@@ -82,5 +86,5 @@ def test_unmodelled_nodes(model):
8286
)
8387
assert (
8488
str(model.unmodelled_nodes[1])
85-
== "<xyz.cppmodel.Unmodelled B<T> <SourceLocation file 'sample.cc', line 12, column 7>>"
89+
== "<xyz.cppmodel.Unmodelled B<T> <SourceLocation file 'sample.cc', line 13, column 7>>"
8690
)

0 commit comments

Comments
 (0)