llvm.org GIT mirror llvm / 653222f
Added some basic lint tools for C++ and generic lint tool applicable to all types of files (TableGen, LLVM assembly, HTML files, etc.) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61592 91177308-0d34-0410-b5e6-96231b3b80d8 Misha Brukman 11 years ago
4 changed file(s) with 168 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/python
1 #
2 # Common lint functions applicable to multiple types of files.
3
4 import re
5
6 def VerifyLineLength(filename, lines, max_length):
7 """Checkes to make sure the file has no lines with lines exceeding the length
8 limit.
9
10 Args:
11 filename: the file under consideration as string
12 lines: contents of the file as string array
13 max_length: maximum acceptable line length as number
14 """
15 line_num = 1
16 for line in lines:
17 length = len(line.rstrip()) # strip off EOL char(s)
18 if length > max_length:
19 print '%s:%d:Line exceeds %d chars (%d)' % (filename, line_num,
20 max_length, length)
21 line_num += 1
22
23
24 def VerifyTrailingWhitespace(filename, lines):
25 """Checkes to make sure the file has no lines with trailing whitespace.
26
27 Args:
28 filename: the file under consideration as string
29 lines: contents of the file as string array
30 """
31 trailing_whitespace_re = re.compile(r'\s+$')
32 line_num = 1
33 for line in lines:
34 if trailing_whitespace_re.match(line):
35 print '%s:%d:Trailing whitespace' % (filename, line_num)
36 line_num += 1
37
38
39 class BaseLint:
40 def RunOnFile(filename, lines):
41 raise Exception('RunOnFile() unimplemented')
42
43
44 def RunLintOverAllFiles(lint, filenames):
45 """Runs linter over the contents of all files.
46
47 Args:
48 lint: subclass of BaseLint, implementing RunOnFile()
49 filenames: list of all files whose contents will be linted
50 """
51 for filename in filenames:
52 file = open(filename, 'r')
53 if not file:
54 print 'Cound not open %s' % filename
55 continue
56 lines = file.readlines()
57 lint.RunOnFile(filename, lines)
0 #!/usr/bin/python
1 #
2 # Checks C++ files to make sure they conform to LLVM standards, as specified in
3 # http://llvm.org/docs/CodingStandards.html .
4 #
5 # TODO: add unittests for the verifier functions:
6 # http://docs.python.org/library/unittest.html .
7
8 import common_lint
9 import re
10 import sys
11
12 def VerifyIncludes(filename, lines):
13 """Makes sure the #includes are in proper order and no disallows files are
14 #included.
15
16 Args:
17 filename: the file under consideration as string
18 lines: contents of the file as string array
19 """
20 include_gtest_re = re.compile(r'^#include "gtest/(.*)"')
21 include_llvm_re = re.compile(r'^#include "llvm/(.*)"')
22 include_support_re = re.compile(r'^#include "(Support/.*)"')
23 include_config_re = re.compile(r'^#include "(Config/.*)"')
24 include_system_re = re.compile(r'^#include <(.*)>')
25
26 DISALLOWED_SYSTEM_HEADERS = ['iostream']
27
28 line_num = 1
29 prev_config_header = None
30 prev_system_header = None
31 for line in lines:
32 # TODO: implement private headers
33 # TODO: implement gtest headers
34 # TODO: implement top-level llvm/* headers
35 # TODO: implement llvm/Support/* headers
36
37 # Process Config/* headers
38 config_header = include_config_re.match(line)
39 if config_header:
40 curr_config_header = config_header.group(1)
41 if prev_config_header:
42 if prev_config_header > curr_config_header:
43 print '%s:%d:Config headers not in order: "%s" before "%s" ' % (
44 filename, line_num, prev_config_header, curr_config_header)
45
46 # Process system headers
47 system_header = include_system_re.match(line)
48 if system_header:
49 curr_system_header = system_header.group(1)
50
51 # Is it blacklisted?
52 if curr_system_header in DISALLOWED_SYSTEM_HEADERS:
53 print '%s:%d:Disallowed system header: <%s>' % (
54 filename, line_num, curr_system_header)
55 elif prev_system_header:
56 # Make sure system headers are alphabetized amongst themselves
57 if prev_system_header > curr_system_header:
58 print '%s:%d:System headers not in order: <%s> before <%s>' % (
59 filename, line_num, prev_system_header, curr_system_header)
60
61 prev_system_header = curr_system_header
62
63 line_num += 1
64
65
66 class CppLint(common_lint.BaseLint):
67 def RunOnFile(self, filename, lines):
68 VerifyIncludes(filename, lines)
69 common_lint.VerifyLineLength(filename, lines)
70 common_lint.VerifyTrailingWhitespace(filename, lines)
71
72
73 def CppLintMain(filenames):
74 common_lint.RunLintOverAllFiles(CppLint(), filenames)
75 return 0
76
77
78 if __name__ == '__main__':
79 sys.exit(CppLintMain(sys.argv[1:]))
0 #!/usr/bin/python
1 #
2 # Checks files to make sure they conform to LLVM standards which can be applied
3 # to any programming language: at present, line length and trailing whitespace.
4
5 import common_lint
6 import sys
7
8 class GenericCodeLint(common_lint.BaseLint):
9 MAX_LINE_LENGTH = 80
10
11 def RunOnFile(self, filename, lines):
12 common_lint.VerifyLineLength(filename, lines,
13 GenericCodeLint.MAX_LINE_LENGTH)
14 common_lint.VerifyTrailingWhitespace(filename, lines)
15
16
17 def GenericCodeLintMain(filenames):
18 common_lint.RunLintOverAllFiles(GenericCodeLint(), filenames)
19 return 0
20
21
22 if __name__ == '__main__':
23 sys.exit(GenericCodeLintMain(sys.argv[1:]))
0 #!/bin/sh
1 # Deletes trailing whitespace in-place in the passed-in files.
2 # Sample syntax:
3 # $0 *.cpp
4
5 perl -pi -e "s/\s+\$//" $*