blob: 8f69b0a390754c603ceac1d959b7eee3ebde7e8f [file] [log] [blame]
Ted Kremenekb0982882008-03-25 22:35:32 +00001#!/usr/bin/env python
2#
3# The LLVM Compiler Infrastructure
4#
5# This file is distributed under the University of Illinois Open Source
6# License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
10# A reduced version of the 'ccc' script that is designed to handle off
11# actual compilation to gcc, but run the code passed to gcc through the
12# static analyzer.
13#
14##===----------------------------------------------------------------------===##
15
16import sys
17import subprocess
18import os
19
20def error(message):
21 print >> sys.stderr, 'ccc: ' + message
22 sys.exit(1)
23
Seo Sanghyeond3894652008-04-04 11:02:21 +000024def quote(arg):
25 if '"' in arg:
26 return repr(arg)
27 return arg
28
Ted Kremenekb0982882008-03-25 22:35:32 +000029def run(args):
Ted Kremenek6e9d38e2008-04-07 23:27:54 +000030 # We MUST print to stderr. Some clients use the stdout output of
31 # gcc for various purposes.
32 print >> sys.stderr, ' '.join(map(quote, args))
33 print >> sys.stderr
Ted Kremenekb0982882008-03-25 22:35:32 +000034 code = subprocess.call(args)
35 if code > 255:
36 code = 1
37 if code:
38 sys.exit(code)
39
Ted Kremenekfe873542008-04-21 21:58:05 +000040def compile(args):
Ted Kremenek6e9d38e2008-04-07 23:27:54 +000041 # We MUST print to stderr. Some clients use the stdout output of
42 # gcc for various purposes.
Ted Kremenekb0982882008-03-25 22:35:32 +000043 print >> sys.stderr, '\n'
44 command = 'gcc'.split()
45 run(command + args)
46
47def remove_pch_extension(path):
48 i = path.rfind('.gch')
49 if i < 0:
50 return path
51 return path[:i]
52
Ted Kremenekf22eacb2008-04-18 22:00:56 +000053def analyze(clang, args,language,output,files,verbose,htmldir):
Ted Kremenek8cb53fb2008-04-03 21:29:11 +000054 if language.find("c++") > 0:
Ted Kremenekb0982882008-03-25 22:35:32 +000055 return
56
Ted Kremenekb0982882008-03-25 22:35:32 +000057 print_args = []
58
Ted Kremenek09c2ad62008-03-31 18:25:05 +000059 if verbose:
Ted Kremenek6e9d38e2008-04-07 23:27:54 +000060 # We MUST print to stderr. Some clients use the stdout output of
61 # gcc for various purposes.
Ted Kremenek09c2ad62008-03-31 18:25:05 +000062 print >> sys.stderr, ' '.join(['\n[LOCATION]:', os.getcwd(), '\n' ])
63 i = 0
64 while i < len(args):
Ted Kremenekb0982882008-03-25 22:35:32 +000065 print_args.append(''.join([ '\'', args[i], '\'' ]))
66 i += 1
Ted Kremeneka9525c92008-05-12 22:07:14 +000067
68
69 RunAnalyzer = 0;
70
Ted Kremenekb0982882008-03-25 22:35:32 +000071 if language.find("header") > 0:
72 target = remove_pch_extension(output)
73 command = 'cp'.split()
74 args = command + files + target.split()
75 else:
Ted Kremenekf22eacb2008-04-18 22:00:56 +000076 command = clang.split() + '-checker-cfref'.split()
Ted Kremenek69b64422008-03-31 21:20:32 +000077 args = command + args;
Ted Kremeneka9525c92008-05-12 22:07:14 +000078 RunAnalyzer = 1
79
80 if verbose == 2:
81 print >> sys.stderr, '#SHELL (cd ' + os.getcwd() + ' && ' + ' '.join(command + print_args) + ')\n'
82
83
84 if RunAnalyzer and htmldir is not None:
85 args.append('-o')
86 print_args.append('-o')
87 args.append(htmldir)
88 print_args.append(htmldir)
Ted Kremenek09c2ad62008-03-31 18:25:05 +000089
Ted Kremenek6e9d38e2008-04-07 23:27:54 +000090 if verbose:
91 # We MUST print to stderr. Some clients use the stdout output of
92 # gcc for various purposes.
Ted Kremenek09c2ad62008-03-31 18:25:05 +000093 print >> sys.stderr, ' '.join(command+print_args)
94 print >> sys.stderr, '\n'
Ted Kremeneka9525c92008-05-12 22:07:14 +000095
96
Ted Kremenekb0982882008-03-25 22:35:32 +000097 subprocess.call(args)
98
99def link(args):
100 command = 'gcc'.split()
101 run(command + args)
102
103def extension(path):
104 return path.split(".")[-1]
105
106def changeextension(path, newext):
107 i = path.rfind('.')
108 if i < 0:
109 return path
110 j = path.rfind('/', 0, i)
111 print path
112 if j < 0:
113 return path[:i] + "." + newext
114 return path[j+1:i] + "." + newext
115
116def inferlanguage(extension):
117 if extension == "c":
118 return "c"
119 elif extension in ["cpp", "cc"]:
120 return "c++"
121 elif extension == "i":
122 return "c-cpp-output"
123 elif extension == "m":
124 return "objective-c"
125 elif extension == "mi":
126 return "objective-c-cpp-output"
127 else:
128 return "unknown"
129
130def main(args):
131 old_args = args
132 action = 'link'
133 output = ''
134 compile_opts = [ ]
135 link_opts = [ ]
136 files = []
137 save_temps = 0
138 language = ''
139
Ted Kremenek09c2ad62008-03-31 18:25:05 +0000140 verbose = 0
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000141 clang = "clang"
142
Ted Kremenek09c2ad62008-03-31 18:25:05 +0000143
144 if os.environ.get('CCC_ANALYZER_VERBOSE') is not None:
Ted Kremeneka9525c92008-05-12 22:07:14 +0000145 verbose = 1
146
147 if os.environ.get('CCC_ANALYZER_LOG') is not None:
148 verbose = 2
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000149
150 clang_env = os.environ.get('CLANG')
151
152 if clang_env is not None:
153 clang = clang_env
Ted Kremenek09c2ad62008-03-31 18:25:05 +0000154
155 htmldir = os.environ.get('CCC_ANALYZER_HTML')
Ted Kremenek09c2ad62008-03-31 18:25:05 +0000156
Ted Kremenekb0982882008-03-25 22:35:32 +0000157 i = 0
158 while i < len(args):
159 arg = args[i]
160
161 # Modes ccc supports
162 if arg == '-E':
163 action = 'preprocess'
164 if arg == '-c':
165 action = 'compile'
166 if arg.startswith('-print-prog-name'):
167 action = 'print-prog-name'
168 if arg == '-save-temps':
169 save_temps = 1
170
171 # Options with no arguments that should pass through
172 if arg in ['-v']:
173 compile_opts.append(arg)
174 link_opts.append(arg)
175
176 # Options with one argument that should be ignored
Ted Kremenekd0eef022008-04-21 20:28:01 +0000177 if arg in ['--param', '-u']:
Ted Kremenekb0982882008-03-25 22:35:32 +0000178 i += 1
179
180 # Prefix matches for the compile mode
Ted Kremenekdc343002008-04-25 21:28:20 +0000181 if arg[:2] in ['-D', '-I', '-U', '-F' ]:
Ted Kremenekb0982882008-03-25 22:35:32 +0000182 if not arg[2:]:
183 arg += args[i+1]
184 i += 1
185 compile_opts.append(arg)
Ted Kremenekdc343002008-04-25 21:28:20 +0000186
Ted Kremenekb0982882008-03-25 22:35:32 +0000187 if arg[:5] in ['-std=']:
188 compile_opts.append(arg)
189
Nate Begeman4cd36032008-04-22 04:47:32 +0000190 # Options with one argument that should pass through to compiler
Ted Kremenekdc343002008-04-25 21:28:20 +0000191 if arg in [ '-include', '-idirafter', '-iprefix',
192 '-iquote', '-isystem', '-iwithprefix',
193 '-iwithprefixbefore']:
Ted Kremenekb0982882008-03-25 22:35:32 +0000194 compile_opts.append(arg)
195 compile_opts.append(args[i+1])
196 i += 1
Ted Kremenekdc343002008-04-25 21:28:20 +0000197
198 # Options with no argument that should pass through to compiler
Ted Kremenek73c083c2008-05-01 21:26:22 +0000199 if arg in [ '-nostdinc', '-fobjc-gc-only', '-fobjc-gc' ]:
Ted Kremenekdc343002008-04-25 21:28:20 +0000200 compile_opts.append(arg)
Ted Kremenekb0982882008-03-25 22:35:32 +0000201
Nate Begeman4cd36032008-04-22 04:47:32 +0000202 # Options with one argument that should pass through to linker
Ted Kremenek73c083c2008-05-01 21:26:22 +0000203 if arg == '-framework':
Nate Begeman4cd36032008-04-22 04:47:32 +0000204 link_opts.append(arg)
205 link_opts.append(args[i+1])
206 i += 1
207
208 # Options with one argument that should pass through to both
209 if arg in ['-isysroot', '-arch']:
210 compile_opts.append(arg)
211 compile_opts.append(args[i+1])
212 link_opts.append(arg)
213 link_opts.append(args[i+1])
214 i += 1
215
Ted Kremenekb0982882008-03-25 22:35:32 +0000216 # Prefix matches for the link mode
217 if arg[:2] in ['-l', '-L', '-O', '-F']:
218 if arg == '-O': arg = '-O1'
219 if arg == '-Os': arg = '-O2'
220 link_opts.append(arg)
221
Ted Kremenekb0982882008-03-25 22:35:32 +0000222 # Input files
223 if arg == '-filelist':
224 f = open(args[i+1])
225 for line in f:
226 files.append(line.strip())
227 f.close()
228 i += 1
229 if arg == '-x':
230 language = args[i+1]
231 i += 1
232 if arg[0] != '-':
233 files.append(arg)
234
235 # Output file
236 if arg == '-o':
237 output = args[i+1]
238 i += 1
239
240 i += 1
241
242 if action == 'print-prog-name':
243 # assume we can handle everything
244 print sys.argv[0]
245 return
246
247 if not files:
248 error('no input files')
249
250 if action == 'preprocess' or save_temps:
251 compile(args)
252
253 if action == 'compile' or save_temps:
254 for i, file in enumerate(files):
255 if not language:
256 language = inferlanguage(extension(file))
257 if save_temps and action != "compile":
258 # Need a temporary output file
259 coutput = changeextension(file, "o");
260 files[i] = coutput
261 elif not output:
262 coutput = changeextension(file, "o")
263 else:
264 coutput = output
265 analyze_args = [ file ]
266 if language != 'unknown':
267 analyze_args = analyze_args + [ '-x', language ]
268 analyze_args = analyze_args + compile_opts
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000269 analyze(clang, analyze_args, language, output, files, verbose, htmldir)
Ted Kremenekb0982882008-03-25 22:35:32 +0000270 compile(args)
271
272
273 if action == 'link':
274 link(args)
275# analyze(link_opts)
276
277if __name__ == '__main__':
278 main(sys.argv[1:])