Skip to content

Commit d3b6f97

Browse files
committed
Rework method for renaming types in typedefs
- Removes nameStackHistory, which was only used for this
1 parent 7baa8b2 commit d3b6f97

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

CppHeaderParser/CppHeaderParser.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,29 @@ def _evaluate_typedef(self):
24432443
if name not in self.typedefs_order:
24442444
self.typedefs_order.append(name)
24452445

2446+
def _finish_struct_typedef(self):
2447+
# Look for the name of a typedef struct: struct typedef {...] StructName; or unions to get renamed
2448+
debug_print("finish struct typedef")
2449+
self.typedef_encountered = False
2450+
2451+
toks = self._consume_up_to([], ";")
2452+
2453+
# grab the first name token, TODO: typedef struct{} X, *PX;
2454+
for tok in toks:
2455+
if tok.type == "NAME":
2456+
new_name = tok.value
2457+
break
2458+
else:
2459+
return
2460+
2461+
type_name_to_rename = self.curClass["name"]
2462+
type_to_rename = self.classes[type_name_to_rename]
2463+
type_to_rename["name"] = new_name
2464+
# Now re install it in its new location
2465+
self.classes[new_name] = type_to_rename
2466+
if new_name != type_name_to_rename:
2467+
del self.classes[type_name_to_rename]
2468+
24462469
def _evaluate_property_stack(self, clearStack=True, addToVar=None):
24472470
"""Create a Property out of the name stack"""
24482471
global parseHistory
@@ -2756,12 +2779,13 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
27562779
"curAccessSpecifier changed/defaulted to %s", self.curAccessSpecifier
27572780
)
27582781
self.initextra()
2759-
# Old namestacks for a given level
2760-
self.nameStackHistory = []
2782+
27612783
self.anon_struct_counter = 0
27622784
self.anon_union_counter = 0
27632785
self.anon_class_counter = 0
27642786

2787+
self.typedef_encountered = False
2788+
27652789
#: Using directives in this header outside of class scope: key is
27662790
#: full name for lookup, value is :class:`.CppVariable`
27672791
self.using = {}
@@ -2978,6 +3002,10 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
29783002
if self.accessSpecifierStack:
29793003
self.curAccessSpecifier = self.accessSpecifierStack[-1]
29803004
self.accessSpecifierStack = self.accessSpecifierStack[:-1]
3005+
3006+
if self.curClass and self.typedef_encountered:
3007+
self._finish_struct_typedef()
3008+
29813009
if self.curClass and self.curClass["parent"]:
29823010
thisClass = self.curClass
29833011
self.curClass = self.curClass["parent"]
@@ -3094,10 +3122,10 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
30943122
"nameSpaces",
30953123
"curAccessSpecifier",
30963124
"accessSpecifierStack",
3097-
"nameStackHistory",
30983125
"anon_struct_counter",
30993126
"anon_union_counter",
31003127
"anon_class_counter",
3128+
"typedef_encountered",
31013129
"_forward_decls",
31023130
"stack",
31033131
"mainClass",
@@ -3269,8 +3297,6 @@ def _discard_ctor_initializer(self):
32693297
def _evaluate_stack(self, token=None):
32703298
"""Evaluates the current name stack"""
32713299

3272-
nameStackCopy = self.nameStack[:]
3273-
32743300
debug_print(
32753301
"Evaluating stack %s\n BraceDepth: %s (called from %s)",
32763302
self.nameStack,
@@ -3380,25 +3406,7 @@ def _evaluate_stack(self, token=None):
33803406
self._parse_enum()
33813407
self.stack = []
33823408
self.stmtTokens = []
3383-
elif (
3384-
len(self.nameStack) == 1
3385-
and len(self.nameStackHistory) > self.braceDepth
3386-
and (
3387-
self.nameStackHistory[self.braceDepth][0][0:2] == ["typedef", "struct"]
3388-
or self.nameStackHistory[self.braceDepth][0][0:2]
3389-
== ["typedef", "union"]
3390-
)
3391-
):
3392-
# Look for the name of a typedef struct: struct typedef {...] StructName; or unions to get renamed
3393-
debug_print("found the naming of a union")
3394-
type_name_to_rename = self.nameStackHistory[self.braceDepth][1]
3395-
new_name = self.nameStack[0]
3396-
type_to_rename = self.classes[type_name_to_rename]
3397-
type_to_rename["name"] = self.nameStack[0]
3398-
# Now re install it in its new location
3399-
self.classes[new_name] = type_to_rename
3400-
if new_name != type_name_to_rename:
3401-
del self.classes[type_name_to_rename]
3409+
34023410
elif self.stack[-1] == ";" and is_property_namestack(self.nameStack):
34033411
debug_print("trace")
34043412
if self.nameStack[0] in ("class", "struct") and len(self.stack) == 3:
@@ -3410,13 +3418,16 @@ def _evaluate_stack(self, token=None):
34103418
else:
34113419
self._evaluate_property_stack() # catches class props and structs in a namespace
34123420

3413-
elif (
3414-
self.nameStack[0] in ("class", "struct", "union")
3415-
or self.nameStack[0] == "typedef"
3416-
and self.nameStack[1] in ("struct", "union")
3421+
elif self.nameStack[0] in ("class", "struct", "union"):
3422+
debug_print("trace")
3423+
self._evaluate_class_stack()
3424+
3425+
elif self.nameStack[0] == "typedef" and self.nameStack[1] in (
3426+
"struct",
3427+
"union",
34173428
):
3418-
# Parsing a union can reuse much of the class parsing
34193429
debug_print("trace")
3430+
self.typedef_encountered = True
34203431
self._evaluate_class_stack()
34213432

34223433
elif not self.curClass:
@@ -3430,12 +3441,6 @@ def _evaluate_stack(self, token=None):
34303441
else:
34313442
debug_print("Discarded statement %s", self.nameStack)
34323443

3433-
className = self.curClass["name"] if self.curClass else ""
3434-
try:
3435-
self.nameStackHistory[self.braceDepth] = (nameStackCopy, className)
3436-
except:
3437-
self.nameStackHistory.append((nameStackCopy, className))
3438-
34393444
# its a little confusing to have some if/else above return and others not, and then clearning the nameStack down here
34403445
self.nameStack = []
34413446
self.lex.doxygenCommentCache = ""
@@ -3684,7 +3689,7 @@ def _strip_parent_keys(self):
36843689
for k in obj.keys():
36853690
trace_print("-Try key", k)
36863691
trace_print("-type", type(obj[k]))
3687-
if k in ["nameStackHistory", "parent", "_public_typedefs"]:
3692+
if k in ["parent", "_public_typedefs"]:
36883693
continue
36893694
if type(obj[k]) == list:
36903695
for i in obj[k]:

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