diff --git a/utils/style_check.py b/utils/style_check.py new file mode 100644 index 0000000..61b1e2c --- /dev/null +++ b/utils/style_check.py @@ -0,0 +1,87 @@ +#===-- Style_check.py - Simple style checking for llvm *- Python -*- +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +#===---------------------------------------------------------------------- + +from optparse import OptionParser + +parser = OptionParser("usage: %prog arg1 arg2 ... argn [options]"); +parser.add_option("", "--no-check-for-tabs", action="store_false", + dest="doCheckForTabs", + default = True, help="don't check for tabs") +parser.add_option("", "--no-check-line-length", action="store_false", + dest="doCheckLineLength", + default = True, help="don't check line length") +parser.add_option("", "--max-line-length", dest="maxLineLength", + default=80, type="int", + help="specify maximum line length [default 80]") +parser.add_option("", "--no-check-whitespace-at-eol", action="store_false", + dest="doCheckWhitespaceAtEol", + default = True, help="don't check for whitespace at the end of a line") +parser.add_option("", "--print-filenames-only", action="store_true", + dest="doPrintFilenamesOnly", + default = False, help="only print the filename(s) where errors were found") + +(options, args) = parser.parse_args() + +doCheckForTabs=options.doCheckForTabs +doCheckLineLength=options.doCheckLineLength +maxLineLength=options.maxLineLength +doCheckWhitespaceAtEol=options.doCheckWhitespaceAtEol +doPrintFilenamesOnly=options.doPrintFilenamesOnly; + +def checkTabs(f, lineNum, line): + column = 0; + for c in line: + column = column + 1 + if c == '\t': + if (doPrintFilenamesOnly): + print f.name + else: + print "tab at line=", lineNum, "column=", column, f.name + return True + return False + +def checkLineLength(f, lineNum, line): + if len(line) > maxLineLength + 1: + if (doPrintFilenamesOnly): + print f.name + else: + print line, + print "line too long=", lineNum, "len=", len(line) - 1, f.name + return True + return False + +def checkWhitespaceBeforeEOL(f, lineNum, line): + if len(line) <= 1: + return; + if line[len(line)-2].isspace(): + if (doPrintFilenamesOnly): + print f.name + else: + print "whitespace at end of line=", lineNum, "len=", len(line) - 1, f.name + return True + return False + +for x in args: + f = open(x, 'r') + lineNum = 0; + for line in f: + lineNum = lineNum + 1 + if doCheckLineLength: + errorFound = checkLineLength(f, lineNum, line) + if errorFound and doPrintFilenamesOnly: + break; + if doCheckForTabs: + errorFound = checkTabs(f, lineNum, line) + if errorFound and doPrintFilenamesOnly: + break; + if doCheckWhitespaceAtEol: + errorFound = checkWhitespaceBeforeEOL(f, lineNum, line) + if errorFound and doPrintFilenamesOnly: + break; +