[llvm-commits] style check - update to support globs and recursive traversal
Reed Kotler
rkotler at mips.com
Fri Jun 1 23:24:32 PDT 2012
When I run this now on the Target subtree alone, it finds 22,000 errors
from just simple things like lines too long, tabs and whitespace at the
end of lines.
More features to come.
Usage: style_check.py arg1 arg2 ... argn [options]
Options:
-h, --help show this help message and exit
--no-check-for-tabs don't check for tabs
--no-check-line-length
don't check line length
--max-line-length=MAXLINELENGTH
specify maximum line length [default 80]
--no-check-whitespace-at-eol
don't check for whitespace at the end of a line
--print-filenames-only
only print the filename(s) where errors were found
--recursive rescursively walk subdirectories
-------------- next part --------------
diff --git a/utils/style_check.py b/utils/style_check.py
new file mode 100644
index 0000000..915becd
--- /dev/null
+++ b/utils/style_check.py
@@ -0,0 +1,107 @@
+#===-- 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
+import glob
+import os
+
+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")
+parser.add_option("", "--recursive", action="store_true",
+ dest="doRecursive",
+ default = False, help="rescursively walk subdirectories")
+(options, args) = parser.parse_args()
+
+doCheckForTabs=options.doCheckForTabs
+doCheckLineLength=options.doCheckLineLength
+maxLineLength=options.maxLineLength
+doCheckWhitespaceAtEol=options.doCheckWhitespaceAtEol
+doPrintFilenamesOnly=options.doPrintFilenamesOnly
+doRecursive=options.doRecursive
+
+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
+
+def doOneFile(fname):
+ if os.path.isdir(fname):
+ if doRecursive:
+ # print "in recursive ", fname
+ for root, dirs, files in os.walk(fname):
+ # print "root ", root, "dirs ", dirs, "files ", files
+ for d in dirs:
+ doOneFile(root + "/" + d)
+ for f in files:
+ doOneFile(root + "/" + f)
+ else:
+ f = open(fname, '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;
+
+for x in args:
+ g = glob.glob(x)
+ for y in g:
+ doOneFile(y)
+
More information about the llvm-commits
mailing list