Skip to content

Commit e6be5ec

Browse files
committed
Make union naming more sensible
- Breaks backwards compat
1 parent 46bb3d6 commit e6be5ec

File tree

2 files changed

+59
-48
lines changed

2 files changed

+59
-48
lines changed

CppHeaderParser/CppHeaderParser.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,13 @@ def _parse_cppclass_name(c, stack):
497497
c["namespace"] = ""
498498

499499
# backwards compat
500-
if name.startswith("anon-struct-"):
501-
name = "<" + name + ">"
500+
if name.startswith("anon-"):
501+
if (
502+
name.startswith("anon-class-")
503+
or name.startswith("anon-struct-")
504+
or name.startswith("anon-union-")
505+
):
506+
name = "<" + name + ">"
502507
c["name"] = name
503508
c["bare_name"] = name
504509
debug_print("Found class '%s'", name)
@@ -820,7 +825,6 @@ class CppUnion(CppClass):
820825

821826
def __init__(self, nameStack, doxygen, location):
822827
CppClass.__init__(self, nameStack, None, doxygen, location)
823-
self["name"] = "union " + self["name"]
824828
self["members"] = self["properties"]["public"]
825829

826830
def transform_to_union_keys(self):
@@ -2435,14 +2439,24 @@ def _evaluate_class_stack(self):
24352439
# When dealing with typedefed structs, get rid of typedef keyword to handle later on
24362440
if self.nameStack[0] == "typedef":
24372441
del self.nameStack[0]
2438-
if len(self.nameStack) == 1:
2442+
2443+
if len(self.nameStack) == 1:
2444+
if self.nameStack[0] == "struct":
24392445
self.anon_struct_counter += 1
24402446
# We cant handle more than 1 anonymous struct, so name them uniquely
24412447
self.nameStack.append("anon-struct-%d" % self.anon_struct_counter)
2448+
elif self.nameStack[0] == "union":
2449+
self.anon_union_counter += 1
2450+
# We cant handle more than 1 anonymous union, so name them uniquely
2451+
self.nameStack.append("anon-union-%d" % self.anon_union_counter)
2452+
elif self.nameStack[0] == "class":
2453+
self.anon_class_counter += 1
2454+
# We cant handle more than 1 anonymous class, so name them uniquely
2455+
self.nameStack.append("anon-class-%d" % self.anon_class_counter)
24422456

24432457
if self.nameStack[0] == "class":
24442458
self.curAccessSpecifier = "private"
2445-
else: # struct
2459+
else: # struct/union
24462460
self.curAccessSpecifier = "public"
24472461
debug_print(
24482462
"curAccessSpecifier changed/defaulted to %s", self.curAccessSpecifier
@@ -2453,19 +2467,13 @@ def _evaluate_class_stack(self):
24532467
self._get_stmt_doxygen(),
24542468
self._get_location(self.nameStack),
24552469
)
2456-
if newClass["name"] == "union ":
2457-
self.anon_union_counter = [self.braceDepth, 2]
2458-
else:
2459-
self.anon_union_counter = [self.braceDepth, 1]
2460-
trace_print("NEW UNION", newClass["name"])
24612470
else:
24622471
newClass = CppClass(
24632472
self.nameStack,
24642473
self.curTemplate,
24652474
self._get_stmt_doxygen(),
24662475
self._get_location(self.nameStack),
24672476
)
2468-
trace_print("NEW CLASS", newClass["name"])
24692477
newClass["declaration_method"] = self.nameStack[0]
24702478
self.classes_order.append(newClass) # good idea to save ordering
24712479
self.stack = [] # fixes if class declared with ';' in closing brace
@@ -2639,7 +2647,8 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
26392647
# Old namestacks for a given level
26402648
self.nameStackHistory = []
26412649
self.anon_struct_counter = 0
2642-
self.anon_union_counter = [-1, 0]
2650+
self.anon_union_counter = 0
2651+
self.anon_class_counter = 0
26432652

