mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:41:06 +01:00
Finish making clang-tidy (mostly) work (again) and run -fix (#2312)
This does some more work to the run_clang_tidy.py wrapper script, and runs an example pass. "again" because it's really the proto fuzzer changes that broke it, it had been working before. "mostly" because there's still an issue within the proto fuzzer that it can't find "port/protobuf.h", i.e. https://github.com/google/libprotobuf-mutator/tree/master/port, but I'm still hesitant to add an include path there.
This commit is contained in:
@@ -57,7 +57,7 @@ print("Building compilation database...")
|
||||
# stand-alone files. This is a bit simpler than scraping the actual compile
|
||||
# actions and allows us to directly index header-only libraries easily and
|
||||
# pro-actively index the specific headers in the project.
|
||||
source_files_query = subprocess.run(
|
||||
source_files_query = subprocess.check_output(
|
||||
[
|
||||
bazel,
|
||||
"query",
|
||||
@@ -67,11 +67,9 @@ source_files_query = subprocess.run(
|
||||
"--incompatible_display_source_file_location",
|
||||
'filter(".*\\.(h|cpp|cc|c|cxx)$", kind("source file", deps(//...)))',
|
||||
],
|
||||
check=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
universal_newlines=True,
|
||||
).stdout
|
||||
)
|
||||
source_files = [
|
||||
Path(line.split(":")[0]) for line in source_files_query.splitlines()
|
||||
]
|
||||
@@ -98,7 +96,7 @@ print(
|
||||
|
||||
# Now collect the generated file labels.
|
||||
# cc_proto_library generates files, but they aren't seen with "generated file".
|
||||
generated_file_labels = subprocess.run(
|
||||
generated_file_labels = subprocess.check_output(
|
||||
[
|
||||
bazel,
|
||||
"query",
|
||||
@@ -111,23 +109,21 @@ generated_file_labels = subprocess.run(
|
||||
'kind("cc_proto_library", deps(//...))'
|
||||
),
|
||||
],
|
||||
check=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
universal_newlines=True,
|
||||
).stdout.splitlines()
|
||||
).splitlines()
|
||||
print("Found %d generated files..." % (len(generated_file_labels),))
|
||||
|
||||
# Directly build these labels so that indexing can find them. Allow this to
|
||||
# fail in case there are build errors in the client, and just warn the user
|
||||
# that they may be missing generated files.
|
||||
print("Building the generated files so that tools can find them...")
|
||||
subprocess.run([bazel, "build", "--keep_going"] + generated_file_labels)
|
||||
subprocess.check_call([bazel, "build", "--keep_going"] + generated_file_labels)
|
||||
|
||||
# Also build some specific targets that depend on external packages so those are
|
||||
# fetched and linked into the Bazel execution root. We try to use cheap files
|
||||
# where possible, but in some cases need to create a virtual include directory.
|
||||
subprocess.run(
|
||||
subprocess.check_call(
|
||||
[
|
||||
bazel,
|
||||
"build",
|
||||
@@ -137,6 +133,8 @@ subprocess.run(
|
||||
"@com_google_googletest//:LICENSE",
|
||||
"@com_googlesource_code_re2//:LICENSE",
|
||||
"@com_github_google_benchmark//:benchmark",
|
||||
"@com_google_libprotobuf_mutator//:LICENSE",
|
||||
"@com_google_protobuf//:any_proto",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
+35
-16
@@ -1,7 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""Runs clang-tidy over all Carbon files.
|
||||
"""
|
||||
"""Runs clang-tidy over all Carbon files."""
|
||||
|
||||
__copyright__ = """
|
||||
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
@@ -9,33 +8,53 @@ Exceptions. See /LICENSE for license information.
|
||||
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(__doc__)
|
||||
# Copied from run-clang-tidy.py for forwarding.
|
||||
parser.add_argument("-fix", action="store_true", help="Apply fix-its")
|
||||
# Local flags.
|
||||
parser.add_argument("files", nargs="*", help="Files to fix")
|
||||
parsed_args = parser.parse_args()
|
||||
|
||||
# If files are passed in, resolve them; otherwise, add a path filter.
|
||||
if parsed_args.files:
|
||||
files = [str(Path(f).resolve()) for f in parsed_args.files]
|
||||
else:
|
||||
files = ["^(?!.*/(bazel-|third_party)).*$"]
|
||||
|
||||
# Set the repo root as the working directory.
|
||||
os.chdir(Path(__file__).parent.parent)
|
||||
os.chdir(Path(__file__).resolve().parent.parent)
|
||||
# Ensure create_compdb has been run.
|
||||
subprocess.check_call(["./scripts/create_compdb.py"])
|
||||
|
||||
args = sys.argv[1:]
|
||||
if not args or args[0] == "--fix":
|
||||
args.append("^(?!.*/(bazel-|third_party)).*$")
|
||||
# Use the run-clang-tidy version that should be with the rest of the clang
|
||||
# toolchain. This exposes us to version skew with user-installed clang
|
||||
# versions, but avoids version skew between the script and clang-tidy
|
||||
# itself.
|
||||
with Path(
|
||||
"./bazel-execroot/external/bazel_cc_toolchain/"
|
||||
"clang_detected_variables.bzl"
|
||||
).open() as f:
|
||||
clang_vars = f.read()
|
||||
clang_bindir_match = re.search(r"clang_bindir = \"(.*)\"", clang_vars)
|
||||
assert clang_bindir_match is not None, clang_vars
|
||||
|
||||
args = [str(Path(clang_bindir_match[1]).joinpath("run-clang-tidy"))]
|
||||
|
||||
# Forward flags.
|
||||
if parsed_args.fix:
|
||||
args.append("-fix")
|
||||
|
||||
# Run clang-tidy from clang-tools-extra.
|
||||
exit(
|
||||
subprocess.call(
|
||||
[
|
||||
"./bazel-execroot/external/llvm-project/clang-tools-extra/"
|
||||
"clang-tidy/tool/run-clang-tidy.py",
|
||||
]
|
||||
+ args
|
||||
)
|
||||
)
|
||||
exit(subprocess.call(args + files))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user