run_clang_tidy.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. """Runs clang-tidy over all Carbon files."""
  3. __copyright__ = """
  4. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  5. Exceptions. See /LICENSE for license information.
  6. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. """
  8. import argparse
  9. import os
  10. import re
  11. import subprocess
  12. from pathlib import Path
  13. def main() -> None:
  14. parser = argparse.ArgumentParser(__doc__)
  15. # Copied from run-clang-tidy.py for forwarding.
  16. parser.add_argument("-fix", action="store_true", help="Apply fix-its")
  17. # Local flags.
  18. parser.add_argument("files", nargs="*", help="Files to fix")
  19. parsed_args = parser.parse_args()
  20. # If files are passed in, resolve them; otherwise, add a path filter.
  21. if parsed_args.files:
  22. files = [str(Path(f).resolve()) for f in parsed_args.files]
  23. else:
  24. files = ["^(?!.*/(bazel-|third_party)).*$"]
  25. # Set the repo root as the working directory.
  26. os.chdir(Path(__file__).resolve().parent.parent)
  27. # Ensure create_compdb has been run.
  28. subprocess.check_call(["./scripts/create_compdb.py"])
  29. # Use the run-clang-tidy version that should be with the rest of the clang
  30. # toolchain. This exposes us to version skew with user-installed clang
  31. # versions, but avoids version skew between the script and clang-tidy
  32. # itself.
  33. with Path(
  34. "./bazel-execroot/external/bazel_cc_toolchain/"
  35. "clang_detected_variables.bzl"
  36. ).open() as f:
  37. clang_vars = f.read()
  38. clang_bindir_match = re.search(r"clang_bindir = \"(.*)\"", clang_vars)
  39. assert clang_bindir_match is not None, clang_vars
  40. args = [str(Path(clang_bindir_match[1]).joinpath("run-clang-tidy"))]
  41. # Forward flags.
  42. if parsed_args.fix:
  43. args.append("-fix")
  44. # Run clang-tidy from clang-tools-extra.
  45. exit(subprocess.call(args + files))
  46. if __name__ == "__main__":
  47. main()