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 .
9 from __future__
import print_function
14 def VerifyIncludes(filename
, lines
):
15 """Makes sure the #includes are in proper order and no disallows files are
19 filename: the file under consideration as string
20 lines: contents of the file as string array
24 include_gtest_re
= re
.compile(r
'^#include "gtest/(.*)"')
25 include_llvm_re
= re
.compile(r
'^#include "llvm/(.*)"')
26 include_support_re
= re
.compile(r
'^#include "(Support/.*)"')
27 include_config_re
= re
.compile(r
'^#include "(Config/.*)"')
28 include_system_re
= re
.compile(r
'^#include <(.*)>')
30 DISALLOWED_SYSTEM_HEADERS
= ['iostream']
33 prev_config_header
= None
34 prev_system_header
= None
36 # TODO: implement private headers
37 # TODO: implement gtest headers
38 # TODO: implement top-level llvm/* headers
39 # TODO: implement llvm/Support/* headers
41 # Process Config/* headers
42 config_header
= include_config_re
.match(line
)
44 curr_config_header
= config_header
.group(1)
45 if prev_config_header
:
46 if prev_config_header
> curr_config_header
:
47 lint
.append((filename
, line_num
,
48 'Config headers not in order: "%s" before "%s"' % (
49 prev_config_header
, curr_config_header
)))
51 # Process system headers
52 system_header
= include_system_re
.match(line
)
54 curr_system_header
= system_header
.group(1)
57 if curr_system_header
in DISALLOWED_SYSTEM_HEADERS
:
58 lint
.append((filename
, line_num
,
59 'Disallowed system header: <%s>' % curr_system_header
))
60 elif prev_system_header
:
61 # Make sure system headers are alphabetized amongst themselves
62 if prev_system_header
> curr_system_header
:
63 lint
.append((filename
, line_num
,
64 'System headers not in order: <%s> before <%s>' % (
65 prev_system_header
, curr_system_header
)))
67 prev_system_header
= curr_system_header
74 class CppLint(common_lint
.BaseLint
):
77 def RunOnFile(self
, filename
, lines
):
79 lint
.extend(VerifyIncludes(filename
, lines
))
80 lint
.extend(common_lint
.VerifyLineLength(filename
, lines
,
81 CppLint
.MAX_LINE_LENGTH
))
82 lint
.extend(common_lint
.VerifyTabs(filename
, lines
))
83 lint
.extend(common_lint
.VerifyTrailingWhitespace(filename
, lines
))
87 def CppLintMain(filenames
):
88 all_lint
= common_lint
.RunLintOverAllFiles(CppLint(), filenames
)
90 print('%s:%d:%s' % (lint
[0], lint
[1], lint
[2]))
94 if __name__
== '__main__':
95 sys
.exit(CppLintMain(sys
.argv
[1:]))