OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2017 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Invokes the Clang static analysis command using arguments provided on the | 6 """Adds an analysis build step to invocations of the Clang C/C++ compiler. |
7 command line. | 7 |
8 Usage: clang_static_analyzer_wrapper.py <compiler> [args...] | |
8 """ | 9 """ |
9 | 10 |
10 import argparse | 11 import argparse |
11 import fnmatch | 12 import fnmatch |
13 import itertools | |
12 import os | 14 import os |
13 import shutil | |
14 import sys | 15 import sys |
15 import tempfile | 16 import wrapper_utils |
16 | 17 |
17 import wrapper_utils | 18 # Flags used to enable analysis for Clang invocations. |
19 analyzer_enable_flags = [ | |
20 '--analyze', | |
21 '-fdiagnostics-show-option', | |
22 ] | |
23 | |
24 # Flags used to configure the analyzer's behavior. | |
25 analyzer_option_flags = [ | |
26 '-analyzer-checker=cplusplus', | |
27 '-analyzer-opt-analyze-nested-blocks', | |
28 '-analyzer-eagerly-assume', | |
29 '-analyzer-output=text', | |
30 '-analyzer-config', | |
31 'suppress-c++-stdlib=true', | |
32 | |
33 # List of checkers to execute. | |
34 # The full list of checkers can be found at | |
35 # https://clang-analyzer.llvm.org/available_checks.html. | |
36 '-analyzer-checker=core', | |
37 '-analyzer-checker=unix', | |
38 '-analyzer-checker=deadcode', | |
39 ] | |
18 | 40 |
19 | 41 |
20 def main(): | 42 def main(): |
21 parser = argparse.ArgumentParser(description=__doc__) | 43 args = sys.argv[1:] |
22 parser.add_argument('--clang-cc-path', | 44 assert(args) |
Nico
2017/03/10 01:55:02
nit:
assert args
(no parens needed)
Kevin M
2017/03/10 17:44:02
Done.
| |
23 help='Path to the clang compiler.', | |
24 metavar='PATH') | |
25 parser.add_argument('--clang-cxx-path', | |
26 help='Path to the clang++ compiler', | |
27 metavar='PATH') | |
28 parser.add_argument('--analyzer', | |
29 help='Path to the language-specific Clang analysis tool.', | |
30 required=True, | |
31 metavar='PATH') | |
32 args, compile_args = parser.parse_known_args() | |
33 | 45 |
34 # Check that only one of --clang-cc-path or --clang-cxx-path are set. | 46 # Interleave 'analyzer_option_flags' flags w/'-Xanalyzer' so that Clang |
35 assert ((args.clang_cc_path != None) != (args.clang_cxx_path != None)) | 47 # passes them to the analysis tool. |
48 # e.g. ['-analyzer-foo', '-analyzer-bar'] => ['-Xanalyzer', '-analyzer-foo', | |
49 # '-Xanalyzer', '-analyzer-bar'] | |
50 interleaved_analyzer_flags = reduce( | |
51 lambda x, y: x + ['-Xanalyzer', y], analyzer_option_flags, []) | |
Nico
2017/03/10 01:55:02
sum(zip([1, 2, 3], [4, 5, 6]), ())
=> maybe simpl
Kevin M
2017/03/10 17:44:02
Done.
| |
36 | 52 |
37 is_cxx = args.clang_cxx_path != None | 53 returncode, stderr = wrapper_utils.CaptureCommandStderr( |
38 env = os.environ | 54 wrapper_utils.CommandToRun(args + analyzer_enable_flags + |
39 env['CCC_ANALYZER_FORCE_ANALYZE_DEBUG_CODE'] = '0' | 55 interleaved_analyzer_flags)) |
40 env['CCC_ANALYZER_OUTPUT_FORMAT'] = 'text' | |
41 clang_path = args.clang_cxx_path or args.clang_cc_path | |
42 if is_cxx: | |
43 env['CCC_CXX'] = clang_path | |
44 env['CLANG_CXX'] = clang_path | |
45 else: | |
46 env['CCC_CC'] = clang_path | |
47 env['CLANG'] = clang_path | |
48 | 56 |
49 # TODO(kmarshall): Place the summarized output in a useful directory. | 57 # TODO(kmarshall): Post-process the analysis output and remove any eror text |
50 temp_dir = tempfile.mkdtemp() | 58 # originating from excluded files. (crbug.com/688616) |
51 try: | 59 sys.stderr.write(stderr) |
52 env['CCC_ANALYZER_HTML'] = temp_dir | |
53 returncode, stderr = wrapper_utils.CaptureCommandStderr( | |
54 wrapper_utils.CommandToRun([args.analyzer] + compile_args), env) | |
55 sys.stderr.write(stderr) | |
56 return returncode | |
57 finally: | |
58 shutil.rmtree(temp_dir) | |
59 | 60 |
60 if __name__ == "__main__": | 61 if returncode != 0: |
62 # The analyzer is a little buggy and can crash midway through an AST parse | |
63 # on otherwise buildable code. | |
64 # If it fails, we should recover gracefully and continue with the build. | |
65 sys.stderr.write( | |
66 'WARNING: Clang analyzer exited with return code %d.\n' % (returncode)) | |
Nico
2017/03/10 01:55:02
I still think we shouldn't have this in here. Plea
Kevin M
2017/03/10 17:44:02
There is the potential for new bugs and regression
| |
67 | |
68 # Build the object file. | |
69 returncode, stderr = wrapper_utils.CaptureCommandStderr( | |
70 wrapper_utils.CommandToRun(args)) | |
71 | |
72 sys.stderr.write(stderr) | |
73 return returncode | |
74 | |
75 if __name__ == '__main__': | |
61 sys.exit(main()) | 76 sys.exit(main()) |
OLD | NEW |