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 a analysis build step to invocations of the Clang C/C++ compiler. |
Wez
2017/02/03 08:05:00
nit: "an"
Kevin M
2017/02/03 18:46:29
Done.
| |
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", | |
Wez
2017/02/03 08:05:00
nit: Not a biggie, but the other .py files in this
| |
21 "-fdiagnostics-show-option" | |
22 ] | |
23 | |
24 # Clang flags specifying which checkers should be run. | |
25 # The full list of checkers can be found at | |
26 # https://clang-analyzer.llvm.org/available_checks.html. | |
27 analyzer_option_flags = [ | |
28 "-analyzer-output=text", | |
29 "-analyzer-opt-analyze-nested-blocks", | |
30 "-analyzer-eagerly-assume", | |
31 "-analyzer-checker=core", | |
32 "-analyzer-checker=unix", | |
33 "-analyzer-checker=deadcode", | |
34 "-analyzer-checker=cplusplus", | |
35 "-analyzer-checker=security.insecureAPI.UncheckedReturn", | |
36 "-analyzer-checker=security.insecureAPI.getpw", | |
37 "-analyzer-checker=security.insecureAPI.gets", | |
38 "-analyzer-checker=security.insecureAPI.mktemp", | |
39 "-analyzer-checker=security.insecureAPI.mkstemp", | |
40 "-analyzer-checker=security.insecureAPI.vfork"] | |
18 | 41 |
19 | 42 |
20 def main(): | 43 def main(): |
21 parser = argparse.ArgumentParser(description=__doc__) | 44 args = sys.argv[1:] |
22 parser.add_argument('--clang-cc-path', | 45 assert len(args) > 0 |
Wez
2017/02/03 08:05:00
nit: Just |assert args| here?
Kevin M
2017/02/03 18:46:29
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 | 46 |
34 # Check that only one of --clang-cc-path or --clang-cxx-path are set. | 47 # Interleave 'analyzer_option_flags' flags w/"-Xanalyzer" so that Clang |
35 assert ((args.clang_cc_path != None) != (args.clang_cxx_path != None)) | 48 # passes them to the analysis tool. |
49 # e.g. ["-analyzer-foo", "-analyzer-bar"] => ["-Xanalyzer", "-analyzer-foo", | |
50 # "-Xanalyzer", "-analyzer-bar"] | |
51 interleaved_analyzer_flags = \ | |
52 list(itertools.chain(*zip(["-Xanalyzer"] * len(analyzer_option_flags), | |
Wez
2017/02/03 08:05:00
Can we line-wrap after "list(" to avoid the need f
Kevin M
2017/02/03 18:46:29
Done.
| |
53 analyzer_option_flags))) | |
Wez
2017/02/03 08:05:00
I think you can write this more simply, e.g:
redu
Kevin M
2017/02/03 18:46:29
Done.
| |
36 | 54 |
37 is_cxx = args.clang_cxx_path != None | 55 returncode, stderr = wrapper_utils.CaptureCommandStderr( |
38 env = os.environ | 56 wrapper_utils.CommandToRun(args + analyzer_enable_flags + |
39 env['CCC_ANALYZER_FORCE_ANALYZE_DEBUG_CODE'] = '0' | 57 interleaved_analyzer_flags)) |
Wez
2017/02/03 08:05:00
What happens to any stdout output here?
Kevin M
2017/02/03 18:46:29
Goes right to stdout. The stderr capturing feature
Wez
2017/02/03 19:27:54
Acknowledged.
| |
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 | 58 |
49 # TODO(kmarshall): Place the summarized output in a useful directory. | 59 # Post-process the analysis output and remove any errors for excluded files. |
Wez
2017/02/03 08:05:00
This should be a TODO?
Kevin M
2017/02/03 18:46:29
Done.
| |
50 temp_dir = tempfile.mkdtemp() | 60 sys.stderr.write(stderr) |
51 try: | 61 |
52 env['CCC_ANALYZER_HTML'] = temp_dir | 62 if returncode != 0: |
53 returncode, stderr = wrapper_utils.CaptureCommandStderr( | 63 # The analyzer is a little buggy and can crash midway through an AST parse |
54 wrapper_utils.CommandToRun([args.analyzer] + compile_args), env) | 64 # on otherwise buildable code. |
55 sys.stderr.write(stderr) | 65 # If it fails, we should recover gracefully and continue with the build. |
56 return returncode | 66 sys.stderr.write( |
57 finally: | 67 "WARNING: Clang analyzer exited with return code %d.\n" % (returncode)) |
58 shutil.rmtree(temp_dir) | 68 |
69 # Build the object file. | |
70 returncode, stderr = wrapper_utils.CaptureCommandStderr( | |
71 wrapper_utils.CommandToRun(args)) | |
72 | |
73 sys.stderr.write(stderr) | |
74 return returncode | |
59 | 75 |
60 if __name__ == "__main__": | 76 if __name__ == "__main__": |
61 sys.exit(main()) | 77 sys.exit(main()) |
OLD | NEW |