Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 1 | #!/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 | |
| 16 | import sys |
| 17 | import subprocess |
| 18 | import os |
| 19 | |
| 20 | def error(message): |
| 21 | print >> sys.stderr, 'ccc: ' + message |
| 22 | sys.exit(1) |
| 23 | |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 24 | def quote(arg): |
| 25 | if '"' in arg: |
| 26 | return repr(arg) |
| 27 | return arg |
| 28 | |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 29 | def run(args): |
Ted Kremenek | 6e9d38e | 2008-04-07 23:27:54 +0000 | [diff] [blame] | 30 | # 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 Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 34 | code = subprocess.call(args) |
| 35 | if code > 255: |
| 36 | code = 1 |
| 37 | if code: |
| 38 | sys.exit(code) |
| 39 | |
Ted Kremenek | fe87354 | 2008-04-21 21:58:05 +0000 | [diff] [blame] | 40 | def compile(args): |
Ted Kremenek | 6e9d38e | 2008-04-07 23:27:54 +0000 | [diff] [blame] | 41 | # We MUST print to stderr. Some clients use the stdout output of |
| 42 | # gcc for various purposes. |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 43 | print >> sys.stderr, '\n' |
| 44 | command = 'gcc'.split() |
| 45 | run(command + args) |
| 46 | |
| 47 | def remove_pch_extension(path): |
| 48 | i = path.rfind('.gch') |
| 49 | if i < 0: |
| 50 | return path |
| 51 | return path[:i] |
| 52 | |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 53 | def analyze(clang, args,language,output,files,verbose,htmldir): |
Ted Kremenek | 8cb53fb | 2008-04-03 21:29:11 +0000 | [diff] [blame] | 54 | if language.find("c++") > 0: |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 55 | return |
| 56 | |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 57 | print_args = [] |
| 58 | |
Ted Kremenek | 09c2ad6 | 2008-03-31 18:25:05 +0000 | [diff] [blame] | 59 | if verbose: |
Ted Kremenek | 6e9d38e | 2008-04-07 23:27:54 +0000 | [diff] [blame] | 60 | # We MUST print to stderr. Some clients use the stdout output of |
| 61 | # gcc for various purposes. |
Ted Kremenek | 09c2ad6 | 2008-03-31 18:25:05 +0000 | [diff] [blame] | 62 | print >> sys.stderr, ' '.join(['\n[LOCATION]:', os.getcwd(), '\n' ]) |
| 63 | i = 0 |
| 64 | while i < len(args): |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 65 | print_args.append(''.join([ '\'', args[i], '\'' ])) |
| 66 | i += 1 |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame^] | 67 | |
| 68 | |
| 69 | RunAnalyzer = 0; |
| 70 | |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 71 | if language.find("header") > 0: |
| 72 | target = remove_pch_extension(output) |
| 73 | command = 'cp'.split() |
| 74 | args = command + files + target.split() |
| 75 | else: |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 76 | command = clang.split() + '-checker-cfref'.split() |
Ted Kremenek | 69b6442 | 2008-03-31 21:20:32 +0000 | [diff] [blame] | 77 | args = command + args; |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame^] | 78 | 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 Kremenek | 09c2ad6 | 2008-03-31 18:25:05 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 6e9d38e | 2008-04-07 23:27:54 +0000 | [diff] [blame] | 90 | if verbose: |
| 91 | # We MUST print to stderr. Some clients use the stdout output of |
| 92 | # gcc for various purposes. |
Ted Kremenek | 09c2ad6 | 2008-03-31 18:25:05 +0000 | [diff] [blame] | 93 | print >> sys.stderr, ' '.join(command+print_args) |
| 94 | print >> sys.stderr, '\n' |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame^] | 95 | |
| 96 | |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 97 | subprocess.call(args) |
| 98 | |
| 99 | def link(args): |
| 100 | command = 'gcc'.split() |
| 101 | run(command + args) |
| 102 | |
| 103 | def extension(path): |
| 104 | return path.split(".")[-1] |
| 105 | |
| 106 | def 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 | |
| 116 | def 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 | |
| 130 | def 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 Kremenek | 09c2ad6 | 2008-03-31 18:25:05 +0000 | [diff] [blame] | 140 | verbose = 0 |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 141 | clang = "clang" |
| 142 | |
Ted Kremenek | 09c2ad6 | 2008-03-31 18:25:05 +0000 | [diff] [blame] | 143 | |
| 144 | if os.environ.get('CCC_ANALYZER_VERBOSE') is not None: |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame^] | 145 | verbose = 1 |
| 146 | |
| 147 | if os.environ.get('CCC_ANALYZER_LOG') is not None: |
| 148 | verbose = 2 |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 149 | |
| 150 | clang_env = os.environ.get('CLANG') |
| 151 | |
| 152 | if clang_env is not None: |
| 153 | clang = clang_env |
Ted Kremenek | 09c2ad6 | 2008-03-31 18:25:05 +0000 | [diff] [blame] | 154 | |
| 155 | htmldir = os.environ.get('CCC_ANALYZER_HTML') |
Ted Kremenek | 09c2ad6 | 2008-03-31 18:25:05 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 157 | 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 Kremenek | d0eef02 | 2008-04-21 20:28:01 +0000 | [diff] [blame] | 177 | if arg in ['--param', '-u']: |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 178 | i += 1 |
| 179 | |
| 180 | # Prefix matches for the compile mode |
Ted Kremenek | dc34300 | 2008-04-25 21:28:20 +0000 | [diff] [blame] | 181 | if arg[:2] in ['-D', '-I', '-U', '-F' ]: |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 182 | if not arg[2:]: |
| 183 | arg += args[i+1] |
| 184 | i += 1 |
| 185 | compile_opts.append(arg) |
Ted Kremenek | dc34300 | 2008-04-25 21:28:20 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 187 | if arg[:5] in ['-std=']: |
| 188 | compile_opts.append(arg) |
| 189 | |
Nate Begeman | 4cd3603 | 2008-04-22 04:47:32 +0000 | [diff] [blame] | 190 | # Options with one argument that should pass through to compiler |
Ted Kremenek | dc34300 | 2008-04-25 21:28:20 +0000 | [diff] [blame] | 191 | if arg in [ '-include', '-idirafter', '-iprefix', |
| 192 | '-iquote', '-isystem', '-iwithprefix', |
| 193 | '-iwithprefixbefore']: |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 194 | compile_opts.append(arg) |
| 195 | compile_opts.append(args[i+1]) |
| 196 | i += 1 |
Ted Kremenek | dc34300 | 2008-04-25 21:28:20 +0000 | [diff] [blame] | 197 | |
| 198 | # Options with no argument that should pass through to compiler |
Ted Kremenek | 73c083c | 2008-05-01 21:26:22 +0000 | [diff] [blame] | 199 | if arg in [ '-nostdinc', '-fobjc-gc-only', '-fobjc-gc' ]: |
Ted Kremenek | dc34300 | 2008-04-25 21:28:20 +0000 | [diff] [blame] | 200 | compile_opts.append(arg) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 201 | |
Nate Begeman | 4cd3603 | 2008-04-22 04:47:32 +0000 | [diff] [blame] | 202 | # Options with one argument that should pass through to linker |
Ted Kremenek | 73c083c | 2008-05-01 21:26:22 +0000 | [diff] [blame] | 203 | if arg == '-framework': |
Nate Begeman | 4cd3603 | 2008-04-22 04:47:32 +0000 | [diff] [blame] | 204 | 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 Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 216 | # 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 Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 222 | # 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 Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 269 | analyze(clang, analyze_args, language, output, files, verbose, htmldir) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 270 | compile(args) |
| 271 | |
| 272 | |
| 273 | if action == 'link': |
| 274 | link(args) |
| 275 | # analyze(link_opts) |
| 276 | |
| 277 | if __name__ == '__main__': |
| 278 | main(sys.argv[1:]) |