Skip to content

Commit d140ad9

Browse files
committed
Fix nested lookups for parameters and return types
1 parent 1972125 commit d140ad9

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

CppHeaderParser/CppHeaderParser.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,21 @@ def get_pure_virtual_methods(self, type="public"):
647647
r[meth["name"]] = meth
648648
return r
649649

650+
def _lookup_type(self, name):
651+
# TODO: should have indexes for these lookups... and they
652+
# should be more unified
653+
for access in supportedAccessSpecifier:
654+
for e in self["enums"][access]:
655+
if e.get("name") == name:
656+
return {
657+
"enum": self["name"] + "::" + e["name"],
658+
"type": e["name"],
659+
"namespace": e["namespace"],
660+
}
661+
for n in self["nested_classes"]:
662+
if n["name"] == name:
663+
return {"raw_type": self["name"] + "::" + n["name"], "type": n["name"]}
664+
650665
def __init__(self, nameStack, curTemplate, doxygen, location):
651666
self["nested_classes"] = []
652667
self["parent"] = None
@@ -1505,15 +1520,21 @@ def resolve_type(self, string, result): # recursive
15051520
else:
15061521
used = None
15071522

1508-
# Search for using directives in parents
1509-
parent = result["parent"]
1510-
while parent:
1511-
p_using = parent.get("using")
1512-
if p_using:
1513-
used = p_using.get(alias)
1514-
if used:
1515-
break
1516-
parent = parent["parent"]
1523+
# Search for in parents
1524+
if not used:
1525+
parent = result["parent"]
1526+
while parent:
1527+
p_using = parent.get("using")
1528+
if p_using:
1529+
used = p_using.get(alias)
1530+
if used:
1531+
break
1532+
lookup = getattr(parent, "_lookup_type", None)
1533+
if lookup:
1534+
used = lookup(alias)
1535+
if used:
1536+
break
1537+
parent = parent["parent"]
15171538

15181539
if not used and self.using:
15191540
# search for type in all enclosing namespaces
@@ -1525,7 +1546,7 @@ def resolve_type(self, string, result): # recursive
15251546
break
15261547

15271548
if used:
1528-
for i in ("type", "namespace", "ctypes_type", "raw_type"):
1549+
for i in ("enum", "type", "namespace", "ctypes_type", "raw_type"):
15291550
if i in used:
15301551
result[i] = used[i]
15311552
result["unresolved"] = False
@@ -1860,6 +1881,26 @@ def finalize(self):
18601881
if meth["pure_virtual"]:
18611882
cls["abstract"] = True
18621883

1884+
# hack
1885+
rtnType = {
1886+
"aliases": [],
1887+
"parent": cls,
1888+
"unresolved": True,
1889+
"constant": 0,
1890+
"constexpr": 0,
1891+
"static": 0,
1892+
"pointer": 0,
1893+
"reference": 0,
1894+
}
1895+
self.resolve_type(meth["rtnType"], rtnType)
1896+
if not rtnType["unresolved"]:
1897+
if "enum" in rtnType:
1898+
meth["rtnType"] = rtnType["enum"]
1899+
elif "raw_type" in rtnType:
1900+
meth["rtnType"] = rtnType["raw_type"]
1901+
1902+
# TODO: all of this needs to die and be replaced by CppVariable
1903+
18631904
if (
18641905
not meth["returns_fundamental"]
18651906
and meth["returns"] in C99_NONSTANDARD

test/test_CppHeaderParser.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,5 +3488,39 @@ def test_enum(self):
34883488
)
34893489

34903490

3491+
class NestedResolving_TestCase(unittest.TestCase):
3492+
def setUp(self):
3493+
self.cppHeader = CppHeaderParser.CppHeader(
3494+
"""
3495+
struct A {
3496+
3497+
enum { ANON };
3498+
3499+
struct B {};
3500+
enum C { X };
3501+
3502+
B fnested(B b);
3503+
C fenum(C c);
3504+
};
3505+
3506+
""",
3507+
"string",
3508+
)
3509+
3510+
def test_nothing(self):
3511+
c = self.cppHeader.classes["A"]
3512+
fn = c["methods"]["public"][0]
3513+
self.assertEqual(fn["name"], "fnested")
3514+
self.assertEqual(fn["rtnType"], "A::B")
3515+
self.assertEqual(len(fn["parameters"]), 1)
3516+
self.assertEqual(fn["parameters"][0]["raw_type"], "A::B")
3517+
3518+
fn = c["methods"]["public"][1]
3519+
self.assertEqual(fn["name"], "fenum")
3520+
self.assertEqual(fn["rtnType"], "A::C")
3521+
self.assertEqual(len(fn["parameters"]), 1)
3522+
self.assertEqual(fn["parameters"][0]["enum"], "A::C")
3523+
3524+
34913525
if __name__ == "__main__":
34923526
unittest.main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy