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): |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 21 | print >> sys.stderr, 'ccc: ' + message |
| 22 | sys.exit(1) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 23 | |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 24 | def quote(arg): |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 25 | if '"' in arg: |
| 26 | return repr(arg) |
| 27 | return arg |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 29 | def run(args): |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 30 | code = subprocess.call(args) |
| 31 | if code > 255: |
| 32 | code = 1 |
| 33 | if code: |
| 34 | sys.exit(code) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | fe87354 | 2008-04-21 21:58:05 +0000 | [diff] [blame] | 36 | def compile(args): |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 37 | command = 'gcc'.split() |
| 38 | run(command + args) |
| 39 | |
| 40 | def remove_pch_extension(path): |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 41 | i = path.rfind('.gch') |
| 42 | if i < 0: |
| 43 | return path |
| 44 | return path[:i] |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 46 | def analyze(clang, args,language,output,files,verbose,htmldir,file,analysis_type): |
Ted Kremenek | 30aba6d | 2008-05-27 23:17:16 +0000 | [diff] [blame] | 47 | if language.rfind("c++") >= 0: |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 48 | return |
| 49 | |
| 50 | print_args = [] |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 52 | if verbose: |
| 53 | # We MUST print to stderr. Some clients use the stdout output of |
| 54 | # gcc for various purposes. |
| 55 | print >> sys.stderr, ' '.join(['\n[LOCATION]:', os.getcwd(), '\n' ]) |
| 56 | i = 0 |
| 57 | while i < len(args): |
| 58 | print_args.append(''.join([ '\'', args[i], '\'' ])) |
| 59 | i += 1 |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 61 | RunAnalyzer = 0; |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 63 | if language.find("header") > 0: |
| 64 | target = remove_pch_extension(output) |
| 65 | command = 'cp'.split() |
| 66 | args = command + files + target.split() |
| 67 | else: |
| 68 | command = clang.split() + analysis_type.split() |
| 69 | args = command + args; |
| 70 | RunAnalyzer = 1 |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 72 | if verbose == 2: |
| 73 | print >> sys.stderr, '#SHELL (cd ' + os.getcwd() + ' && ' + ' '.join(command + print_args) + ')\n' |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 75 | if RunAnalyzer and htmldir is not None: |
| 76 | args.append('-o') |
| 77 | print_args.append('-o') |
| 78 | args.append(htmldir) |
| 79 | print_args.append(htmldir) |
| 80 | |
| 81 | if verbose == 1: |
| 82 | # We MUST print to stderr. Some clients use the stdout output of |
| 83 | # gcc for various purposes. |
| 84 | print >> sys.stderr, ' '.join(command+print_args) |
| 85 | print >> sys.stderr, '\n' |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | 6814447 | 2008-06-16 21:41:07 +0000 | [diff] [blame] | 87 | subprocess.call(args) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 88 | |
| 89 | def extension(path): |
| 90 | return path.split(".")[-1] |
| 91 | |
| 92 | def changeextension(path, newext): |
| 93 | i = path.rfind('.') |
| 94 | if i < 0: |
| 95 | return path |
| 96 | j = path.rfind('/', 0, i) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 97 | if j < 0: |
| 98 | return path[:i] + "." + newext |
| 99 | return path[j+1:i] + "." + newext |
| 100 | |
| 101 | def inferlanguage(extension): |
| 102 | if extension == "c": |
| 103 | return "c" |
| 104 | elif extension in ["cpp", "cc"]: |
| 105 | return "c++" |
| 106 | elif extension == "i": |
| 107 | return "c-cpp-output" |
| 108 | elif extension == "m": |
| 109 | return "objective-c" |
| 110 | elif extension == "mi": |
| 111 | return "objective-c-cpp-output" |
Ted Kremenek | cd85348 | 2008-06-10 18:56:59 +0000 | [diff] [blame] | 112 | elif extension in [ "s", "o", "a", "so" ]: |
Ted Kremenek | bfd6a3f | 2008-05-14 20:17:17 +0000 | [diff] [blame] | 113 | return "skip" |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 114 | else: |
| 115 | return "unknown" |
| 116 | |
| 117 | def main(args): |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 118 | old_args = args |
| 119 | action = 'link' |
| 120 | output = '' |
| 121 | compile_opts = [ ] |
| 122 | link_opts = [ ] |
| 123 | files = [] |
| 124 | save_temps = 0 |
| 125 | language = '' |
| 126 | |
| 127 | verbose = 0 |
| 128 | clang = "clang" |
| 129 | |
| 130 | # Forward to GCC. |
| 131 | compile(args) |
| 132 | |
| 133 | # Set the analyzer flag. |
| 134 | analysis_type = os.environ.get('CCC_ANALYZER_ANALYSIS') |
| 135 | |
| 136 | if analysis_type is not None: |
| 137 | analysis_type = "-" + analysis_type |
| 138 | else: |
| 139 | analysis_type = "-checker-cfref" |
| 140 | |
| 141 | # Determine the level of verbosity. |
| 142 | if os.environ.get('CCC_ANALYZER_VERBOSE') is not None: |
| 143 | verbose = 1 |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 145 | if os.environ.get('CCC_ANALYZER_LOG') is not None: |
| 146 | verbose = 2 |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 148 | # Determine what clang executable to use. |
| 149 | clang_env = os.environ.get('CLANG') |
| 150 | |
| 151 | if clang_env is not None: |
| 152 | clang = clang_env |
| 153 | |
| 154 | # Get the HTML output directory. |
| 155 | htmldir = None |
| 156 | |
| 157 | if analysis_type == "-checker-cfref": |
| 158 | htmldir = os.environ.get('CCC_ANALYZER_HTML') |
| 159 | |
| 160 | # Process the arguments. |
| 161 | i = 0 |
| 162 | while i < len(args): |
| 163 | arg = args[i] |
| 164 | |
| 165 | # Modes ccc supports |
| 166 | if arg == '-E': |
| 167 | action = 'preprocess' |
| 168 | if arg == '-c': |
| 169 | action = 'compile' |
| 170 | if arg.startswith('-print-prog-name'): |
| 171 | action = 'print-prog-name' |
| 172 | if arg == '-save-temps': |
| 173 | save_temps = 1 |
| 174 | |
| 175 | # Options with no arguments that should pass through |
| 176 | if arg in ['-v']: |
| 177 | compile_opts.append(arg) |
| 178 | link_opts.append(arg) |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 180 | # Options with one argument that should be ignored |
| 181 | if arg in ['--param', '-u']: |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 182 | i += 1 |
| 183 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 184 | # Prefix matches for the compile mode |
| 185 | if arg[:2] in ['-D', '-I', '-U', '-F' ]: |
| 186 | if not arg[2:]: |
| 187 | arg += args[i+1] |
| 188 | i += 1 |
| 189 | compile_opts.append(arg) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 191 | if arg[:5] in ['-std=']: |
| 192 | compile_opts.append(arg) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 194 | # Options with one argument that should pass through to compiler |
| 195 | if arg in [ '-include', '-idirafter', '-iprefix', |
| 196 | '-iquote', '-isystem', '-iwithprefix', |
| 197 | '-iwithprefixbefore']: |
| 198 | compile_opts.append(arg) |
| 199 | compile_opts.append(args[i+1]) |
| 200 | i += 1 |
| 201 | |
| 202 | # Options with no argument that should pass through to compiler |
| 203 | if arg in [ '-nostdinc', '-fobjc-gc-only', '-fobjc-gc' ]: |
| 204 | compile_opts.append(arg) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 206 | # Options with one argument that should pass through to linker |
| 207 | if arg == '-framework': |
| 208 | link_opts.append(arg) |
| 209 | link_opts.append(args[i+1]) |
| 210 | i += 1 |
Ted Kremenek | bfd6a3f | 2008-05-14 20:17:17 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 212 | # Options with one argument that should pass through to both |
| 213 | if arg in ['-isysroot', '-arch']: |
| 214 | compile_opts.append(arg) |
| 215 | compile_opts.append(args[i+1]) |
| 216 | link_opts.append(arg) |
| 217 | link_opts.append(args[i+1]) |
| 218 | i += 1 |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 220 | # Prefix matches for the link mode |
| 221 | if arg[:2] in ['-l', '-L', '-O', '-F']: |
| 222 | if arg == '-O': arg = '-O1' |
| 223 | if arg == '-Os': arg = '-O2' |
| 224 | link_opts.append(arg) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 226 | # Input files |
| 227 | if arg == '-filelist': |
| 228 | f = open(args[i+1]) |
| 229 | for line in f: |
| 230 | files.append(line.strip()) |
| 231 | f.close() |
| 232 | i += 1 |
| 233 | if arg == '-x': |
| 234 | language = args[i+1] |
| 235 | i += 1 |
| 236 | if arg[0] != '-': |
| 237 | files.append(arg) |
| 238 | |
| 239 | # Output file |
| 240 | if arg == '-o': |
| 241 | output = args[i+1] |
| 242 | i += 1 |
Ted Kremenek | 49061fa | 2008-06-04 20:49:03 +0000 | [diff] [blame] | 243 | |
| 244 | # Arguments we currently ignore with one option. |
| 245 | if arg in ['-install_name', '-exported_symbols_list', |
Ted Kremenek | fe4db8b | 2008-06-05 22:46:24 +0000 | [diff] [blame] | 246 | '-current_version', '-compatibility_version', '-init', '-e', |
Ted Kremenek | cd85348 | 2008-06-10 18:56:59 +0000 | [diff] [blame] | 247 | '-seg1addr', '-bundle_loader', '-multiply_defined']: |
Ted Kremenek | 49061fa | 2008-06-04 20:49:03 +0000 | [diff] [blame] | 248 | i += 1 |
| 249 | |
| 250 | # Arguments we currently ignore with three options. |
| 251 | if arg in ['-sectorder']: |
| 252 | i += 3 |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 253 | |
| 254 | i += 1 |
| 255 | |
| 256 | if action == 'print-prog-name': |
| 257 | # assume we can handle everything |
| 258 | print sys.argv[0] |
| 259 | return |
| 260 | |
| 261 | if not files: |
| 262 | error('no input files') |
| 263 | |
| 264 | if action == 'compile' or save_temps or action == 'link': |
| 265 | for i, file in enumerate(files): |
Ted Kremenek | 508b381 | 2008-05-24 16:14:34 +0000 | [diff] [blame] | 266 | file_language = language |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 267 | if not language: |
Ted Kremenek | 508b381 | 2008-05-24 16:14:34 +0000 | [diff] [blame] | 268 | file_language = inferlanguage(extension(file)) |
Ted Kremenek | 39165e2 | 2008-05-24 16:16:30 +0000 | [diff] [blame] | 269 | if file_language == "skip": |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 270 | continue |
| 271 | |
| 272 | if save_temps and action != "compile": |
| 273 | # Need a temporary output file |
| 274 | coutput = changeextension(file, "o"); |
| 275 | files[i] = coutput |
| 276 | elif not output: |
| 277 | coutput = changeextension(file, "o") |
| 278 | else: |
| 279 | coutput = output |
| 280 | analyze_args = [ file ] |
Ted Kremenek | b96ffdf | 2008-06-02 17:13:40 +0000 | [diff] [blame] | 281 | if file_language != 'unknown': |
| 282 | analyze_args = [ '-x', file_language ] + analyze_args |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 283 | analyze_args = analyze_args + compile_opts |
| 284 | analyze(clang, analyze_args, language, output, files, verbose, htmldir, file, analysis_type) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 285 | |
| 286 | if __name__ == '__main__': |
| 287 | main(sys.argv[1:]) |