Skip to content

Commit

Permalink
Fix using declaration parsing (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Aug 29, 2023
1 parent 6eed30a commit b3e5a67
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
16 changes: 16 additions & 0 deletions wpiformat/wpiformat/test/test_usingdeclaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,20 @@ def test_usingdeclaration():
)
test.add_output("", True)

# "using" in comment without trailing semicolon
test.add_input(
"./Test.h",
"// using"
+ os.linesep
+ "void func() {"
+ os.linesep
+ " using A = int;"
+ os.linesep
+ " using B = int;"
+ os.linesep
+ "}"
+ os.linesep,
)
test.add_output("", True)

test.run(OutputType.STDOUT)
17 changes: 8 additions & 9 deletions wpiformat/wpiformat/usingdeclaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run_pipeline(self, config_file, name, lines):
# "using" declarations are scoped, so content inside any bracket pair is
# considered outside the global namespace.
token_regex = regex.compile(
r"/\*|\*/|//|\\\\|\\\"|\"|\\'|'|" + linesep + r"|\{|\}|using\s[^;]*;"
r"/\*|\*/|//|\\\\|\\\"|\"|\\'|'|" + linesep + r"|\{|\}|using\b"
)

brace_count = 0
Expand Down Expand Up @@ -69,13 +69,12 @@ def run_pipeline(self, config_file, name, lines):
linenum = lines.count(linesep, 0, match.start()) + 1
if "NOLINT" not in lines.splitlines()[linenum - 1]:
format_succeeded = False
print(
name
+ ": "
+ str(linenum)
+ ": '"
+ token
+ "' in global namespace"
)

# Extract using declaration
using_decl = lines[
match.start() : lines.find(";", match.start()) + 1
]

print(f"{name}: {linenum}: '{using_decl}' in global namespace")

return lines, format_succeeded

0 comments on commit b3e5a67

Please sign in to comment.