aboutsummaryrefslogtreecommitdiffstats
path: root/utils/lint
diff options
context:
space:
mode:
authorShih-wei Liao <sliao@google.com>2010-02-10 11:10:31 -0800
committerShih-wei Liao <sliao@google.com>2010-02-10 11:10:31 -0800
commite264f62ca09a8f65c87a46d562a4d0f9ec5d457e (patch)
tree59e3d57ef656cef79afa708ae0a3daf25cd91fcf /utils/lint
downloadexternal_llvm-e264f62ca09a8f65c87a46d562a4d0f9ec5d457e.zip
external_llvm-e264f62ca09a8f65c87a46d562a4d0f9ec5d457e.tar.gz
external_llvm-e264f62ca09a8f65c87a46d562a4d0f9ec5d457e.tar.bz2
Check in LLVM r95781.
Diffstat (limited to 'utils/lint')
-rw-r--r--utils/lint/common_lint.py97
-rwxr-xr-xutils/lint/cpp_lint.py94
-rwxr-xr-xutils/lint/generic_lint.py24
-rwxr-xr-xutils/lint/remove_trailing_whitespace.sh6
4 files changed, 221 insertions, 0 deletions
diff --git a/utils/lint/common_lint.py b/utils/lint/common_lint.py
new file mode 100644
index 0000000..e982680
--- /dev/null
+++ b/utils/lint/common_lint.py
@@ -0,0 +1,97 @@
+#!/usr/bin/python
+#
+# Common lint functions applicable to multiple types of files.
+
+import re
+
+def VerifyLineLength(filename, lines, max_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:
+ 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):
+ """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')):
+ lint.append((filename, line_num, 'Trailing whitespace'))
+ line_num += 1
+ return lint
+
+
+class BaseLint:
+ def RunOnFile(filename, lines):
+ raise Exception('RunOnFile() unimplemented')
+
+
+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.extend(linter.RunOnFile(filename, lines))
+
+ return lint
diff --git a/utils/lint/cpp_lint.py b/utils/lint/cpp_lint.py
new file mode 100755
index 0000000..07fad58
--- /dev/null
+++ b/utils/lint/cpp_lint.py
@@ -0,0 +1,94 @@
+#!/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
+ """
+ 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/.*)"')
+ 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:
+ 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)
+ if system_header:
+ curr_system_header = system_header.group(1)
+
+ # Is it blacklisted?
+ if curr_system_header in DISALLOWED_SYSTEM_HEADERS:
+ 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:
+ 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):
+ 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):
+ all_lint = common_lint.RunLintOverAllFiles(CppLint(), filenames)
+ for lint in all_lint:
+ print '%s:%d:%s' % (lint[0], lint[1], lint[2])
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(CppLintMain(sys.argv[1:]))
diff --git a/utils/lint/generic_lint.py b/utils/lint/generic_lint.py
new file mode 100755
index 0000000..c8f4835
--- /dev/null
+++ b/utils/lint/generic_lint.py
@@ -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:]))
diff --git a/utils/lint/remove_trailing_whitespace.sh b/utils/lint/remove_trailing_whitespace.sh
new file mode 100755
index 0000000..6e0c9be
--- /dev/null
+++ b/utils/lint/remove_trailing_whitespace.sh
@@ -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+$/\n/' $*