[llvm-commits] [llvm] r61592 - in /llvm/trunk/utils/lint: ./ common_lint.py cpp_lint.py generic_lint.py remove_trailing_whitespace.sh

Misha Brukman brukman+llvm at gmail.com
Fri Jan 2 13:15:31 PST 2009


Author: brukman
Date: Fri Jan  2 15:15:30 2009
New Revision: 61592

URL: http://llvm.org/viewvc/llvm-project?rev=61592&view=rev
Log:
Added some basic lint tools for C++ and generic lint tool applicable to all
types of files (TableGen, LLVM assembly, HTML files, etc.)

Added:
    llvm/trunk/utils/lint/
    llvm/trunk/utils/lint/common_lint.py
    llvm/trunk/utils/lint/cpp_lint.py   (with props)
    llvm/trunk/utils/lint/generic_lint.py   (with props)
    llvm/trunk/utils/lint/remove_trailing_whitespace.sh   (with props)

Added: llvm/trunk/utils/lint/common_lint.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lint/common_lint.py?rev=61592&view=auto

==============================================================================
--- llvm/trunk/utils/lint/common_lint.py (added)
+++ llvm/trunk/utils/lint/common_lint.py Fri Jan  2 15:15:30 2009
@@ -0,0 +1,58 @@
+#!/usr/bin/python
+#
+# Common lint functions applicable to multiple types of files.
+
+import re
+
+def VerifyLineLength(filename, lines, max_length):
+  """Checkes 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
+  """
+  line_num = 1
+  for line in lines:
+    length = len(line.rstrip())  # strip off EOL char(s)
+    if length > max_length:
+      print '%s:%d:Line exceeds %d chars (%d)' % (filename, line_num,
+                                                  max_length, length)
+    line_num += 1
+
+
+def VerifyTrailingWhitespace(filename, lines):
+  """Checkes 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
+  """
+  trailing_whitespace_re = re.compile(r'\s+$')
+  line_num = 1
+  for line in lines:
+    if trailing_whitespace_re.match(line):
+      print '%s:%d:Trailing whitespace' % (filename, line_num)
+    line_num += 1
+
+
+class BaseLint:
+  def RunOnFile(filename, lines):
+    raise Exception('RunOnFile() unimplemented')
+
+
+def RunLintOverAllFiles(lint, 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
+  """
+  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)

Added: llvm/trunk/utils/lint/cpp_lint.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lint/cpp_lint.py?rev=61592&view=auto

==============================================================================
--- llvm/trunk/utils/lint/cpp_lint.py (added)
+++ llvm/trunk/utils/lint/cpp_lint.py Fri Jan  2 15:15:30 2009
@@ -0,0 +1,80 @@
+#!/usr/bin/python
+#
+# Checks C++ files to make sure they conform to LLVM standards, as specified in
+# http://llvm.org/docs/CodingStandards.html .
+#
+# TODO: add unittests for the verifier functions:
+# http://docs.python.org/library/unittest.html .
+
+import common_lint
+import re
+import sys
+
+def VerifyIncludes(filename, lines):
+  """Makes sure the #includes are in proper order and no disallows files are
+  #included.
+
+  Args:
+    filename: the file under consideration as string
+    lines: contents of the file as string array
+  """
+  include_gtest_re = re.compile(r'^#include "gtest/(.*)"')
+  include_llvm_re = re.compile(r'^#include "llvm/(.*)"')
+  include_support_re = re.compile(r'^#include "(Support/.*)"')
+  include_config_re = re.compile(r'^#include "(Config/.*)"')
+  include_system_re = re.compile(r'^#include <(.*)>')
+
+  DISALLOWED_SYSTEM_HEADERS = ['iostream']
+
+  line_num = 1
+  prev_config_header = None
+  prev_system_header = None
+  for line in lines:
+    # TODO: implement private headers
+    # TODO: implement gtest headers
+    # TODO: implement top-level llvm/* headers
+    # TODO: implement llvm/Support/* headers
+
+    # Process Config/* headers
+    config_header = include_config_re.match(line)
+    if config_header:
+      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)
+
+    # Process system headers
+    system_header = include_system_re.match(line)
+    if system_header:
+      curr_system_header = system_header.group(1)
+
+      # Is it blacklisted?
+      if curr_system_header in DISALLOWED_SYSTEM_HEADERS:
+        print '%s:%d:Disallowed system header: <%s>' % (
+            filename, line_num, 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)
+
+      prev_system_header = curr_system_header
+
+    line_num += 1
+
+
+class CppLint(common_lint.BaseLint):
+  def RunOnFile(self, filename, lines):
+    VerifyIncludes(filename, lines)
+    common_lint.VerifyLineLength(filename, lines)
+    common_lint.VerifyTrailingWhitespace(filename, lines)
+
+
+def CppLintMain(filenames):
+  common_lint.RunLintOverAllFiles(CppLint(), filenames)
+  return 0
+
+
+if __name__ == '__main__':
+  sys.exit(CppLintMain(sys.argv[1:]))

Propchange: llvm/trunk/utils/lint/cpp_lint.py

------------------------------------------------------------------------------
    svn:executable = *

Added: llvm/trunk/utils/lint/generic_lint.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lint/generic_lint.py?rev=61592&view=auto

==============================================================================
--- llvm/trunk/utils/lint/generic_lint.py (added)
+++ llvm/trunk/utils/lint/generic_lint.py Fri Jan  2 15:15:30 2009
@@ -0,0 +1,24 @@
+#!/usr/bin/python
+#
+# Checks files to make sure they conform to LLVM standards which can be applied
+# to any programming language: at present, line length and trailing whitespace.
+
+import common_lint
+import sys
+
+class GenericCodeLint(common_lint.BaseLint):
+  MAX_LINE_LENGTH = 80
+
+  def RunOnFile(self, filename, lines):
+    common_lint.VerifyLineLength(filename, lines,
+                                 GenericCodeLint.MAX_LINE_LENGTH)
+    common_lint.VerifyTrailingWhitespace(filename, lines)
+
+
+def GenericCodeLintMain(filenames):
+  common_lint.RunLintOverAllFiles(GenericCodeLint(), filenames)
+  return 0
+
+
+if __name__ == '__main__':
+  sys.exit(GenericCodeLintMain(sys.argv[1:]))

Propchange: llvm/trunk/utils/lint/generic_lint.py

------------------------------------------------------------------------------
    svn:executable = *

Added: llvm/trunk/utils/lint/remove_trailing_whitespace.sh
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lint/remove_trailing_whitespace.sh?rev=61592&view=auto

==============================================================================
--- llvm/trunk/utils/lint/remove_trailing_whitespace.sh (added)
+++ llvm/trunk/utils/lint/remove_trailing_whitespace.sh Fri Jan  2 15:15:30 2009
@@ -0,0 +1,6 @@
+#!/bin/sh
+# Deletes trailing whitespace in-place in the passed-in files.
+# Sample syntax:
+#   $0 *.cpp
+
+perl -pi -e "s/\s+\$//" $*

Propchange: llvm/trunk/utils/lint/remove_trailing_whitespace.sh

------------------------------------------------------------------------------
    svn:executable = *





More information about the llvm-commits mailing list