2020-07-15 09:31:13 +02:00
|
|
|
#!/usr/bin/env python
|
2009-01-02 21:15:30 +00:00
|
|
|
#
|
|
|
|
# Checks C++ files to make sure they conform to LLVM standards, as specified in
|
|
|
|
# http://llvm.org/docs/CodingStandards.html .
|
|
|
|
#
|
|
|
|
# TODO: add unittests for the verifier functions:
|
|
|
|
# http://docs.python.org/library/unittest.html .
|
|
|
|
|
2019-01-03 14:11:33 +00:00
|
|
|
from __future__ import print_function
|
2009-01-02 21:15:30 +00:00
|
|
|
import common_lint
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
def VerifyIncludes(filename, lines):
|
|
|
|
"""Makes sure the #includes are in proper order and no disallows files are
|
|
|
|
#included.
|
2009-02-20 23:44:54 +00:00
|
|
|
|
2023-05-15 11:02:42 +02:00
|
|
|
Args:
|
2009-01-02 21:15:30 +00:00
|
|
|
filename: the file under consideration as string
|
|
|
|
lines: contents of the file as string array
|
2023-05-15 11:02:42 +02:00
|
|
|
"""
|
2009-02-20 23:44:54 +00:00
|
|
|
lint = []
|
2009-01-02 21:15:30 +00:00
|
|
|
|
|
|
|
include_gtest_re = re.compile(r'^#include "gtest/(.*)"')
|
|
|
|
include_llvm_re = re.compile(r'^#include "llvm/(.*)"')
|
|
|
|
include_support_re = re.compile(r'^#include "(Support/.*)"')
|
|
|
|
include_config_re = re.compile(r'^#include "(Config/.*)"')
|
|
|
|
include_system_re = re.compile(r"^#include <(.*)>")
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
DISALLOWED_SYSTEM_HEADERS = ["iostream"]
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
line_num = 1
|
|
|
|
prev_config_header = None
|
|
|
|
prev_system_header = None
|
|
|
|
for line in lines:
|
|
|
|
# TODO: implement private headers
|
|
|
|
# TODO: implement gtest headers
|
|
|
|
# TODO: implement top-level llvm/* headers
|
|
|
|
# TODO: implement llvm/Support/* headers
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
# Process Config/* headers
|
|
|
|
config_header = include_config_re.match(line)
|
|
|
|
if config_header:
|
|
|
|
curr_config_header = config_header.group(1)
|
|
|
|
if prev_config_header:
|
|
|
|
if prev_config_header > curr_config_header:
|
|
|
|
lint.append(
|
2023-05-15 11:02:42 +02:00
|
|
|
(
|
2009-01-02 21:24:29 +00:00
|
|
|
filename,
|
2009-02-20 23:44:54 +00:00
|
|
|
line_num,
|
|
|
|
'Config headers not in order: "%s" before "%s"'
|
|
|
|
% (prev_config_header, curr_config_header),
|
2023-05-15 11:02:42 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
# Process system headers
|
2009-01-02 21:24:29 +00:00
|
|
|
system_header = include_system_re.match(line)
|
2009-01-02 21:15:30 +00:00
|
|
|
if system_header:
|
|
|
|
curr_system_header = system_header.group(1)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2020-06-20 00:15:50 -07:00
|
|
|
# Is it disallowed?
|
2009-01-02 21:15:30 +00:00
|
|
|
if curr_system_header in DISALLOWED_SYSTEM_HEADERS:
|
2009-02-20 23:44:54 +00:00
|
|
|
lint.append(
|
2023-05-15 11:02:42 +02:00
|
|
|
(
|
2009-01-02 21:15:30 +00:00
|
|
|
filename,
|
2009-02-20 23:44:54 +00:00
|
|
|
line_num,
|
|
|
|
"Disallowed system header: <%s>" % curr_system_header,
|
2023-05-15 11:02:42 +02:00
|
|
|
)
|
|
|
|
)
|
2009-01-02 21:15:30 +00:00
|
|
|
elif prev_system_header:
|
2009-01-02 21:24:29 +00:00
|
|
|
# Make sure system headers are alphabetized amongst themselves
|
2009-01-02 21:15:30 +00:00
|
|
|
if prev_system_header > curr_system_header:
|
2009-02-20 23:44:54 +00:00
|
|
|
lint.append(
|
2023-05-15 11:02:42 +02:00
|
|
|
(
|
2009-01-02 21:15:30 +00:00
|
|
|
filename,
|
2009-02-20 23:44:54 +00:00
|
|
|
line_num,
|
|
|
|
"System headers not in order: <%s> before <%s>"
|
|
|
|
% (prev_system_header, curr_system_header),
|
2023-05-15 11:02:42 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
prev_system_header = curr_system_header
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
line_num += 1
|
2009-01-02 21:24:29 +00:00
|
|
|
|
2009-02-20 23:44:54 +00:00
|
|
|
return lint
|
2009-01-02 21:15:30 +00:00
|
|
|
|
|
|
|
|
2009-02-20 23:44:54 +00:00
|
|
|
class CppLint(common_lint.BaseLint):
|
|
|
|
MAX_LINE_LENGTH = 80
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
def RunOnFile(self, filename, lines):
|
2009-02-20 23:44:54 +00:00
|
|
|
lint = []
|
|
|
|
lint.extend(VerifyIncludes(filename, lines))
|
|
|
|
lint.extend(
|
|
|
|
common_lint.VerifyLineLength(filename, lines, CppLint.MAX_LINE_LENGTH)
|
2023-05-15 11:02:42 +02:00
|
|
|
)
|
2009-02-20 23:44:54 +00:00
|
|
|
lint.extend(common_lint.VerifyTabs(filename, lines))
|
|
|
|
lint.extend(common_lint.VerifyTrailingWhitespace(filename, lines))
|
|
|
|
return lint
|
2023-05-15 11:02:42 +02:00
|
|
|
|
|
|
|
|
2009-01-02 21:15:30 +00:00
|
|
|
def CppLintMain(filenames):
|
2009-02-20 23:44:54 +00:00
|
|
|
all_lint = common_lint.RunLintOverAllFiles(CppLint(), filenames)
|
|
|
|
for lint in all_lint:
|
2019-01-03 14:11:33 +00:00
|
|
|
print("%s:%d:%s" % (lint[0], lint[1], lint[2]))
|
2009-01-02 21:15:30 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(CppLintMain(sys.argv[1:]))
|