Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/env python3
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # Copyright (C) Google LLC, 2020
0005 #
0006 # Author: Nathan Huckleberry <nhuck@google.com>
0007 #
0008 """A helper routine run clang-tidy and the clang static-analyzer on
0009 compile_commands.json.
0010 """
0011 
0012 import argparse
0013 import json
0014 import multiprocessing
0015 import subprocess
0016 import sys
0017 
0018 
0019 def parse_arguments():
0020     """Set up and parses command-line arguments.
0021     Returns:
0022         args: Dict of parsed args
0023         Has keys: [path, type]
0024     """
0025     usage = """Run clang-tidy or the clang static-analyzer on a
0026         compilation database."""
0027     parser = argparse.ArgumentParser(description=usage)
0028 
0029     type_help = "Type of analysis to be performed"
0030     parser.add_argument("type",
0031                         choices=["clang-tidy", "clang-analyzer"],
0032                         help=type_help)
0033     path_help = "Path to the compilation database to parse"
0034     parser.add_argument("path", type=str, help=path_help)
0035 
0036     return parser.parse_args()
0037 
0038 
0039 def init(l, a):
0040     global lock
0041     global args
0042     lock = l
0043     args = a
0044 
0045 
0046 def run_analysis(entry):
0047     # Disable all checks, then re-enable the ones we want
0048     checks = "-checks=-*,"
0049     if args.type == "clang-tidy":
0050         checks += "linuxkernel-*"
0051     else:
0052         checks += "clang-analyzer-*"
0053         checks += ",-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
0054     p = subprocess.run(["clang-tidy", "-p", args.path, checks, entry["file"]],
0055                        stdout=subprocess.PIPE,
0056                        stderr=subprocess.STDOUT,
0057                        cwd=entry["directory"])
0058     with lock:
0059         sys.stderr.buffer.write(p.stdout)
0060 
0061 
0062 def main():
0063     args = parse_arguments()
0064 
0065     lock = multiprocessing.Lock()
0066     pool = multiprocessing.Pool(initializer=init, initargs=(lock, args))
0067     # Read JSON data into the datastore variable
0068     with open(args.path, "r") as f:
0069         datastore = json.load(f)
0070         pool.map(run_analysis, datastore)
0071 
0072 
0073 if __name__ == "__main__":
0074     main()