2 # SPDX-License-Identifier: GPL-2.0
4 # Copyright (C) Google LLC, 2020
6 # Author: Nathan Huckleberry <nhuck@google.com>
8 """A helper routine run clang-tidy and the clang static-analyzer on
14 import multiprocessing
20 def parse_arguments():
21 """Set up and parses command-line arguments.
23 args: Dict of parsed args
24 Has keys: [path, type]
26 usage = """Run clang-tidy or the clang static-analyzer on a
27 compilation database."""
28 parser = argparse.ArgumentParser(description=usage)
30 type_help = "Type of analysis to be performed"
31 parser.add_argument("type",
32 choices=["clang-tidy", "clang-analyzer"],
34 path_help = "Path to the compilation database to parse"
35 parser.add_argument("path", type=str, help=path_help)
37 return parser.parse_args()
47 def run_analysis(entry):
48 # Disable all checks, then re-enable the ones we want
49 checks = "-checks=-*,"
50 if args.type == "clang-tidy":
51 checks += "linuxkernel-*"
53 checks += "clang-analyzer-*"
54 checks += ",-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
55 p = subprocess.run(["clang-tidy", "-p", args.path, checks, entry["file"]],
56 stdout=subprocess.PIPE,
57 stderr=subprocess.STDOUT,
58 cwd=entry["directory"])
60 sys.stderr.buffer.write(p.stdout)
64 args = parse_arguments()
66 lock = multiprocessing.Lock()
67 pool = multiprocessing.Pool(initializer=init, initargs=(lock, args))
68 # Read JSON data into the datastore variable
69 with open(args.path, "r") as f:
70 datastore = json.load(f)
71 pool.map(run_analysis, datastore)
74 if __name__ == "__main__":