Cleanup indentation and remove some dead code.

Analyze files not compiled using "-c".  This fixes:

<rdar://problem/5961638> invoke checker when gcc is not called with "-c"

llvm-svn: 51545
This commit is contained in:
Ted Kremenek 2008-05-24 15:58:54 +00:00
parent a08154d85f
commit f18f460716
1 changed files with 194 additions and 215 deletions

View File

@ -18,85 +18,73 @@ import subprocess
import os import os
def error(message): def error(message):
print >> sys.stderr, 'ccc: ' + message print >> sys.stderr, 'ccc: ' + message
sys.exit(1) sys.exit(1)
def quote(arg): def quote(arg):
if '"' in arg: if '"' in arg:
return repr(arg) return repr(arg)
return arg return arg
def run(args): def run(args):
# We MUST print to stderr. Some clients use the stdout output of code = subprocess.call(args)
# gcc for various purposes. if code > 255:
#print >> sys.stderr, ' '.join(map(quote, args)) code = 1
#print >> sys.stderr if code:
code = subprocess.call(args) sys.exit(code)
if code > 255:
code = 1
if code:
sys.exit(code)
def compile(args): def compile(args):
# We MUST print to stderr. Some clients use the stdout output of
# gcc for various purposes.
#print >> sys.stderr, '\n'
command = 'gcc'.split() command = 'gcc'.split()
run(command + args) run(command + args)
def remove_pch_extension(path): def remove_pch_extension(path):
i = path.rfind('.gch') i = path.rfind('.gch')
if i < 0: if i < 0:
return path return path
return path[:i] return path[:i]
def analyze(clang, args,language,output,files,verbose,htmldir,file,analysis_type): def analyze(clang, args,language,output,files,verbose,htmldir,file,analysis_type):
if language.find("c++") > 0: if language.find("c++") > 0:
return return
print_args = []
print_args = [] if verbose:
# We MUST print to stderr. Some clients use the stdout output of
if verbose: # gcc for various purposes.
# We MUST print to stderr. Some clients use the stdout output of print >> sys.stderr, ' '.join(['\n[LOCATION]:', os.getcwd(), '\n' ])
# gcc for various purposes. i = 0
print >> sys.stderr, ' '.join(['\n[LOCATION]:', os.getcwd(), '\n' ]) while i < len(args):
i = 0 print_args.append(''.join([ '\'', args[i], '\'' ]))
while i < len(args): i += 1
print_args.append(''.join([ '\'', args[i], '\'' ]))
i += 1
RunAnalyzer = 0;
RunAnalyzer = 0; if language.find("header") > 0:
target = remove_pch_extension(output)
command = 'cp'.split()
args = command + files + target.split()
else:
command = clang.split() + analysis_type.split()
args = command + args;
RunAnalyzer = 1
if language.find("header") > 0: if verbose == 2:
target = remove_pch_extension(output) print >> sys.stderr, '#SHELL (cd ' + os.getcwd() + ' && ' + ' '.join(command + print_args) + ')\n'
command = 'cp'.split()
args = command + files + target.split()
else:
command = clang.split() + analysis_type.split()
args = command + args;
RunAnalyzer = 1
if verbose == 2: if RunAnalyzer and htmldir is not None:
print >> sys.stderr, '#SHELL (cd ' + os.getcwd() + ' && ' + ' '.join(command + print_args) + ')\n' args.append('-o')
print_args.append('-o')
args.append(htmldir)
print_args.append(htmldir)
if verbose == 1:
# We MUST print to stderr. Some clients use the stdout output of
# gcc for various purposes.
print >> sys.stderr, ' '.join(command+print_args)
print >> sys.stderr, '\n'
if RunAnalyzer and htmldir is not None: subprocess.call(args)
args.append('-o')
print_args.append('-o')
args.append(htmldir)
print_args.append(htmldir)
if verbose:
# We MUST print to stderr. Some clients use the stdout output of
# gcc for various purposes.
print >> sys.stderr, ' '.join(command+print_args)
print >> sys.stderr, '\n'
subprocess.call(args)
def link(args):
command = 'gcc'.split()
run(command + args)
def extension(path): def extension(path):
return path.split(".")[-1] return path.split(".")[-1]
@ -128,171 +116,162 @@ def inferlanguage(extension):
return "unknown" return "unknown"
def main(args): def main(args):
old_args = args old_args = args
action = 'link' action = 'link'
output = '' output = ''
compile_opts = [ ] compile_opts = [ ]
link_opts = [ ] link_opts = [ ]
files = [] files = []
save_temps = 0 save_temps = 0
language = '' language = ''
verbose = 0
clang = "clang"
# Forward to GCC.
compile(args)
# Set the analyzer flag.
analysis_type = os.environ.get('CCC_ANALYZER_ANALYSIS')
if analysis_type is not None:
analysis_type = "-" + analysis_type
else:
analysis_type = "-checker-cfref"
# Determine the level of verbosity.
if os.environ.get('CCC_ANALYZER_VERBOSE') is not None:
verbose = 1
verbose = 0 if os.environ.get('CCC_ANALYZER_LOG') is not None:
clang = "clang" verbose = 2
# Forward to GCC. # Determine what clang executable to use.
compile(args) clang_env = os.environ.get('CLANG')
if clang_env is not None:
clang = clang_env
# Get the HTML output directory.
htmldir = None
if analysis_type == "-checker-cfref":
htmldir = os.environ.get('CCC_ANALYZER_HTML')
# Process the arguments.
i = 0
while i < len(args):
arg = args[i]
# Modes ccc supports
if arg == '-E':
action = 'preprocess'
if arg == '-c':
action = 'compile'
if arg.startswith('-print-prog-name'):
action = 'print-prog-name'
if arg == '-save-temps':
save_temps = 1
# Options with no arguments that should pass through
if arg in ['-v']:
compile_opts.append(arg)
link_opts.append(arg)
# Set the analyzer flag. # Options with one argument that should be ignored
analysis_type = os.environ.get('CCC_ANALYZER_ANALYSIS') if arg in ['--param', '-u']:
if analysis_type is not None:
analysis_type = "-" + analysis_type
else:
analysis_type = "-checker-cfref"
# Determine the level of verbosity.
if os.environ.get('CCC_ANALYZER_VERBOSE') is not None:
verbose = 1
if os.environ.get('CCC_ANALYZER_LOG') is not None:
verbose = 2
# Determine what clang executable to use.
clang_env = os.environ.get('CLANG')
if clang_env is not None:
clang = clang_env
# Get the HTML output directory.
htmldir = None
if analysis_type == "-checker-cfref":
htmldir = os.environ.get('CCC_ANALYZER_HTML')
# Process the arguments.
i = 0
while i < len(args):
arg = args[i]
# Modes ccc supports
if arg == '-E':
action = 'preprocess'
if arg == '-c':
action = 'compile'
if arg.startswith('-print-prog-name'):
action = 'print-prog-name'
if arg == '-save-temps':
save_temps = 1
# Options with no arguments that should pass through
if arg in ['-v']:
compile_opts.append(arg)
link_opts.append(arg)
# Options with one argument that should be ignored
if arg in ['--param', '-u']:
i += 1
# Prefix matches for the compile mode
if arg[:2] in ['-D', '-I', '-U', '-F' ]:
if not arg[2:]:
arg += args[i+1]
i += 1
compile_opts.append(arg)
if arg[:5] in ['-std=']:
compile_opts.append(arg)
# Options with one argument that should pass through to compiler
if arg in [ '-include', '-idirafter', '-iprefix',
'-iquote', '-isystem', '-iwithprefix',
'-iwithprefixbefore']:
compile_opts.append(arg)
compile_opts.append(args[i+1])
i += 1
# Options with no argument that should pass through to compiler
if arg in [ '-nostdinc', '-fobjc-gc-only', '-fobjc-gc' ]:
compile_opts.append(arg)
# Options with one argument that should pass through to linker
if arg == '-framework':
link_opts.append(arg)
link_opts.append(args[i+1])
i += 1
# Options with one argument that should pass through to both
if arg in ['-isysroot', '-arch']:
compile_opts.append(arg)
compile_opts.append(args[i+1])
link_opts.append(arg)
link_opts.append(args[i+1])
i += 1
# Prefix matches for the link mode
if arg[:2] in ['-l', '-L', '-O', '-F']:
if arg == '-O': arg = '-O1'
if arg == '-Os': arg = '-O2'
link_opts.append(arg)
# Input files
if arg == '-filelist':
f = open(args[i+1])
for line in f:
files.append(line.strip())
f.close()
i += 1
if arg == '-x':
language = args[i+1]
i += 1
if arg[0] != '-':
files.append(arg)
# Output file
if arg == '-o':
output = args[i+1]
i += 1
i += 1 i += 1
if action == 'print-prog-name': # Prefix matches for the compile mode
# assume we can handle everything if arg[:2] in ['-D', '-I', '-U', '-F' ]:
print sys.argv[0] if not arg[2:]:
return arg += args[i+1]
i += 1
compile_opts.append(arg)
if not files: if arg[:5] in ['-std=']:
error('no input files') compile_opts.append(arg)
# if action == 'preprocess' or save_temps: # Options with one argument that should pass through to compiler
# compile(args) if arg in [ '-include', '-idirafter', '-iprefix',
'-iquote', '-isystem', '-iwithprefix',
'-iwithprefixbefore']:
compile_opts.append(arg)
compile_opts.append(args[i+1])
i += 1
# Options with no argument that should pass through to compiler
if arg in [ '-nostdinc', '-fobjc-gc-only', '-fobjc-gc' ]:
compile_opts.append(arg)
if action == 'compile' or save_temps: # Options with one argument that should pass through to linker
for i, file in enumerate(files): if arg == '-framework':
if not language: link_opts.append(arg)
language = inferlanguage(extension(file)) link_opts.append(args[i+1])
if language == "skip": i += 1
continue
if save_temps and action != "compile": # Options with one argument that should pass through to both
# Need a temporary output file if arg in ['-isysroot', '-arch']:
coutput = changeextension(file, "o"); compile_opts.append(arg)
files[i] = coutput compile_opts.append(args[i+1])
elif not output: link_opts.append(arg)
coutput = changeextension(file, "o") link_opts.append(args[i+1])
else: i += 1
coutput = output
analyze_args = [ file ]
if language != 'unknown':
analyze_args = [ '-x', language ] + analyze_args
analyze_args = analyze_args + compile_opts
analyze(clang, analyze_args, language, output, files, verbose, htmldir, file, analysis_type)
# compile(args)
# Prefix matches for the link mode
if arg[:2] in ['-l', '-L', '-O', '-F']:
if arg == '-O': arg = '-O1'
if arg == '-Os': arg = '-O2'
link_opts.append(arg)
# if action == 'link': # Input files
# link(args) if arg == '-filelist':
# # analyze(link_opts) f = open(args[i+1])
for line in f:
files.append(line.strip())
f.close()
i += 1
if arg == '-x':
language = args[i+1]
i += 1
if arg[0] != '-':
files.append(arg)
# Output file
if arg == '-o':
output = args[i+1]
i += 1
i += 1
if action == 'print-prog-name':
# assume we can handle everything
print sys.argv[0]
return
if not files:
error('no input files')
if action == 'compile' or save_temps or action == 'link':
for i, file in enumerate(files):
if not language:
language = inferlanguage(extension(file))
if language == "skip":
continue
if save_temps and action != "compile":
# Need a temporary output file
coutput = changeextension(file, "o");
files[i] = coutput
elif not output:
coutput = changeextension(file, "o")
else:
coutput = output
analyze_args = [ file ]
if language != 'unknown':
analyze_args = [ '-x', language ] + analyze_args
analyze_args = analyze_args + compile_opts
analyze(clang, analyze_args, language, output, files, verbose, htmldir, file, analysis_type)
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv[1:]) main(sys.argv[1:])