From 34e0d1813685c141b88debb15dc0f99beffbb762 Mon Sep 17 00:00:00 2001 From: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com> Date: Wed, 9 Oct 2024 21:15:36 -0700 Subject: [PATCH] Fix cidentlist handling of single quotes in numeric literals (#288) --- wpiformat/wpiformat/cidentlist.py | 7 ++++++- wpiformat/wpiformat/test/test_cidentlist.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/wpiformat/wpiformat/cidentlist.py b/wpiformat/wpiformat/cidentlist.py index 8557b42..b79bb65 100644 --- a/wpiformat/wpiformat/cidentlist.py +++ b/wpiformat/wpiformat/cidentlist.py @@ -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\d+)|" extern_str = r"(?Pextern \"C(\+\+)?\")\s+(?P\{)?|" braces_str = r"\{|\}|;|def\s+\w+|\w+\**\s+\w+\s*(?P\(\))" postfix_str = r"(?=\s*(;|\{))" @@ -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 @@ -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() @@ -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 @@ -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): diff --git a/wpiformat/wpiformat/test/test_cidentlist.py b/wpiformat/wpiformat/test/test_cidentlist.py index 39fa775..4dbebfa 100644 --- a/wpiformat/wpiformat/test/test_cidentlist.py +++ b/wpiformat/wpiformat/test/test_cidentlist.py @@ -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)