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
15 def VerifyIncludes(filename
, lines
):
16 """Makes sure the #includes are in proper order and no disallows files are
20 filename: the file under consideration as string
21 lines: contents of the file as string array
25 include_gtest_re
= re
.compile(r
'^#include "gtest/(.*)"')
26 include_llvm_re
= re
.compile(r
'^#include "llvm/(.*)"')
27 include_support_re
= re
.compile(r
'^#include "(Support/.*)"')
28 include_config_re
= re
.compile(r
'^#include "(Config/.*)"')
29 include_system_re
= re
.compile(r
"^#include <(.*)>")
31 DISALLOWED_SYSTEM_HEADERS
= ["iostream"]
34 prev_config_header
= None
35 prev_system_header
= None
37 # TODO: implement private headers
38 # TODO: implement gtest headers
39 # TODO: implement top-level llvm/* headers
40 # TODO: implement llvm/Support/* headers
42 # Process Config/* headers
43 config_header
= include_config_re
.match(line
)
45 curr_config_header
= config_header
.group(1)
46 if prev_config_header
:
47 if prev_config_header
> curr_config_header
:
52 'Config headers not in order: "%s" before "%s"'
53 % (prev_config_header
, curr_config_header
),
57 # Process system headers
58 system_header
= include_system_re
.match(line
)
60 curr_system_header
= system_header
.group(1)
63 if curr_system_header
in DISALLOWED_SYSTEM_HEADERS
:
68 "Disallowed system header: <%s>" % curr_system_header
,
71 elif prev_system_header
:
72 # Make sure system headers are alphabetized amongst themselves
73 if prev_system_header
> curr_system_header
:
78 "System headers not in order: <%s> before <%s>"
79 % (prev_system_header
, curr_system_header
),
83 prev_system_header
= curr_system_header
90 class CppLint(common_lint
.BaseLint
):
93 def RunOnFile(self
, filename
, lines
):
95 lint
.extend(VerifyIncludes(filename
, lines
))
97 common_lint
.VerifyLineLength(filename
, lines
, CppLint
.MAX_LINE_LENGTH
)
99 lint
.extend(common_lint
.VerifyTabs(filename
, lines
))
100 lint
.extend(common_lint
.VerifyTrailingWhitespace(filename
, lines
))
104 def CppLintMain(filenames
):
105 all_lint
= common_lint
.RunLintOverAllFiles(CppLint(), filenames
)
106 for lint
in all_lint
:
107 print("%s:%d:%s" % (lint
[0], lint
[1], lint
[2]))
111 if __name__
== "__main__":
112 sys
.exit(CppLintMain(sys
.argv
[1:]))