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 | |
Ted Kremenek | 2797b17 | 2008-06-30 16:12:30 +0000 | [diff] [blame] | 50 | RunAnalyzer = 0; |
| 51 | |
| 52 | if language.find("header") > 0: |
| 53 | target = remove_pch_extension(output) |
| 54 | command = ['cp'] |
| 55 | args = command + files + [ target ] |
| 56 | else: |
| 57 | command = clang.split() + analysis_type.split() |
| 58 | args = command + args; |
| 59 | RunAnalyzer = 1 |
| 60 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 61 | print_args = [] |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 63 | if verbose: |
| 64 | # We MUST print to stderr. Some clients use the stdout output of |
| 65 | # gcc for various purposes. |
| 66 | print >> sys.stderr, ' '.join(['\n[LOCATION]:', os.getcwd(), '\n' ]) |
| 67 | i = 0 |
| 68 | while i < len(args): |
| 69 | print_args.append(''.join([ '\'', args[i], '\'' ])) |
| 70 | i += 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: |
Ted Kremenek | 0100678 | 2008-07-02 23:16:10 +0000 | [diff] [blame] | 73 | print >> sys.stderr, '#SHELL (cd ' + os.getcwd() + ' && ' + ' '.join(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. |
Ted Kremenek | 0100678 | 2008-07-02 23:16:10 +0000 | [diff] [blame] | 84 | print >> sys.stderr, ' '.join(print_args) |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 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: |
Ted Kremenek | 0100678 | 2008-07-02 23:16:10 +0000 | [diff] [blame] | 139 | analysis_type = "-warn-dead-stores -checker-cfref" |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 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. |
Ted Kremenek | 0100678 | 2008-07-02 23:16:10 +0000 | [diff] [blame] | 155 | htmldir = os.environ.get('CCC_ANALYZER_HTML') |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 156 | |
| 157 | # Process the arguments. |
| 158 | i = 0 |
| 159 | while i < len(args): |
| 160 | arg = args[i] |
| 161 | |
| 162 | # Modes ccc supports |
| 163 | if arg == '-E': |
| 164 | action = 'preprocess' |
| 165 | if arg == '-c': |
| 166 | action = 'compile' |
| 167 | if arg.startswith('-print-prog-name'): |
| 168 | action = 'print-prog-name' |
| 169 | if arg == '-save-temps': |
| 170 | save_temps = 1 |
| 171 | |
| 172 | # Options with no arguments that should pass through |
| 173 | if arg in ['-v']: |
| 174 | compile_opts.append(arg) |
| 175 | link_opts.append(arg) |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 177 | # Options with one argument that should be ignored |
| 178 | if arg in ['--param', '-u']: |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 179 | i += 1 |
| 180 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 181 | # Prefix matches for the compile mode |
| 182 | if arg[:2] in ['-D', '-I', '-U', '-F' ]: |
| 183 | if not arg[2:]: |
| 184 | arg += args[i+1] |
| 185 | i += 1 |
| 186 | compile_opts.append(arg) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 188 | if arg[:5] in ['-std=']: |
| 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 | # Options with one argument that should pass through to compiler |
| 192 | if arg in [ '-include', '-idirafter', '-iprefix', |
| 193 | '-iquote', '-isystem', '-iwithprefix', |
| 194 | '-iwithprefixbefore']: |
| 195 | compile_opts.append(arg) |
| 196 | compile_opts.append(args[i+1]) |
| 197 | i += 1 |
| 198 | |
| 199 | # Options with no argument that should pass through to compiler |
| 200 | if arg in [ '-nostdinc', '-fobjc-gc-only', '-fobjc-gc' ]: |
| 201 | compile_opts.append(arg) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 203 | # Options with one argument that should pass through to linker |
| 204 | if arg == '-framework': |
| 205 | link_opts.append(arg) |
| 206 | link_opts.append(args[i+1]) |
| 207 | i += 1 |
Ted Kremenek | bfd6a3f | 2008-05-14 20:17:17 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 209 | # Options with one argument that should pass through to both |
| 210 | if arg in ['-isysroot', '-arch']: |
| 211 | compile_opts.append(arg) |
| 212 | compile_opts.append(args[i+1]) |
| 213 | link_opts.append(arg) |
| 214 | link_opts.append(args[i+1]) |
| 215 | i += 1 |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 217 | # Prefix matches for the link mode |
| 218 | if arg[:2] in ['-l', '-L', '-O', '-F']: |
| 219 | if arg == '-O': arg = '-O1' |
| 220 | if arg == '-Os': arg = '-O2' |
| 221 | link_opts.append(arg) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 223 | # Input files |
| 224 | if arg == '-filelist': |
| 225 | f = open(args[i+1]) |
| 226 | for line in f: |
| 227 | files.append(line.strip()) |
| 228 | f.close() |
| 229 | i += 1 |
| 230 | if arg == '-x': |
| 231 | language = args[i+1] |
| 232 | i += 1 |
| 233 | if arg[0] != '-': |
| 234 | files.append(arg) |
| 235 | |
| 236 | # Output file |
| 237 | if arg == '-o': |
| 238 | output = args[i+1] |
| 239 | i += 1 |
Ted Kremenek | 49061fa | 2008-06-04 20:49:03 +0000 | [diff] [blame] | 240 | |
| 241 | # Arguments we currently ignore with one option. |
| 242 | if arg in ['-install_name', '-exported_symbols_list', |
Ted Kremenek | fe4db8b | 2008-06-05 22:46:24 +0000 | [diff] [blame] | 243 | '-current_version', '-compatibility_version', '-init', '-e', |
Ted Kremenek | cd85348 | 2008-06-10 18:56:59 +0000 | [diff] [blame] | 244 | '-seg1addr', '-bundle_loader', '-multiply_defined']: |
Ted Kremenek | 49061fa | 2008-06-04 20:49:03 +0000 | [diff] [blame] | 245 | i += 1 |
| 246 | |
| 247 | # Arguments we currently ignore with three options. |
| 248 | if arg in ['-sectorder']: |
| 249 | i += 3 |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 250 | |
| 251 | i += 1 |
| 252 | |
| 253 | if action == 'print-prog-name': |
| 254 | # assume we can handle everything |
| 255 | print sys.argv[0] |
| 256 | return |
| 257 | |
| 258 | if not files: |
| 259 | error('no input files') |
| 260 | |
| 261 | if action == 'compile' or save_temps or action == 'link': |
| 262 | for i, file in enumerate(files): |
Ted Kremenek | 508b381 | 2008-05-24 16:14:34 +0000 | [diff] [blame] | 263 | file_language = language |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 264 | if not language: |
Ted Kremenek | 508b381 | 2008-05-24 16:14:34 +0000 | [diff] [blame] | 265 | file_language = inferlanguage(extension(file)) |
Ted Kremenek | 39165e2 | 2008-05-24 16:16:30 +0000 | [diff] [blame] | 266 | if file_language == "skip": |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 267 | continue |
| 268 | |
| 269 | if save_temps and action != "compile": |
| 270 | # Need a temporary output file |
| 271 | coutput = changeextension(file, "o"); |
| 272 | files[i] = coutput |
| 273 | elif not output: |
| 274 | coutput = changeextension(file, "o") |
| 275 | else: |
| 276 | coutput = output |
| 277 | analyze_args = [ file ] |
Ted Kremenek | b96ffdf | 2008-06-02 17:13:40 +0000 | [diff] [blame] | 278 | if file_language != 'unknown': |
| 279 | analyze_args = [ '-x', file_language ] + analyze_args |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 280 | analyze_args = analyze_args + compile_opts |
| 281 | analyze(clang, analyze_args, language, output, files, verbose, htmldir, file, analysis_type) |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 282 | |
| 283 | if __name__ == '__main__': |
| 284 | main(sys.argv[1:]) |