llvm.org GIT mirror llvm / 9259905
* 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@65202 91177308-0d34-0410-b5e6-96231b3b80d8 Misha Brukman 10 years ago
2 changed file(s) with 70 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
44 import re
55
66 def VerifyLineLength(filename, lines, max_length):
7 """Checkes to make sure the file has no lines with lines exceeding the length
7 """Checks to make sure the file has no lines with lines exceeding the length
88 limit.
99
1010 Args:
1111 filename: the file under consideration as string
1212 lines: contents of the file as string array
1313 max_length: maximum acceptable line length as number
14
15 Returns:
16 A list of tuples with format [(filename, line number, msg), ...] with any
17 violations found.
1418 """
19 lint = []
1520 line_num = 1
1621 for line in lines:
1722 length = len(line.rstrip('\n'))
1823 if length > max_length:
19 print '%s:%d:Line exceeds %d chars (%d)' % (filename, line_num,
20 max_length, length)
24 lint.append((filename, line_num,
25 'Line exceeds %d chars (%d)' % (max_length, length)))
2126 line_num += 1
27 return lint
2228
23
24 def VerifyTrailingWhitespace(filename, lines):
25 """Checkes to make sure the file has no lines with trailing whitespace.
29 def VerifyTabs(filename, lines):
30 """Checks to make sure the file has no tab characters.
2631
2732 Args:
2833 filename: the file under consideration as string
2934 lines: contents of the file as string array
35
36 Returns:
37 A list of tuples with format [(line_number, msg), ...] with any violations
38 found.
3039 """
40 lint = []
41 tab_re = re.compile(r'\t')
42 line_num = 1
43 for line in lines:
44 if tab_re.match(line.rstrip('\n')):
45 lint.append((filename, line_num, 'Tab found instead of whitespace'))
46 line_num += 1
47 return lint
48
49
50 def VerifyTrailingWhitespace(filename, lines):
51 """Checks to make sure the file has no lines with trailing whitespace.
52
53 Args:
54 filename: the file under consideration as string
55 lines: contents of the file as string array
56
57 Returns:
58 A list of tuples with format [(filename, line number, msg), ...] with any
59 violations found.
60 """
61 lint = []
3162 trailing_whitespace_re = re.compile(r'\s+$')
3263 line_num = 1
3364 for line in lines:
3465 if trailing_whitespace_re.match(line.rstrip('\n')):
35 print '%s:%d:Trailing whitespace' % (filename, line_num)
66 lint.append((filename, line_num, 'Trailing whitespace'))
3667 line_num += 1
68 return lint
3769
3870
3971 class BaseLint:
4173 raise Exception('RunOnFile() unimplemented')
4274
4375
44 def RunLintOverAllFiles(lint, filenames):
76 def RunLintOverAllFiles(linter, filenames):
4577 """Runs linter over the contents of all files.
4678
4779 Args:
4880 lint: subclass of BaseLint, implementing RunOnFile()
4981 filenames: list of all files whose contents will be linted
82
83 Returns:
84 A list of tuples with format [(filename, line number, msg), ...] with any
85 violations found.
5086 """
87 lint = []
5188 for filename in filenames:
5289 file = open(filename, 'r')
5390 if not file:
5491 print 'Cound not open %s' % filename
5592 continue
5693 lines = file.readlines()
57 lint.RunOnFile(filename, lines)
94 lint.extend(linter.RunOnFile(filename, lines))
95
96 return lint
1717 filename: the file under consideration as string
1818 lines: contents of the file as string array
1919 """
20 lint = []
21
2022 include_gtest_re = re.compile(r'^#include "gtest/(.*)"')
2123 include_llvm_re = re.compile(r'^#include "llvm/(.*)"')
2224 include_support_re = re.compile(r'^#include "(Support/.*)"')
4042 curr_config_header = config_header.group(1)
4143 if prev_config_header:
4244 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 lint.append((filename, line_num,
46 'Config headers not in order: "%s" before "%s"' % (
47 prev_config_header, curr_config_header)))
4548
4649 # Process system headers
4750 system_header = include_system_re.match(line)
5053
5154 # Is it blacklisted?
5255 if curr_system_header in DISALLOWED_SYSTEM_HEADERS:
53 print '%s:%d:Disallowed system header: <%s>' % (
54 filename, line_num, curr_system_header)
56 lint.append((filename, line_num,
57 'Disallowed system header: <%s>' % curr_system_header))
5558 elif prev_system_header:
5659 # Make sure system headers are alphabetized amongst themselves
5760 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)
61 lint.append((filename, line_num,
62 'System headers not in order: <%s> before <%s>' % (
63 prev_system_header, curr_system_header)))
6064
6165 prev_system_header = curr_system_header
6266
6367 line_num += 1
68
69 return lint
6470
6571
6672 class CppLint(common_lint.BaseLint):
6773 MAX_LINE_LENGTH = 80
6874
6975 def RunOnFile(self, filename, lines):
70 VerifyIncludes(filename, lines)
71 common_lint.VerifyLineLength(filename, lines, CppLint.MAX_LINE_LENGTH)
72 common_lint.VerifyTrailingWhitespace(filename, lines)
76 lint = []
77 lint.extend(VerifyIncludes(filename, lines))
78 lint.extend(common_lint.VerifyLineLength(filename, lines,
79 CppLint.MAX_LINE_LENGTH))
80 lint.extend(common_lint.VerifyTabs(filename, lines))
81 lint.extend(common_lint.VerifyTrailingWhitespace(filename, lines))
82 return lint
7383
7484
7585 def CppLintMain(filenames):
76 common_lint.RunLintOverAllFiles(CppLint(), filenames)
86 all_lint = common_lint.RunLintOverAllFiles(CppLint(), filenames)
87 for lint in all_lint:
88 print '%s:%d:%s' % (lint[0], lint[1], lint[2])
7789 return 0
7890
7991