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

Commit f83ab79

Browse files
Marco Massenziotkruse
Marco Massenzio
authored andcommitted
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 f83ab79

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

cpplint.py

+50-19
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
6262
[--counting=total|toplevel|detailed] [--root=subdir]
6363
[--linelength=digits]
64+
[--headers=ext1,ext2]
6465
<file> [file] ...
6566
6667
The style guidelines this tries to follow are those in
@@ -139,6 +140,13 @@
139140
Examples:
140141
--extensions=hpp,cpp
141142
143+
headers=extension,extension,...
144+
The allowed header extensions that cpplint will consider to be header files
145+
(by default, only .h files will be assumed to be headers)
146+
147+
Examples:
148+
--headers=h,hpp
149+
142150
cpplint.py supports per-directory configurations specified in CPPLINT.cfg
143151
files. CPPLINT.cfg file can contain a number of key=value pairs.
144152
Currently the following options are supported:
@@ -519,6 +527,12 @@ def u(x):
519527
itervalues = dict.values
520528
iteritems = dict.items
521529

530+
# Files with any of these extensions are considered to be
531+
# header files (and will undergo different style checks).
532+
# This set can be extended by using the --headers
533+
# option (also supported in CPPLINT.cfg)
534+
_header_extensions = set(['h'])
535+
522536
def ParseNolintSuppressions(filename, raw_line, linenum, error):
523537
"""Updates the global list of error-suppressions.
524538
@@ -1798,21 +1812,22 @@ def CheckHeaderFileIncluded(filename, include_state, error):
17981812
return
17991813

18001814
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]
1815+
for ext in _header_extensions:
1816+
headerfile = filename[0:len(filename) - 2] + ext
1817+
if not os.path.exists(headerfile):
1818+
continue
1819+
headername = FileInfo(headerfile).RepositoryName()
1820+
first_include = None
1821+
for section_list in include_state.include_list:
1822+
for f in section_list:
1823+
if headername in f[0] or f[0] in headername:
1824+
return
1825+
if not first_include:
1826+
first_include = f[1]
18121827

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

18171832

18181833
def CheckForBadCharacters(filename, lines, error):
@@ -4453,7 +4468,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
44534468

44544469
# Check if the line is a header guard.
44554470
is_header_guard = False
4456-
if file_extension == 'h':
4471+
if file_extension in _header_extensions:
44574472
cppvar = GetHeaderGuardCPPVariable(filename)
44584473
if (line.startswith('#ifndef %s' % cppvar) or
44594474
line.startswith('#define %s' % cppvar) or
@@ -4825,7 +4840,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
48254840
CheckGlobalStatic(filename, clean_lines, linenum, error)
48264841
CheckPrintf(filename, clean_lines, linenum, error)
48274842

4828-
if file_extension == 'h':
4843+
if file_extension in _header_extensions:
48294844
# TODO(unknown): check that 1-arg constructors are explicit.
48304845
# How to tell it's a constructor?
48314846
# (handled in CheckForNonStandardConstructs for now)
@@ -4932,7 +4947,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
49324947
# Check for use of unnamed namespaces in header files. Registration
49334948
# macros are typically OK, so we allow use of "namespace {" on lines
49344949
# that end with backslashes.
4935-
if (file_extension == 'h'
4950+
if (file_extension in _header_extensions
49364951
and Search(r'\bnamespace\s*{', line)
49374952
and line[-1] != '\\'):
49384953
error(filename, linenum, 'build/namespaces', 4,
@@ -6048,7 +6063,7 @@ def ProcessFileData(filename, file_extension, lines, error,
60486063
RemoveMultiLineComments(filename, lines, error)
60496064
clean_lines = CleansedLines(lines)
60506065

6051-
if file_extension == 'h':
6066+
if file_extension in _header_extensions:
60526067
CheckForHeaderGuard(filename, clean_lines, error)
60536068

60546069
for line in range(clean_lines.NumLines()):
@@ -6128,6 +6143,15 @@ def ProcessConfigOverrides(filename):
61286143
_line_length = int(val)
61296144
except ValueError:
61306145
sys.stderr.write('Line length must be numeric.')
6146+
elif name == 'extensions':
6147+
global _valid_extensions
6148+
try:
6149+
extensions = [ext.strip() for ext in val.split(',')]
6150+
_valid_extensions = _valid_extensions.union(set(extensions))
6151+
except ValueError:
6152+
sys.stderr.write('Extensions should be a comma-separated list of values;'
6153+
'for example: extensions=hpp,cpp\n'
6154+
'This could not be parsed: "%s"' % (values,))
61316155
else:
61326156
sys.stderr.write(
61336157
'Invalid configuration option (%s) in file %s\n' %
@@ -6274,7 +6298,8 @@ def ParseArguments(args):
62746298
'filter=',
62756299
'root=',
62766300
'linelength=',
6277-
'extensions='])
6301+
'extensions=',
6302+
'headers='])
62786303
except getopt.GetoptError:
62796304
PrintUsage('Invalid arguments.')
62806305

@@ -6315,6 +6340,12 @@ def ParseArguments(args):
63156340
_valid_extensions = set(val.split(','))
63166341
except ValueError:
63176342
PrintUsage('Extensions must be comma seperated list.')
6343+
elif opt == '--headers':
6344+
global _header_extensions
6345+
try:
6346+
_header_extensions = set(val.split(','))
6347+
except ValueError:
6348+
PrintUsage('Extensions must be comma seperated list.')
63186349

63196350
if not filenames:
63206351
PrintUsage('No files were specified.')

0 commit comments

Comments
 (0)