util: added line length and boolean comparison style checkers
authorSteve Reinhardt <steve.reinhardt@amd.com>
Sun, 7 Feb 2016 01:21:20 +0000 (17:21 -0800)
committerSteve Reinhardt <steve.reinhardt@amd.com>
Sun, 7 Feb 2016 01:21:20 +0000 (17:21 -0800)
Added checkers for line length and boolean comparisons
(== true/== false) to the style script.

util/style.py

index ee3c0bf94f5c9e67c72a1293ec20cf436e1014c5..7ee58dfa68d96b3300d3a8f2ad1bd0e9ab7f1e09 100644 (file)
@@ -404,12 +404,6 @@ class SortedIncludes(Verifier):
             f.write('\n')
         f.close()
 
-# list of all verifier classes
-all_verifiers = [
-    Whitespace,
-    ControlSpace,
-    SortedIncludes
-]
 
 def linelen(line):
     tabs = line.count('\t')
@@ -425,6 +419,48 @@ def linelen(line):
 
     return count
 
+class LineLength(Verifier):
+    languages = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons'))
+    test_name = 'line length'
+    opt_name = 'length'
+
+    def check_line(self, line):
+        return linelen(line) <= 78
+
+    def fix(self, filename, regions=all_regions):
+        self.write("Warning: cannot automatically fix overly long lines.\n")
+
+
+class BoolCompare(Verifier):
+    languages = set(('C', 'C++', 'python'))
+    test_name = 'boolean comparison'
+    opt_name = 'boolcomp'
+
+    regex = re.compile(r'\s*==\s*([Tt]rue|[Ff]alse)\b')
+
+    def check_line(self, line):
+        return self.regex.search(line) == None
+
+    def fix_line(self, line):
+        match = self.regex.search(line)
+        if match:
+            if match.group(1) in ('true', 'True'):
+                line = self.regex.sub('', line)
+            else:
+                self.write("Warning: cannot automatically fix "
+                           "comparisons with false/False.\n")
+        return line
+
+
+# list of all verifier classes
+all_verifiers = [
+    Whitespace,
+    ControlSpace,
+    LineLength,
+    BoolCompare,
+    SortedIncludes
+]
+
 class ValidationStats(object):
     def __init__(self):
         self.toolong = 0