~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/clang-tools/run-clang-tools.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #!/usr/bin/env python3
  2 # SPDX-License-Identifier: GPL-2.0
  3 #
  4 # Copyright (C) Google LLC, 2020
  5 #
  6 # Author: Nathan Huckleberry <nhuck@google.com>
  7 #
  8 """A helper routine run clang-tidy and the clang static-analyzer on
  9 compile_commands.json.
 10 """
 11 
 12 import argparse
 13 import json
 14 import multiprocessing
 15 import subprocess
 16 import sys
 17 
 18 
 19 def parse_arguments():
 20     """Set up and parses command-line arguments.
 21     Returns:
 22         args: Dict of parsed args
 23         Has keys: [path, type]
 24     """
 25     usage = """Run clang-tidy or the clang static-analyzer on a
 26         compilation database."""
 27     parser = argparse.ArgumentParser(description=usage)
 28 
 29     type_help = "Type of analysis to be performed"
 30     parser.add_argument("type",
 31                         choices=["clang-tidy", "clang-analyzer"],
 32                         help=type_help)
 33     path_help = "Path to the compilation database to parse"
 34     parser.add_argument("path", type=str, help=path_help)
 35 
 36     checks_help = "Checks to pass to the analysis"
 37     parser.add_argument("-checks", type=str, default=None, help=checks_help)
 38     header_filter_help = "Pass the -header-filter value to the tool"
 39     parser.add_argument("-header-filter", type=str, default=None, help=header_filter_help)
 40 
 41     return parser.parse_args()
 42 
 43 
 44 def init(l, a):
 45     global lock
 46     global args
 47     lock = l
 48     args = a
 49 
 50 
 51 def run_analysis(entry):
 52     # Disable all checks, then re-enable the ones we want
 53     global args
 54     checks = None
 55     if args.checks:
 56         checks = args.checks.split(',')
 57     else:
 58         checks = ["-*"]
 59         if args.type == "clang-tidy":
 60             checks.append("linuxkernel-*")
 61         else:
 62             checks.append("clang-analyzer-*")
 63             checks.append("-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling")
 64     file = entry["file"]
 65     if not file.endswith(".c") and not file.endswith(".cpp"):
 66         with lock:
 67             print(f"Skipping non-C file: '{file}'", file=sys.stderr)
 68         return
 69     pargs = ["clang-tidy", "-p", args.path, "-checks=" + ",".join(checks)]
 70     if args.header_filter:
 71         pargs.append("-header-filter=" + args.header_filter)
 72     pargs.append(file)
 73     p = subprocess.run(pargs,
 74                        stdout=subprocess.PIPE,
 75                        stderr=subprocess.STDOUT,
 76                        cwd=entry["directory"])
 77     with lock:
 78         sys.stderr.buffer.write(p.stdout)
 79 
 80 
 81 def main():
 82     try:
 83         args = parse_arguments()
 84 
 85         lock = multiprocessing.Lock()
 86         pool = multiprocessing.Pool(initializer=init, initargs=(lock, args))
 87         # Read JSON data into the datastore variable
 88         with open(args.path, "r") as f:
 89             datastore = json.load(f)
 90             pool.map(run_analysis, datastore)
 91     except BrokenPipeError:
 92         # Python flushes standard streams on exit; redirect remaining output
 93         # to devnull to avoid another BrokenPipeError at shutdown
 94         devnull = os.open(os.devnull, os.O_WRONLY)
 95         os.dup2(devnull, sys.stdout.fileno())
 96         sys.exit(1)  # Python exits with error code 1 on EPIPE
 97 
 98 
 99 if __name__ == "__main__":
100     main()

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php