[llvm-commits] [llvm] r65202 - in /llvm/trunk/utils/lint: common_lint.py cpp_lint.py

Misha Brukman brukman+llvm at gmail.com
Fri Feb 20 15:44:55 PST 2009


Author: brukman
Date: Fri Feb 20 17:44:54 2009
New Revision: 65202

URL: http://llvm.org/viewvc/llvm-project?rev=65202&view=rev
Log:
* Fixed spelling
* Linters now return their information instead of printing it, to
  enable easier unittesting
* Added support for finding tabs in files, added to C++ linter

Modified:
    llvm/trunk/utils/lint/common_lint.py
    llvm/trunk/utils/lint/cpp_lint.py

Modified: llvm/trunk/utils/lint/common_lint.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lint/common_lint.py?rev=65202&r1=65201&r2=65202&view=diff

==============================================================================
--- llvm/trunk/utils/lint/common_lint.py (original)
+++ llvm/trunk/utils/lint/common_lint.py Fri Feb 20 17:44:54 2009
@@ -5,36 +5,68 @@
 import re
 
 def VerifyLineLength(filename, lines, max_length):
-  """Checkes to make sure the file has no lines with lines exceeding the length
+  """Checks to make sure the file has no lines with lines exceeding the length
   limit.
 
   Args:
     filename: the file under consideration as string
     lines: contents of the file as string array
     max_length: maximum acceptable line length as number
+
+  Returns:
+    A list of tuples with format [(filename, line number, msg), ...] with any
+    violations found.
   """
+  lint = []
   line_num = 1
   for line in lines:
     length = len(line.rstrip('\n'))
     if length > max_length:
-      print '%s:%d:Line exceeds %d chars (%d)' % (filename, line_num,
-                                                  max_length, length)
+      lint.append((filename, line_num,
+                   'Line exceeds %d chars (%d)' % (max_length, length)))
+    line_num += 1
+  return lint
+
+def VerifyTabs(filename, lines):
+  """Checks to make sure the file has no tab characters.
+
+  Args:
+    filename: the file under consideration as string
+    lines: contents of the file as string array
+
+  Returns:
+    A list of tuples with format [(line_number, msg), ...] with any violations
+    found.
+  """
+  lint = []
+  tab_re = re.compile(r'\t')
+  line_num = 1
+  for line in lines:
+    if tab_re.match(line.rstrip('\n')):
+      lint.append((filename, line_num, 'Tab found instead of whitespace'))
     line_num += 1
+  return lint
 
 
 def VerifyTrailingWhitespace(filename, lines):
-  """Checkes to make sure the file has no lines with trailing whitespace.
+  """Checks to make sure the file has no lines with trailing whitespace.
 
   Args:
     filename: the file under consideration as string
     lines: contents of the file as string array
+
+  Returns:
+    A list of tuples with format [(filename, line number, msg), ...] with any
+    violations found.
   """
+  lint = []
   trailing_whitespace_re = re.compile(r'\s+$')
   line_num = 1
   for line in lines:
     if trailing_whitespace_re.match(line.rstrip('\n')):
-      print '%s:%d:Trailing whitespace' % (filename, line_num)
+      lint.append((filename, line_num, 'Trailing whitespace'))
     line_num += 1
+  return lint
 
 
 class BaseLint:
@@ -42,17 +74,24 @@
     raise Exception('RunOnFile() unimplemented')
 
 
-def RunLintOverAllFiles(lint, filenames):
+def RunLintOverAllFiles(linter, filenames):
   """Runs linter over the contents of all files.
 
   Args:
     lint: subclass of BaseLint, implementing RunOnFile()
     filenames: list of all files whose contents will be linted
+
+  Returns:
+    A list of tuples with format [(filename, line number, msg), ...] with any
+    violations found.
   """
+  lint = []
   for filename in filenames:
     file = open(filename, 'r')
     if not file:
       print 'Cound not open %s' % filename
       continue
     lines = file.readlines()
-    lint.RunOnFile(filename, lines)
+    lint.extend(linter.RunOnFile(filename, lines))
+
+  return lint

Modified: llvm/trunk/utils/lint/cpp_lint.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lint/cpp_lint.py?rev=65202&r1=65201&r2=65202&view=diff

==============================================================================
--- llvm/trunk/utils/lint/cpp_lint.py (original)
+++ llvm/trunk/utils/lint/cpp_lint.py Fri Feb 20 17:44:54 2009
@@ -18,6 +18,8 @@
     filename: the file under consideration as string
     lines: contents of the file as string array
   """
+  lint = []
+
   include_gtest_re = re.compile(r'^#include "gtest/(.*)"')
   include_llvm_re = re.compile(r'^#include "llvm/(.*)"')
   include_support_re = re.compile(r'^#include "(Support/.*)"')
@@ -41,8 +43,9 @@
       curr_config_header = config_header.group(1)
       if prev_config_header:
         if prev_config_header > curr_config_header:
-          print '%s:%d:Config headers not in order: "%s" before "%s" ' % (
-              filename, line_num, prev_config_header, curr_config_header)
+          lint.append((filename, line_num,
+                       'Config headers not in order: "%s" before "%s"' % (
+                         prev_config_header, curr_config_header)))
 
     # Process system headers
     system_header = include_system_re.match(line)
@@ -51,30 +54,39 @@
 
       # Is it blacklisted?
       if curr_system_header in DISALLOWED_SYSTEM_HEADERS:
-        print '%s:%d:Disallowed system header: <%s>' % (
-            filename, line_num, curr_system_header)
+        lint.append((filename, line_num,
+                     'Disallowed system header: <%s>' % curr_system_header))
       elif prev_system_header:
         # Make sure system headers are alphabetized amongst themselves
         if prev_system_header > curr_system_header:
-          print '%s:%d:System headers not in order: <%s> before <%s>' % (
-              filename, line_num, prev_system_header, curr_system_header)
+          lint.append((filename, line_num,
+                       'System headers not in order: <%s> before <%s>' % (
+                         prev_system_header, curr_system_header)))
 
       prev_system_header = curr_system_header
 
     line_num += 1
 
+  return lint
+
 
 class CppLint(common_lint.BaseLint):
   MAX_LINE_LENGTH = 80
 
   def RunOnFile(self, filename, lines):
-    VerifyIncludes(filename, lines)
-    common_lint.VerifyLineLength(filename, lines, CppLint.MAX_LINE_LENGTH)
-    common_lint.VerifyTrailingWhitespace(filename, lines)
+    lint = []
+    lint.extend(VerifyIncludes(filename, lines))
+    lint.extend(common_lint.VerifyLineLength(filename, lines,
+                                             CppLint.MAX_LINE_LENGTH))
+    lint.extend(common_lint.VerifyTabs(filename, lines))
+    lint.extend(common_lint.VerifyTrailingWhitespace(filename, lines))
+    return lint
 
 
 def CppLintMain(filenames):
-  common_lint.RunLintOverAllFiles(CppLint(), filenames)
+  all_lint = common_lint.RunLintOverAllFiles(CppLint(), filenames)
+  for lint in all_lint:
+    print '%s:%d:%s' % (lint[0], lint[1], lint[2])
   return 0
 
 





More information about the llvm-commits mailing list