26442653
#: Using directives in this header outside of class scope: key is
26452654
#: full name for lookup, value is :class:`.CppVariable`
@@ -2751,11 +2760,6 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
27512760
tok = lex.token(eof_ok=True)
27522761
if not tok:
27532762
break
2754-
if (
2755-
self.anon_union_counter[0] == self.braceDepth
2756-
and self.anon_union_counter[1]
2757-
):
2758-
self.anon_union_counter[1] -= 1
27592763
tok.value = TagStr(tok.value, location=tok.location)
27602764

27612765
# debug_print("TOK: %s", tok)
@@ -2875,6 +2879,24 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
28752879
self.curClass = self.curClass[
28762880
: -(len(thisClass["name"]) + 2)
28772881
]
2882+
2883+
# Detect anonymous union members
2884+
if (
2885+
self.curClass
2886+
and thisClass["declaration_method"] == "union"
2887+
and thisClass["name"].startswith("<")
2888+
and self.lex.token_if(";")
2889+
):
2890+
debug_print("Creating anonymous union")
2891+
# Force the processing of an anonymous union
2892+
self.nameStack = [""]
2893+
self.stack = self.nameStack + [";"]
2894+
debug_print("pre eval anon stack")
2895+
self._evaluate_stack(";")
2896+
debug_print("post eval anon stack")
2897+
self.stack = []
2898+
self.nameStack = []
2899+
self.stmtTokens = []
28782900
else:
28792901
self.curClass = ""
28802902
self.stack = []
@@ -2891,8 +2913,6 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
28912913
self.nameStack.append(tok.value)
28922914
else:
28932915
self.nameStack.append(tok.value)
2894-
if self.anon_union_counter[0] == self.braceDepth:
2895-
self.anon_union_counter = [-1, 0]
28962916
elif tok.type == ":":
28972917
if self.nameStack and self.nameStack[0] in supportedAccessSpecifier:
28982918
specifier = " ".join(self.nameStack)
@@ -2916,24 +2936,6 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
29162936
self.nameStack.append(tok.value)
29172937

29182938
elif tok.type == ";":
2919-
if (
2920-
self.anon_union_counter[0] == self.braceDepth
2921-
and self.anon_union_counter[1]
2922-
):
2923-
debug_print("Creating anonymous union")
2924-
# Force the processing of an anonymous union
2925-
saved_namestack = self.nameStack[:]
2926-
saved_stack = self.stack[:]
2927-
self.nameStack = [""]
2928-
self.stack = self.nameStack + [";"]
2929-
self.nameStack = self.nameStack[0:1]
2930-
debug_print("pre eval anon stack")
2931-
self._evaluate_stack(tok.type)
2932-
debug_print("post eval anon stack")
2933-
self.nameStack = saved_namestack
2934-
self.stack = saved_stack
2935-
self.anon_union_counter = [-1, 0]
2936-
29372939
self._evaluate_stack(tok.type)
29382940
self.stack = []
29392941
self.nameStack = []
@@ -2995,6 +2997,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
29952997
"nameStackHistory",
29962998
"anon_struct_counter",
29972999
"anon_union_counter",
3000+
"anon_class_counter",
29983001
"_classes_brace_level",
29993002
"_forward_decls",
30003003
"stack",

test/test_CppHeaderParser.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ def test_property(self):
855855
"constant": 0,
856856
"name": "a",
857857
"reference": 0,
858-
"type": "",
858+
"type": "<anon-struct-1>",
859859
"static": 0,
860860
"pointer": 0,
861861
}
@@ -1037,7 +1037,7 @@ def test_property(self):
10371037
"constant": 0,
10381038
"name": "u",
10391039
"reference": 0,
1040-
"type": "union HogUnion",
1040+
"type": "HogUnion",
10411041
"static": 0,
10421042
"pointer": 0,
10431043
}
@@ -1051,13 +1051,13 @@ def test_property(self):
10511051

