Skip to content

Commit ea5fcc8

Browse files
committed
Fixed language type for non-braced extern not reverting after semicolon
Semicolon is now considered a token and causes an extra extern brace pop like a closing curly brace does.
1 parent 1d3638b commit ea5fcc8

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

wpiformat/test/test_cidentlist.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,43 @@ def test_cidentlist():
171171
"#endif" + os.linesep))
172172
outputs.append((inputs[len(inputs) - 1][1], False, True))
173173

174+
inputs.append(("./Timer.hpp",
175+
"extern \"C\" void Timer1IntHandler();" + os.linesep + \
176+
os.linesep + \
177+
"class Timer {" + os.linesep + \
178+
"public:" + os.linesep + \
179+
" void Set(uint32_t newTime);" + os.linesep + \
180+
os.linesep + \
181+
" void Start();" + os.linesep + \
182+
os.linesep + \
183+
" void Stop();" + os.linesep + \
184+
os.linesep + \
185+
" uint16_t GetID() const;" + os.linesep + \
186+
os.linesep + \
187+
" static uint32_t GetTime();" + os.linesep + \
188+
os.linesep + \
189+
"private:" + os.linesep + \
190+
" friend void Timer1IntHandler();" + os.linesep + \
191+
"};" + os.linesep))
192+
outputs.append((
193+
"extern \"C\" void Timer1IntHandler(void);" + os.linesep + \
194+
os.linesep + \
195+
"class Timer {" + os.linesep + \
196+
"public:" + os.linesep + \
197+
" void Set(uint32_t newTime);" + os.linesep + \
198+
os.linesep + \
199+
" void Start();" + os.linesep + \
200+
os.linesep + \
201+
" void Stop();" + os.linesep + \
202+
os.linesep + \
203+
" uint16_t GetID() const;" + os.linesep + \
204+
os.linesep + \
205+
" static uint32_t GetTime();" + os.linesep + \
206+
os.linesep + \
207+
"private:" + os.linesep + \
208+
" friend void Timer1IntHandler();" + os.linesep + \
209+
"};" + os.linesep, True, True))
210+
174211
assert len(inputs) == len(outputs)
175212

176213
config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")

wpiformat/wpiformat/cidentlist.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def run_pipeline(self, config_file, name, lines):
3232
# "def\\s+\w+" matches preprocessor directives "#ifdef" and "#ifndef" so
3333
# their contents aren't used as a return type.
3434
extern_str = "(?P<ext_decl>extern \"C(\+\+)?\")\s+(?P<ext_brace>\{)?|"
35-
braces_str = "\{|\}|def\s+\w+|\w+\s+\w+\s*(?P<paren>\(\))"
35+
braces_str = "\{|\}|;|def\s+\w+|\w+\s+\w+\s*(?P<paren>\(\))"
3636
postfix_str = "(?=\s*(;|\{))"
3737
token_regex = re.compile(extern_str + braces_str + postfix_str)
3838

@@ -54,6 +54,12 @@ def run_pipeline(self, config_file, name, lines):
5454
elif token == "}":
5555
is_c = extern_brace_indices.pop()
5656

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 == ";":
5763
# If the next stack frame is from an extern without braces, pop
5864
# it.
5965
if extern_brace_indices[-1] >= EXTRA_POP_OFFSET:
@@ -66,7 +72,7 @@ def run_pipeline(self, config_file, name, lines):
6672
else:
6773
# Handling an extern without braces changing the language
6874
# type is done by treating it as a pseudo-brace that gets
69-
# popped as well when the next "}" is encountered.
75+
# popped as well when the next "}" or ";" is encountered.
7076
# The "extra pop" offset is used as a flag on the top stack
7177
# value that is checked whenever a pop is performed.
7278
extern_brace_indices.append(is_c + EXTRA_POP_OFFSET)

0 commit comments

Comments
 (0)