Skip to content

Commit 10b58a6

Browse files
committed
cidentlist.py no longer pushes braces within comments onto its stack
1 parent db4eb01 commit 10b58a6

File tree

2 files changed

+64
-40
lines changed

2 files changed

+64
-40
lines changed

wpiformat/test/test_cidentlist.py

+7
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,11 @@ def test_cidentlist():
205205
" friend void Timer1IntHandler();" + os.linesep + \
206206
"};" + os.linesep, True, True)
207207

208+
# Ensure comments with } in them don't mess up brace stack
209+
test.add_input("./Test.cpp",
210+
"void func() {" + os.linesep + \
211+
" // closing }" + os.linesep + \
212+
"}" + os.linesep)
213+
test.add_latest_input_as_output(True)
214+
208215
test.run(OutputType.FILE)

wpiformat/wpiformat/cidentlist.py

+57-40
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ def run_pipeline(self, config_file, name, lines):
3131
#
3232
# "def\\s+\w+" matches preprocessor directives "#ifdef" and "#ifndef" so
3333
# their contents aren't used as a return type.
34+
comment_str = "/\*|\*/|//|" + linesep + "|"
3435
extern_str = "(?P<ext_decl>extern \"C(\+\+)?\")\s+(?P<ext_brace>\{)?|"
3536
braces_str = "\{|\}|;|def\s+\w+|\w+\s+\w+\s*(?P<paren>\(\))"
3637
postfix_str = "(?=\s*(;|\{))"
37-
token_regex = regex.compile(extern_str + braces_str + postfix_str)
38+
token_regex = regex.compile(
39+
comment_str + extern_str + braces_str + postfix_str)
3840

3941
EXTRA_POP_OFFSET = 2
4042

@@ -46,49 +48,64 @@ def run_pipeline(self, config_file, name, lines):
4648
# is_c + pop offset == 3: C++ lang restore that needs extra brace pop
4749
extern_brace_indices = [is_c]
4850

51+
in_multicomment = False
52+
in_comment = False
4953
for match in token_regex.finditer(lines):
5054
token = match.group()
5155

52-
if token == "{":
53-
extern_brace_indices.append(is_c)
54-
elif token == "}":
55-
is_c = extern_brace_indices.pop()
56-
57-
# If the next stack frame is from an extern without braces, pop
58-
# it.
59-
if extern_brace_indices[-1] >= EXTRA_POP_OFFSET:
60-
is_c = extern_brace_indices[-1] - EXTRA_POP_OFFSET
61-
extern_brace_indices.pop()
62-
elif token == ";":
63-
# If the next stack frame is from an extern without braces, pop
64-
# it.
65-
if extern_brace_indices[-1] >= EXTRA_POP_OFFSET:
66-
is_c = extern_brace_indices[-1] - EXTRA_POP_OFFSET
67-
extern_brace_indices.pop()
68-
elif token.startswith("extern"):
69-
# Back up language setting first
70-
if match.group("ext_brace"):
56+
if token == "/*":
57+
in_multicomment = True
58+
elif token == "*/":
59+
in_multicomment = False
60+
in_comment = False
61+
elif token == "//":
62+
print("into comment")
63+
in_comment = True
64+
elif token == linesep:
65+
print("out of comment")
66+
in_comment = False
67+
elif not in_multicomment and not in_comment:
68+
if token == "{":
7169
extern_brace_indices.append(is_c)
72-
else:
73-
# Handling an extern without braces changing the language
74-
# type is done by treating it as a pseudo-brace that gets
75-
# popped as well when the next "}" or ";" is encountered.
76-
# The "extra pop" offset is used as a flag on the top stack
77-
# value that is checked whenever a pop is performed.
78-
extern_brace_indices.append(is_c + EXTRA_POP_OFFSET)
79-
80-
# Change language based on extern declaration
81-
if match.group("ext_decl") == "extern \"C\"":
82-
is_c = True
83-
else:
84-
is_c = False
85-
elif match.group(
86-
"paren") and "return " not in match.group() and is_c:
87-
# Replaces () with (void)
88-
output += lines[pos:match.span("paren")[0]] + "(void)"
89-
pos = match.span("paren")[0] + len("()")
90-
91-
file_changed = True
70+
elif token == "}":
71+
is_c = extern_brace_indices.pop()
72+
73+
# If the next stack frame is from an extern without braces,
74+
# pop it.
75+
if extern_brace_indices[-1] >= EXTRA_POP_OFFSET:
76+
is_c = extern_brace_indices[-1] - EXTRA_POP_OFFSET
77+
extern_brace_indices.pop()
78+
elif token == ";":
79+
# If the next stack frame is from an extern without braces,
80+
# pop it.
81+
if extern_brace_indices[-1] >= EXTRA_POP_OFFSET:
82+
is_c = extern_brace_indices[-1] - EXTRA_POP_OFFSET
83+
extern_brace_indices.pop()
84+
elif token.startswith("extern"):
85+
# Back up language setting first
86+
if match.group("ext_brace"):
87+
extern_brace_indices.append(is_c)
88+
else:
89+
# Handling an extern without braces changing the
90+
# language type is done by treating it as a pseudo-brace
91+
# that gets popped as well when the next "}" or ";" is
92+
# encountered. The "extra pop" offset is used as a flag
93+
# on the top stack value that is checked whenever a pop
94+
# is performed.
95+
extern_brace_indices.append(is_c + EXTRA_POP_OFFSET)
96+
97+
# Change language based on extern declaration
98+
if match.group("ext_decl") == "extern \"C\"":
99+
is_c = True
100+
else:
101+
is_c = False
102+
elif match.group(
103+
"paren") and "return " not in match.group() and is_c:
104+
# Replaces () with (void)
105+
output += lines[pos:match.span("paren")[0]] + "(void)"
106+
pos = match.span("paren")[0] + len("()")
107+
108+
file_changed = True
92109

93110
# Write rest of file if it wasn't all processed
94111
if pos < len(lines):

0 commit comments

Comments
 (0)