Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: build/toolchain/clang_static_analyzer_wrapper.py

Issue 2667853004: Remove dependency on scan-build wrapper script for Clang analysis builds. (Closed)
Patch Set: wez feedback Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 # 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 ]
18 36
19 37
20 def main(): 38 def main():
21 parser = argparse.ArgumentParser(description=__doc__) 39 args = sys.argv[1:]
22 parser.add_argument('--clang-cc-path', 40 assert(args)
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 41
34 # Check that only one of --clang-cc-path or --clang-cxx-path are set. 42 # Interleave 'analyzer_option_flags' flags w/'-Xanalyzer' so that Clang
35 assert ((args.clang_cc_path != None) != (args.clang_cxx_path != None)) 43 # passes them to the analysis tool.
44 # e.g. ['-analyzer-foo', '-analyzer-bar'] => ['-Xanalyzer', '-analyzer-foo',
45 # '-Xanalyzer', '-analyzer-bar']
46 interleaved_analyzer_flags = reduce(
47 lambda x, y: x + ['-Xanalyzer', y], analyzer_option_flags, [])
36 48
37 is_cxx = args.clang_cxx_path != None 49 returncode, stderr = wrapper_utils.CaptureCommandStderr(
38 env = os.environ 50 wrapper_utils.CommandToRun(args + analyzer_enable_flags +
39 env['CCC_ANALYZER_FORCE_ANALYZE_DEBUG_CODE'] = '0' 51 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 52
49 # TODO(kmarshall): Place the summarized output in a useful directory. 53 # TODO(kmarshall): Post-process the analysis output and remove any errors for
50 temp_dir = tempfile.mkdtemp() 54 # excluded files.
Wez 2017/02/03 19:27:54 nit: Add a bug # :D
Kevin M 2017/02/06 17:39:56 Done.
51 try: 55 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 56
60 if __name__ == "__main__": 57 if returncode != 0:
58 # The analyzer is a little buggy and can crash midway through an AST parse
59 # on otherwise buildable code.
60 # If it fails, we should recover gracefully and continue with the build.
61 sys.stderr.write(
62 'WARNING: Clang analyzer exited with return code %d.\n' % (returncode))
63
64 # Build the object file.
65 returncode, stderr = wrapper_utils.CaptureCommandStderr(
66 wrapper_utils.CommandToRun(args))
67
68 sys.stderr.write(stderr)
69 return returncode
70
71 if __name__ == '__main__':
61 sys.exit(main()) 72 sys.exit(main())
OLDNEW
« no previous file with comments | « DEPS ('k') | build/toolchain/gcc_toolchain.gni » ('j') | build/toolchain/gcc_toolchain.gni » ('J')

Powered by Google App Engine
This is Rietveld 408576698