Skip to content

Commit 34e0d18

Browse files
Fix cidentlist handling of single quotes in numeric literals (wpilibsuite#288)
1 parent b3604cf commit 34e0d18

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

wpiformat/wpiformat/cidentlist.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def run_pipeline(self, config_file, name, lines):
4646
comment_str = r"/\*|\*/|//|" + linesep + r"|"
4747
string_str = r"\\\\|\\\"|\"|"
4848
char_str = r"\\'|'|"
49+
digits_str = r"(?P<digits>\d+)|"
4950
extern_str = r"(?P<ext_decl>extern \"C(\+\+)?\")\s+(?P<ext_brace>\{)?|"
5051
braces_str = r"\{|\}|;|def\s+\w+|\w+\**\s+\w+\s*(?P<paren>\(\))"
5152
postfix_str = r"(?=\s*(;|\{))"
@@ -54,6 +55,7 @@ def run_pipeline(self, config_file, name, lines):
5455
+ comment_str
5556
+ string_str
5657
+ char_str
58+
+ digits_str
5759
+ extern_str
5860
+ braces_str
5961
+ postfix_str
@@ -74,6 +76,7 @@ def run_pipeline(self, config_file, name, lines):
7476
in_singlecomment = False
7577
in_string = False
7678
in_char = False
79+
last_digit_end = -1
7780
for match in token_regex.finditer(lines):
7881
token = match.group()
7982

@@ -114,7 +117,7 @@ def run_pipeline(self, config_file, name, lines):
114117
elif token == "\\'":
115118
continue
116119
elif token == "'":
117-
if not in_string:
120+
if not in_string and match.start() != last_digit_end:
118121
in_char = not in_char
119122
elif in_string or in_char:
120123
# Tokens processed after this branch are ignored if they are in
@@ -165,6 +168,8 @@ def run_pipeline(self, config_file, name, lines):
165168
# Replaces () with (void)
166169
output += lines[pos : match.span("paren")[0]] + "(void)"
167170
pos = match.span("paren")[0] + len("()")
171+
elif match.group("digits"):
172+
last_digit_end = match.end()
168173

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

wpiformat/wpiformat/test/test_cidentlist.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,12 @@ def test_cidentlist():
494494
True,
495495
)
496496

497+
# Ensure single quotes in numeric literals are ignored
498+
test.add_input("./Test.cpp", "void func() { int x = 1'000; }")
499+
test.add_latest_input_as_output(True)
500+
501+
# Ensure single quotes after numeric literals are not ignored
502+
test.add_input("./Test.cpp", "void func() { std::cout << 1 << '0'; }")
503+
test.add_latest_input_as_output(True)
504+
497505
test.run(OutputType.FILE)

0 commit comments

Comments
 (0)