3
# Checks C++ files to make sure they conform to LLVM standards, as specified in
4
# http://llvm.org/docs/CodingStandards.html .
6
# TODO: add unittests for the verifier functions:
7
# http://docs.python.org/library/unittest.html .
13
def VerifyIncludes(filename, lines):
14
"""Makes sure the #includes are in proper order and no disallows files are
18
filename: the file under consideration as string
19
lines: contents of the file as string array
23
include_gtest_re = re.compile(r'^#include "gtest/(.*)"')
24
include_llvm_re = re.compile(r'^#include "llvm/(.*)"')
25
include_support_re = re.compile(r'^#include "(Support/.*)"')
26
include_config_re = re.compile(r'^#include "(Config/.*)"')
27
include_system_re = re.compile(r'^#include <(.*)>')
29
DISALLOWED_SYSTEM_HEADERS = ['iostream']
32
prev_config_header = None
33
prev_system_header = None
35
# TODO: implement private headers
36
# TODO: implement gtest headers
37
# TODO: implement top-level llvm/* headers
38
# TODO: implement llvm/Support/* headers
40
# Process Config/* headers
41
config_header = include_config_re.match(line)
43
curr_config_header = config_header.group(1)
44
if prev_config_header:
45
if prev_config_header > curr_config_header:
46
lint.append((filename, line_num,
47
'Config headers not in order: "%s" before "%s"' % (
48
prev_config_header, curr_config_header)))
50
# Process system headers
51
system_header = include_system_re.match(line)
53
curr_system_header = system_header.group(1)
56
if curr_system_header in DISALLOWED_SYSTEM_HEADERS:
57
lint.append((filename, line_num,
58
'Disallowed system header: <%s>' % curr_system_header))
59
elif prev_system_header:
60
# Make sure system headers are alphabetized amongst themselves
61
if prev_system_header > curr_system_header:
62
lint.append((filename, line_num,
63
'System headers not in order: <%s> before <%s>' % (
64
prev_system_header, curr_system_header)))
66
prev_system_header = curr_system_header
73
class CppLint(common_lint.BaseLint):
76
def RunOnFile(self, filename, lines):
78
lint.extend(VerifyIncludes(filename, lines))
79
lint.extend(common_lint.VerifyLineLength(filename, lines,
80
CppLint.MAX_LINE_LENGTH))
81
lint.extend(common_lint.VerifyTabs(filename, lines))
82
lint.extend(common_lint.VerifyTrailingWhitespace(filename, lines))
86
def CppLintMain(filenames):
87
all_lint = common_lint.RunLintOverAllFiles(CppLint(), filenames)
89
print '%s:%d:%s' % (lint[0], lint[1], lint[2])
93
if __name__ == '__main__':
94
sys.exit(CppLintMain(sys.argv[1:]))