diff options
author | bors-servo <infra@servo.org> | 2023-06-09 12:08:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-09 12:08:15 +0200 |
commit | b36db7f90edd2bdf6569413ea204e40b87324b2c (patch) | |
tree | 0951de0f5cfe9d8883bca5609ab9575cb83bf076 /python | |
parent | fc512cef42a8b5d0046cb467baac0c8bc8856af5 (diff) | |
parent | d4eadc9e3719b7017fd9d60ad1b5cc6533e253e7 (diff) | |
download | servo-b36db7f90edd2bdf6569413ea204e40b87324b2c.tar.gz servo-b36db7f90edd2bdf6569413ea204e40b87324b2c.zip |
Auto merge of #29860 - mrobinson:clang-format-python, r=jdm
Get clang-format from pip and upgrade to version 16
This allows relying on a specific version of clang-format and no longer use any version checks. In addition, we can use --dry-run -Werror in order to avoid having to run against every file individually.
Fix #29847.
Fix #29846.
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #29847 and fix #29846.
- [x] These changes do not require tests because they update dev infrastructure.
<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Diffstat (limited to 'python')
-rw-r--r-- | python/requirements.txt | 3 | ||||
-rw-r--r-- | python/servo/testing_commands.py | 47 |
2 files changed, 18 insertions, 32 deletions
diff --git a/python/requirements.txt b/python/requirements.txt index c151cb416bd..bbf19513285 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -36,4 +36,7 @@ six == 1.15 # For sending build notifications. notify-py == 0.3.42 +# For formatting C++ files. +clang-format ~= 16.0.0 + -e python/tidy diff --git a/python/servo/testing_commands.py b/python/servo/testing_commands.py index f2be571c9db..6ed56465ecc 100644 --- a/python/servo/testing_commands.py +++ b/python/servo/testing_commands.py @@ -19,7 +19,6 @@ from collections import OrderedDict import time import shutil import subprocess -from xml.etree.ElementTree import XML import wpt import wpt.manifestupdate @@ -297,7 +296,6 @@ class MachCommands(CommandBase): help="Run unit tests for tidy") @CommandArgument('--stylo', default=False, action="store_true", help="Only handle files in the stylo tree") - @CommandArgument('--force-cpp', default=False, action="store_true", help="Force CPP check") def test_tidy(self, all_files, no_progress, self_test, stylo, force_cpp=False, no_wpt=False): if self_test: return test_tidy.do_tests() @@ -310,18 +308,9 @@ class MachCommands(CommandBase): self.install_rustfmt() rustfmt_failed = self.call_rustup_run(["cargo", "fmt", "--", "--check"]) + print("Checking C++ files for tidiness...") env = self.build_env() - clangfmt_failed = False - available, cmd, files = setup_clangfmt(env) - if available: - for file in files: - stdout = check_output([cmd, "-output-replacements-xml", file], env=env) - if len(XML(stdout)) > 0: - print("%s is not formatted correctly." % file) - clangfmt_failed = True - elif force_cpp: - print("Error: can't find suitable clang-format version. Required with --force-cpp.") - return True + clangfmt_failed = not run_clang_format(env, ["--dry-run", "--Werror"]) if rustfmt_failed or clangfmt_failed: print("Run `./mach fmt` to fix the formatting") @@ -415,11 +404,8 @@ class MachCommands(CommandBase): description='Format the Rust and CPP source files with rustfmt and clang-format', category='testing') def format_code(self): - env = self.build_env() - available, cmd, files = setup_clangfmt(env) - if available and len(files) > 0: - check_call([cmd, "-i"] + files, env=env) + run_clang_format(env, ['-i']) self.install_rustfmt() return self.call_rustup_run(["cargo", "fmt"]) @@ -614,21 +600,18 @@ class MachCommands(CommandBase): [run_file, "|".join(tests), bin_path, base_dir]) -def setup_clangfmt(env): - cmd = "clang-format.exe" if sys.platform == "win32" else "clang-format" - try: - version = check_output([cmd, "--version"], env=env, universal_newlines=True).rstrip() - print(version) - if version.find("clang-format version {}.".format(CLANGFMT_VERSION)) == -1: - print("clang-format: wrong version (v{} required). Skipping CPP formatting.".format(CLANGFMT_VERSION)) - return False, None, None - except OSError: - print("clang-format not installed. Skipping CPP formatting.") - return False, None, None - gitcmd = ['git', 'ls-files'] - gitfiles = check_output(gitcmd + CLANGFMT_CPP_DIRS, universal_newlines=True).splitlines() - filtered = [line for line in gitfiles if line.endswith(".h") or line.endswith(".cpp")] - return True, cmd, filtered +def run_clang_format(env, args): + gitfiles = check_output( + ['git', 'ls-files'] + CLANGFMT_CPP_DIRS, + universal_newlines=True).splitlines() + files = [line for line in gitfiles if line.endswith(".h") or line.endswith(".cpp")] + clang_cmd = "clang-format.exe" if sys.platform == "win32" else "clang-format" + + if not files: + return True + + returncode = call([clang_cmd] + args + files, env=env) + return returncode == 0 def create_parser_create(): |