* Use os.path.walk to visit directories

* Use \w instead of [a-zA-Z0-9] in some regex's
* Add switch to the "space after control statement" rule
* Added pointer/reference style rule


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33921 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Alexandre Deckner
2009-11-06 15:55:44 +00:00
parent df989eb2bb
commit 2ec4a3b13f
+20 -20
View File
@@ -35,32 +35,24 @@ def run(fileSet, rules, outputFileName):
closeHtml(outputFileName) closeHtml(outputFileName)
# make a flat list of files recursively from the arg list def visit(result, dir, names):
def makeFileSet(args):
files = []
extensions = [".cpp", ".h"] extensions = [".cpp", ".h"]
for arg in args: names.remove(".svn")
if os.path.isfile(arg) and os.path.splitext(arg)[1] in extensions: for name in names:
files.append(arg) path = os.path.join(dir, name)
elif os.path.isdir(arg): if os.path.isfile(path) and os.path.splitext(name)[1] in extensions:
for content in os.listdir(arg): print "adding", path
path = os.path.join(arg, content) result.append(path)
if os.path.isfile(path) \
and os.path.splitext(path)[1] in extensions:
files.append(path)
elif os.path.isdir(path) and os.path.basename(path) != ".svn":
files.extend(makeFileSet([path]))
return files
cppRules = {} cppRules = {}
cppRules["Line over 80 char"] = re.compile('[^\n]{81,}') cppRules["Line over 80 char"] = re.compile('[^\n]{81,}')
cppRules["Spaces instead of tabs"] = re.compile(' ') cppRules["Spaces instead of tabs"] = re.compile(' ')
cppRules["Missing space after for/if/select/while"] \ cppRules["Missing space after control statement"] \
= re.compile('(for|if|select|while)\(') = re.compile('(for|if|while|switch)\(')
cppRules["Missing space at comment start"] = re.compile('//[a-zA-Z0-9]') cppRules["Missing space at comment start"] = re.compile('//\w')
cppRules["Missing space after operator"] \ cppRules["Missing space after operator"] \
= re.compile('[a-zA-Z0-9](==|[,=>/+\-*;\|])[a-zA-Z0-9]') = re.compile('\w(==|[,=>/+\-*;\|])\w')
cppRules["Operator at line end"] = re.compile('([*=/+\-\|\&\?]|\&&|\|\|)(?=\n)') cppRules["Operator at line end"] = re.compile('([*=/+\-\|\&\?]|\&&|\|\|)(?=\n)')
cppRules["Missing space"] = re.compile('\){') cppRules["Missing space"] = re.compile('\){')
cppRules["Mixed tabs/spaces"] = re.compile('( \t]|\t )+') cppRules["Mixed tabs/spaces"] = re.compile('( \t]|\t )+')
@@ -70,6 +62,8 @@ cppRules["Lines between functions > 2"] \
cppRules["Lines between functions < 2"] \ cppRules["Lines between functions < 2"] \
= re.compile('(?<=\n})([ \t]*\n){0,2}(?=.)') = re.compile('(?<=\n})([ \t]*\n){0,2}(?=.)')
cppRules["Windows Line Ending"] = re.compile('\r') cppRules["Windows Line Ending"] = re.compile('\r')
cppRules["Bad pointer/reference style"] \
= re.compile('(?<=\w) [*&](?=(\w|[,\)]))')
# TODO: ignore some rules in comments # TODO: ignore some rules in comments
#cppRules["-Comment 1"] = re.compile('[^/]/\*(.|[\r\n])*?\*/') #cppRules["-Comment 1"] = re.compile('[^/]/\*(.|[\r\n])*?\*/')
@@ -77,7 +71,13 @@ cppRules["Windows Line Ending"] = re.compile('\r')
if len(sys.argv) >= 2 and sys.argv[1] != "--help": if len(sys.argv) >= 2 and sys.argv[1] != "--help":
run(makeFileSet(sys.argv[1:]), cppRules, "styleviolations.html") files = []
for arg in sys.argv[1:]:
if os.path.isfile(arg):
files.append(arg)
else:
os.path.walk(arg, visit, files)
run(files, cppRules, "styleviolations.html")
else: else:
print "Usage: python checkstyle.py file.cpp [file2.cpp] [directory]\n" print "Usage: python checkstyle.py file.cpp [file2.cpp] [directory]\n"
print "Checks c++ source files against the Haiku Coding Guidelines." print "Checks c++ source files against the Haiku Coding Guidelines."