blob: a9882a3f51e81189e3212bdf00939012a2fcb2f7 [file] [log] [blame]
kmarshall520f9512017-01-24 23:25:011#!/usr/bin/env python
kmarshall6694c0e2017-03-10 19:47:212# Copyright 2017 The Chromium Authors. All rights reserved.
kmarshall520f9512017-01-24 23:25:013# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
kmarshall6694c0e2017-03-10 19:47:216"""Adds an analysis build step to invocations of the Clang C/C++ compiler.
7
8Usage: clang_static_analyzer_wrapper.py <compiler> [args...]
kmarshall520f9512017-01-24 23:25:019"""
10
11import argparse
12import fnmatch
kmarshall6694c0e2017-03-10 19:47:2113import itertools
kmarshall520f9512017-01-24 23:25:0114import os
kmarshall520f9512017-01-24 23:25:0115import sys
kmarshall520f9512017-01-24 23:25:0116import wrapper_utils
17
kmarshall6694c0e2017-03-10 19:47:2118# Flags used to enable analysis for Clang invocations.
19analyzer_enable_flags = [
20 '--analyze',
kmarshall6694c0e2017-03-10 19:47:2121]
22
23# Flags used to configure the analyzer's behavior.
24analyzer_option_flags = [
kmarshall5847e4b2017-03-16 21:39:5125 '-fdiagnostics-show-option',
kmarshall6694c0e2017-03-10 19:47:2126 '-analyzer-checker=cplusplus',
27 '-analyzer-opt-analyze-nested-blocks',
kmarshall6694c0e2017-03-10 19:47:2128 '-analyzer-output=text',
29 '-analyzer-config',
30 'suppress-c++-stdlib=true',
31
32# List of checkers to execute.
33# The full list of checkers can be found at
34# https://clang-analyzer.llvm.org/available_checks.html.
35 '-analyzer-checker=core',
36 '-analyzer-checker=unix',
37 '-analyzer-checker=deadcode',
38]
39
kmarshall520f9512017-01-24 23:25:0140
kmarshall5847e4b2017-03-16 21:39:5141# Prepends every element of a list |args| with |token|.
42# e.g. ['-analyzer-foo', '-analyzer-bar'] => ['-Xanalyzer', '-analyzer-foo',
43# '-Xanalyzer', '-analyzer-bar']
44def interleave_args(args, token):
45 return list(sum(zip([token] * len(args), args), ()))
46
47
kmarshall520f9512017-01-24 23:25:0148def main():
kmarshall5847e4b2017-03-16 21:39:5149 parser = argparse.ArgumentParser()
50 parser.add_argument('--mode',
51 choices=['clang', 'cl'],
52 required=True,
53 help='Specifies the compiler argument convention to use.')
54 parser.add_argument('args', nargs=argparse.REMAINDER)
55 parsed_args = parser.parse_args()
kmarshall520f9512017-01-24 23:25:0156
kmarshall5847e4b2017-03-16 21:39:5157 prefix = '-Xclang' if parsed_args.mode == 'cl' else '-Xanalyzer'
58 cmd = parsed_args.args + analyzer_enable_flags + \
59 interleave_args(analyzer_option_flags, prefix)
kmarshall6694c0e2017-03-10 19:47:2160 returncode, stderr = wrapper_utils.CaptureCommandStderr(
kmarshall5847e4b2017-03-16 21:39:5161 wrapper_utils.CommandToRun(cmd))
kmarshall6694c0e2017-03-10 19:47:2162 sys.stderr.write(stderr)
kmarshall6694c0e2017-03-10 19:47:2163
kmarshall5847e4b2017-03-16 21:39:5164 returncode, stderr = wrapper_utils.CaptureCommandStderr(
65 wrapper_utils.CommandToRun(parsed_args.args))
66 sys.stderr.write(stderr)
67
68 return returncode
kmarshall6694c0e2017-03-10 19:47:2169
70if __name__ == '__main__':
kmarshall520f9512017-01-24 23:25:0171 sys.exit(main())