Skip to content

Commit

Permalink
Fix cidentlist handling of single quotes in numeric literals
Browse files Browse the repository at this point in the history
  • Loading branch information
KangarooKoala committed Oct 10, 2024
1 parent b3604cf commit ec1df28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion wpiformat/wpiformat/cidentlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def run_pipeline(self, config_file, name, lines):
comment_str = r"/\*|\*/|//|" + linesep + r"|"
string_str = r"\\\\|\\\"|\"|"
char_str = r"\\'|'|"
digits_str = r"(?P<digits>\d+)|"
extern_str = r"(?P<ext_decl>extern \"C(\+\+)?\")\s+(?P<ext_brace>\{)?|"
braces_str = r"\{|\}|;|def\s+\w+|\w+\**\s+\w+\s*(?P<paren>\(\))"
postfix_str = r"(?=\s*(;|\{))"
Expand All @@ -54,6 +55,7 @@ def run_pipeline(self, config_file, name, lines):
+ comment_str
+ string_str
+ char_str
+ digits_str
+ extern_str
+ braces_str
+ postfix_str
Expand All @@ -74,6 +76,7 @@ def run_pipeline(self, config_file, name, lines):
in_singlecomment = False
in_string = False
in_char = False
last_digit_end = -1
for match in token_regex.finditer(lines):
token = match.group()

Expand Down Expand Up @@ -114,7 +117,7 @@ def run_pipeline(self, config_file, name, lines):
elif token == "\\'":
continue
elif token == "'":
if not in_string:
if not in_string and match.start() != last_digit_end:
in_char = not in_char
elif in_string or in_char:
# Tokens processed after this branch are ignored if they are in
Expand Down Expand Up @@ -165,6 +168,8 @@ def run_pipeline(self, config_file, name, lines):
# Replaces () with (void)
output += lines[pos : match.span("paren")[0]] + "(void)"
pos = match.span("paren")[0] + len("()")
elif match.group("digits"):
last_digit_end = match.end()

# Write rest of file if it wasn't all processed
if pos < len(lines):
Expand Down
8 changes: 8 additions & 0 deletions wpiformat/wpiformat/test/test_cidentlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,12 @@ def test_cidentlist():
True,
)

# Ensure single quotes in numeric literals are ignored
test.add_input("./Test.cpp", "void func() { int x = 1'000; }")
test.add_latest_input_as_output(True)

# Ensure single quotes after numeric literals are not ignored
test.add_input("./Test.cpp", "void func() { std::cout << 1 << '0'; }")
test.add_latest_input_as_output(True)

test.run(OutputType.FILE)

0 comments on commit ec1df28

Please sign in to comment.