Skip to content
This repository was archived by the owner on Jul 13, 2019. It is now read-only.

Commit 25eec63

Browse files
committed
Added an extensible set() for header extensions
Using flag name to --headers as it makes more sense Added the extensions option to the CPPLINT.cfg option file
1 parent dcf8a87 commit 25eec63

File tree

1 file changed

+60
-21
lines changed

1 file changed

+60
-21
lines changed

cpplint.py

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,23 @@
5252
import sys
5353
import unicodedata
5454

55+
# Files with any of these extensions are considered to be
56+
# header files (and will undergo different style checks).
57+
# This set can be extended by using the --headers
58+
# option (also supported in CPPLINT.cfg)
59+
_header_extensions = ['h', 'hpp', 'hxx', 'h++', 'cuh']
60+
61+
5562
# The allowed extensions for file names
5663
# This is set by --extensions flag.
57-
_valid_extensions = set(['c', 'cc', 'cpp', 'cxx', 'c++', 'h', 'hpp', 'hxx',
58-
'h++'])
64+
_valid_extensions = set(['c', 'cc', 'cpp', 'cxx', 'c++', 'cu'] + _header_extensions)
65+
5966

6067
_USAGE = """
6168
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
6269
[--counting=total|toplevel|detailed] [--root=subdir]
6370
[--linelength=digits]
71+
[--headers=ext1,ext2]
6472
<file> [file] ...
6573
6674
The style guidelines this tries to follow are those in
@@ -139,6 +147,13 @@
139147
Examples:
140148
--extensions=hpp,cpp
141149
150+
headers=extension,extension,...
151+
The allowed header extensions that cpplint will consider to be header files
152+
(by default, only .h files will be assumed to be headers)
153+
154+
Examples:
155+
--headers=h,hpp
156+
142157
cpplint.py supports per-directory configurations specified in CPPLINT.cfg
143158
files. CPPLINT.cfg file can contain a number of key=value pairs.
144159
Currently the following options are supported:
@@ -1798,21 +1813,22 @@ def CheckHeaderFileIncluded(filename, include_state, error):
17981813
return
17991814

18001815
fileinfo = FileInfo(filename)
1801-
headerfile = filename[0:len(filename) - 2] + 'h'
1802-
if not os.path.exists(headerfile):
1803-
return
1804-
headername = FileInfo(headerfile).RepositoryName()
1805-
first_include = 0
1806-
for section_list in include_state.include_list:
1807-
for f in section_list:
1808-
if headername in f[0] or f[0] in headername:
1809-
return
1810-
if not first_include:
1811-
first_include = f[1]
1816+
for ext in _header_extensions:
1817+
headerfile = filename[0:len(filename) - 2] + ext
1818+
if not os.path.exists(headerfile):
1819+
continue
1820+
headername = FileInfo(headerfile).RepositoryName()
1821+
first_include = None
1822+
for section_list in include_state.include_list:
1823+
for f in section_list:
1824+
if headername in f[0] or f[0] in headername:
1825+
return
1826+
if not first_include:
1827+
first_include = f[1]
18121828

1813-
error(filename, first_include, 'build/include', 5,
1814-
'%s should include its header file %s' % (fileinfo.RepositoryName(),
1815-
headername))
1829+
error(filename, first_include, 'build/include', 5,
1830+
'%s should include its header file %s' % (fileinfo.RepositoryName(),
1831+
headername))
18161832

18171833

18181834
def CheckForBadCharacters(filename, lines, error):
@@ -4453,7 +4469,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
44534469

44544470
# Check if the line is a header guard.
44554471
is_header_guard = False
4456-
if file_extension == 'h':
4472+
if file_extension in _header_extensions:
44574473
cppvar = GetHeaderGuardCPPVariable(filename)
44584474
if (line.startswith('#ifndef %s' % cppvar) or
44594475
line.startswith('#define %s' % cppvar) or
@@ -4825,7 +4841,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
48254841
CheckGlobalStatic(filename, clean_lines, linenum, error)
48264842
CheckPrintf(filename, clean_lines, linenum, error)
48274843

4828-
if file_extension == 'h':
4844+
if file_extension in _header_extensions:
48294845
# TODO(unknown): check that 1-arg constructors are explicit.
48304846
# How to tell it's a constructor?
48314847
# (handled in CheckForNonStandardConstructs for now)
@@ -4932,7 +4948,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
49324948
# Check for use of unnamed namespaces in header files. Registration
49334949
# macros are typically OK, so we allow use of "namespace {" on lines
49344950
# that end with backslashes.
4935-
if (file_extension == 'h'
4951+
if (file_extension in _header_extensions
49364952
and Search(r'\bnamespace\s*{', line)
49374953
and line[-1] != '\\'):
49384954
error(filename, linenum, 'build/namespaces', 4,
@@ -6048,7 +6064,7 @@ def ProcessFileData(filename, file_extension, lines, error,
60486064
RemoveMultiLineComments(filename, lines, error)
60496065
clean_lines = CleansedLines(lines)
60506066

6051-
if file_extension == 'h':
6067+
if file_extension in _header_extensions:
60526068
CheckForHeaderGuard(filename, clean_lines, error)
60536069

60546070
for line in range(clean_lines.NumLines()):
@@ -6128,6 +6144,22 @@ def ProcessConfigOverrides(filename):
61286144
_line_length = int(val)
61296145
except ValueError:
61306146
sys.stderr.write('Line length must be numeric.')
6147+
elif name == 'extensions':
6148+
global _valid_extensions
6149+
try:
6150+
extensions = [ext.strip() for ext in val.split(',')]
6151+
_valid_extensions = _valid_extensions.union(set(extensions))
6152+
except ValueError:
6153+
sys.stderr.write('Extensions should be a comma-separated list of values;'
6154+
'for example: extensions=hpp,cpp\n'
6155+
'This could not be parsed: "%s"' % (val,))
6156+
try:
6157+
extensions = [ext.strip() for ext in val.split(',')]
6158+
_valid_extensions = _valid_extensions.union(set(extensions))
6159+
except ValueError:
6160+
sys.stderr.write('Extensions should be a comma-separated list of values;'
6161+
'for example: extensions=hpp,cpp\n'
6162+
'This could not be parsed: "%s"' % (values,))
61316163
else:
61326164
sys.stderr.write(
61336165
'Invalid configuration option (%s) in file %s\n' %
@@ -6274,7 +6306,8 @@ def ParseArguments(args):
62746306
'filter=',
62756307
'root=',
62766308
'linelength=',
6277-
'extensions='])
6309+
'extensions=',
6310+
'headers='])
62786311
except getopt.GetoptError:
62796312
PrintUsage('Invalid arguments.')
62806313

@@ -6315,6 +6348,12 @@ def ParseArguments(args):
63156348
_valid_extensions = set(val.split(','))
63166349
except ValueError:
63176350
PrintUsage('Extensions must be comma seperated list.')
6351+
elif opt == '--headers':
6352+
global _header_extensions
6353+
try:
6354+
_header_extensions = set(val.split(','))
6355+
except ValueError:
6356+
PrintUsage('Extensions must be comma seperated list.')
63186357

63196358
if not filenames:
63206359
PrintUsage('No files were specified.')

0 commit comments

Comments
 (0)