blob: 4912493db147e47b4980675e221624d8778cf190 [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
24def run(args):
25 print >> sys.stderr, ' '.join(args)
26 print >> sys.stderr, '\n'
27 code = subprocess.call(args)
28 if code > 255:
29 code = 1
30 if code:
31 sys.exit(code)
32
33def preprocess(args):
34 command = 'clang -E'.split()
35 run(command + args)
36
37def compile(args):
38 print >> sys.stderr, '\n'
39 command = 'gcc'.split()
40 run(command + args)
41
42def remove_pch_extension(path):
43 i = path.rfind('.gch')
44 if i < 0:
45 return path
46 return path[:i]
47
Ted Kremenek09c2ad62008-03-31 18:25:05 +000048def analyze(args,language,output,files,verbose,htmldir):
Ted Kremenekb0982882008-03-25 22:35:32 +000049 if language.find("c++") > 0:
50 return
51
Ted Kremenekb0982882008-03-25 22:35:32 +000052 print_args = []
53
Ted Kremenek09c2ad62008-03-31 18:25:05 +000054 if verbose:
55 print >> sys.stderr, ' '.join(['\n[LOCATION]:', os.getcwd(), '\n' ])
56 i = 0
57 while i < len(args):
Ted Kremenekb0982882008-03-25 22:35:32 +000058 print_args.append(''.join([ '\'', args[i], '\'' ]))
59 i += 1
60
61 if language.find("header") > 0:
62 target = remove_pch_extension(output)
63 command = 'cp'.split()
64 args = command + files + target.split()
65 else:
66 command = 'clang --grsimple'.split()
Ted Kremenek09c2ad62008-03-31 18:25:05 +000067 args = command + args + [ '-o', htmldir ]
68 print_args.append('-o')
69 print_args.append(htmldir)
70
71 if verbose:
72 print >> sys.stderr, ' '.join(command+print_args)
73 print >> sys.stderr, '\n'
Ted Kremenekb0982882008-03-25 22:35:32 +000074
Ted Kremenekb0982882008-03-25 22:35:32 +000075 subprocess.call(args)
76
77def link(args):
78 command = 'gcc'.split()
79 run(command + args)
80
81def extension(path):
82 return path.split(".")[-1]
83
84def changeextension(path, newext):
85 i = path.rfind('.')
86 if i < 0:
87 return path
88 j = path.rfind('/', 0, i)
89 print path
90 if j < 0:
91 return path[:i] + "." + newext
92 return path[j+1:i] + "." + newext
93
94def inferlanguage(extension):
95 if extension == "c":
96 return "c"
97 elif extension in ["cpp", "cc"]:
98 return "c++"
99 elif extension == "i":
100 return "c-cpp-output"
101 elif extension == "m":
102 return "objective-c"
103 elif extension == "mi":
104 return "objective-c-cpp-output"
105 else:
106 return "unknown"
107
108def main(args):
109 old_args = args
110 action = 'link'
111 output = ''
112 compile_opts = [ ]
113 link_opts = [ ]
114 files = []
115 save_temps = 0
116 language = ''
117
Ted Kremenek09c2ad62008-03-31 18:25:05 +0000118 verbose = 0
119
120 if os.environ.get('CCC_ANALYZER_VERBOSE') is not None:
121 verbose =1
122
123 htmldir = os.environ.get('CCC_ANALYZER_HTML')
124 if htmldir is None:
125 print >> sys.stderr, 'error: CCC_ANALYZER_HTML environment variable not set.'
126 sys.exit(1)
127
Ted Kremenekb0982882008-03-25 22:35:32 +0000128 i = 0
129 while i < len(args):
130 arg = args[i]
131
132 # Modes ccc supports
133 if arg == '-E':
134 action = 'preprocess'
135 if arg == '-c':
136 action = 'compile'
137 if arg.startswith('-print-prog-name'):
138 action = 'print-prog-name'
139 if arg == '-save-temps':
140 save_temps = 1
141
142 # Options with no arguments that should pass through
143 if arg in ['-v']:
144 compile_opts.append(arg)
145 link_opts.append(arg)
146
147 # Options with one argument that should be ignored
148 if arg in ['--param', '-arch', '-u']:
149 i += 1
150
151 # Prefix matches for the compile mode
152 if arg[:2] in ['-D', '-I', '-U', '-F']:
153 if not arg[2:]:
154 arg += args[i+1]
155 i += 1
156 compile_opts.append(arg)
157 if arg[:5] in ['-std=']:
158 compile_opts.append(arg)
159
160 # Options with one argument that should pass through
161 if arg in ['-include']:
162 compile_opts.append(arg)
163 compile_opts.append(args[i+1])
164 i += 1
165
166 # Prefix matches for the link mode
167 if arg[:2] in ['-l', '-L', '-O', '-F']:
168 if arg == '-O': arg = '-O1'
169 if arg == '-Os': arg = '-O2'
170 link_opts.append(arg)
171
172 # Options with one argument that should pass through
173 if arg in ['-framework']:
174 link_opts.append(arg)
175 link_opts.append(args[i+1])
176 i += 1
177
178 # Input files
179 if arg == '-filelist':
180 f = open(args[i+1])
181 for line in f:
182 files.append(line.strip())
183 f.close()
184 i += 1
185 if arg == '-x':
186 language = args[i+1]
187 i += 1
188 if arg[0] != '-':
189 files.append(arg)
190
191 # Output file
192 if arg == '-o':
193 output = args[i+1]
194 i += 1
195
196 i += 1
197
198 if action == 'print-prog-name':
199 # assume we can handle everything
200 print sys.argv[0]
201 return
202
203 if not files:
204 error('no input files')
205
206 if action == 'preprocess' or save_temps:
207 compile(args)
208
209 if action == 'compile' or save_temps:
210 for i, file in enumerate(files):
211 if not language:
212 language = inferlanguage(extension(file))
213 if save_temps and action != "compile":
214 # Need a temporary output file
215 coutput = changeextension(file, "o");
216 files[i] = coutput
217 elif not output:
218 coutput = changeextension(file, "o")
219 else:
220 coutput = output
221 analyze_args = [ file ]
222 if language != 'unknown':
223 analyze_args = analyze_args + [ '-x', language ]
224 analyze_args = analyze_args + compile_opts
Ted Kremenek09c2ad62008-03-31 18:25:05 +0000225 analyze(analyze_args, language, output, files, verbose, htmldir)
Ted Kremenekb0982882008-03-25 22:35:32 +0000226 compile(args)
227
228
229 if action == 'link':
230 link(args)
231# analyze(link_opts)
232
233if __name__ == '__main__':
234 main(sys.argv[1:])