10521052
def test_union(self):
10531053
cmp_values = {
1054-
"name": "union HogUnion",
1054+
"name": "HogUnion",
10551055
"parent": self.cppHeader.classes["HogClass"],
10561056
"declaration_method": "union",
10571057
}
10581058
self.assertEqual(
10591059
filter_dict_keys(
1060-
self.cppHeader.classes["HogClass::union HogUnion"], cmp_values.keys()
1060+
self.cppHeader.classes["HogClass::HogUnion"], cmp_values.keys()
10611061
),
10621062
cmp_values,
10631063
)
@@ -1073,7 +1073,7 @@ def test_union_member_a(self):
10731073
}
10741074
self.assertEqual(
10751075
filter_dict_keys(
1076-
self.cppHeader.classes["HogClass::union HogUnion"]["members"][0],
1076+
self.cppHeader.classes["HogClass::HogUnion"]["members"][0],
10771077
cmp_values.keys(),
10781078
),
10791079
cmp_values,
@@ -1090,7 +1090,7 @@ def test_union_member_b(self):
10901090
}
10911091
self.assertEqual(
10921092
filter_dict_keys(
1093-
self.cppHeader.classes["HogClass::union HogUnion"]["members"][1],
1093+
self.cppHeader.classes["HogClass::HogUnion"]["members"][1],
10941094
cmp_values.keys(),
10951095
),
10961096
cmp_values,
@@ -1891,6 +1891,14 @@ class Beans_TestCase(unittest.TestCase):
18911891
def setUp(self):
18921892
self.cppHeader = CppHeaderParser.CppHeader("TestSampleClass.h")
18931893

1894+
def test_public_props(self):
1895+
self.assertEqual(
1896+
len(self.cppHeader.classes["Beans"]["properties"]["public"]), 4
1897+
)
1898+
self.assertEqual(
1899+
self.cppHeader.classes["Beans"]["properties"]["public"][2]["name"], "data"
1900+
)
1901+
18941902
def test_anonymous_union_name(self):
18951903
return self.assertEqual(
18961904
self.cppHeader.classes["Beans"]["properties"]["public"][1]["name"], ""
@@ -2179,11 +2187,11 @@ def setUp(self):
21792187
self.cppHeader = CppHeaderParser.CppHeader("TestSampleClass.h")
21802188

21812189
def test_anon_struct_1_exists(self):
2182-
self.assertEqual("<anon-struct-1>" in self.cppHeader.classes, True)
2190+
self.assertEqual("<anon-struct-5>" in self.cppHeader.classes, True)
21832191

21842192
def test_beta_exists(self):
21852193
self.assertEqual(
2186-
self.cppHeader.classes["<anon-struct-1>"]["properties"]["public"][0][
2194+
self.cppHeader.classes["<anon-struct-5>"]["properties"]["public"][0][
21872195
"name"
21882196
],
21892197
"anon_struct_variable",
@@ -2469,7 +2477,7 @@ def setUp(self):
24692477
self.cppHeader = CppHeaderParser.CppHeader("TestSampleClass.h")
24702478

24712479
def test_Olive_exists(self):
2472-
self.assertEqual("union olive" in self.cppHeader.classes, True)
2480+
self.assertEqual("olive" in self.cppHeader.classes, True)
24732481

24742482
def test_union_member_x(self):
24752483
cmp_values = {
@@ -2482,7 +2490,7 @@ def test_union_member_x(self):
24822490
}
24832491
self.assertEqual(
24842492
filter_dict_keys(
2485-
self.cppHeader.classes["union olive"]["members"][0], cmp_values.keys()
2493+
self.cppHeader.classes["olive"]["members"][0], cmp_values.keys()
24862494
),
24872495
cmp_values,
24882496
)
@@ -3765,7 +3773,7 @@ def test_fn(self):
37653773
outer = self.cppHeader.classes["Outer"]
37663774
self.assertEqual(outer["parent"], None)
37673775

3768-
inner = self.cppHeader.classes["Outer::union "]
3776+
inner = self.cppHeader.classes["Outer::<anon-union-1>"]
37693777
self.assertIs(inner["parent"], outer)
37703778

37713779
self.assertEqual(2, len(outer["properties"]["public"]))

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