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

Commit a337729

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 b066038 commit a337729

File tree

1 file changed

+60
-21
lines changed

1 file changed

+60
-21
lines changed

cpplint.py

+60-21
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:
@@ -1804,21 +1819,22 @@ def CheckHeaderFileIncluded(filename, include_state, error):
18041819
return
18051820

18061821
fileinfo = FileInfo(filename)
1807-
headerfile = filename[0:len(filename) - 2] + 'h'
1808-
if not os.path.exists(headerfile):
1809-
return
1810-
headername = FileInfo(headerfile).RepositoryName()
1811-
first_include = 0
1812-
for section_list in include_state.include_list:
1813-
for f in section_list:
1814-
if headername in f[0] or f[0] in headername:
1815-
return
1816-
if not first_include:
1817-
first_include = f[1]
1822+
for ext in _header_extensions:
1823+
headerfile = filename[0:len(filename) - 2] + ext
1824+
if not os.path.exists(headerfile):
1825+
continue
1826+
headername = FileInfo(headerfile).RepositoryName()
1827+
first_include = None
1828+
for section_list in include_state.include_list:
1829+
for f in section_list:
1830+
if headername in f[0] or f[0] in headername:
1831+
return
1832+
if not first_include:
1833+
first_include = f[1]
18181834

1819-
error(filename, first_include, 'build/include', 5,
1820-
'%s should include its header file %s' % (fileinfo.RepositoryName(),
1821-
headername))
1835+
error(filename, first_include, 'build/include', 5,
1836+
'%s should include its header file %s' % (fileinfo.RepositoryName(),
1837+
headername))
18221838

18231839

18241840
def CheckForBadCharacters(filename, lines, error):
@@ -4459,7 +4475,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
44594475

44604476
# Check if the line is a header guard.
44614477
is_header_guard = False
4462-
if file_extension == 'h':
4478+
if file_extension in _header_extensions:
44634479
cppvar = GetHeaderGuardCPPVariable(filename)
44644480
if (line.startswith('#ifndef %s' % cppvar) or
44654481
line.startswith('#define %s' % cppvar) or
@@ -4828,7 +4844,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
48284844
CheckGlobalStatic(filename, clean_lines, linenum, error)
48294845
CheckPrintf(filename, clean_lines, linenum, error)
48304846

4831-
if file_extension == 'h':
4847+
if file_extension in _header_extensions:
48324848
# TODO(unknown): check that 1-arg constructors are explicit.
48334849
# How to tell it's a constructor?
48344850
# (handled in CheckForNonStandardConstructs for now)
@@ -4935,7 +4951,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
49354951
# Check for use of unnamed namespaces in header files. Registration
49364952
# macros are typically OK, so we allow use of "namespace {" on lines
49374953
# that end with backslashes.
4938-
if (file_extension == 'h'
4954+
if (file_extension in _header_extensions
49394955
and Search(r'\bnamespace\s*{', line)
49404956
and line[-1] != '\\'):
49414957
error(filename, linenum, 'build/namespaces', 4,
@@ -6051,7 +6067,7 @@ def ProcessFileData(filename, file_extension, lines, error,
60516067
RemoveMultiLineComments(filename, lines, error)
60526068
clean_lines = CleansedLines(lines)
60536069

6054-
if file_extension == 'h':
6070+
if file_extension in _header_extensions:
60556071
CheckForHeaderGuard(filename, clean_lines, error)
60566072

60576073
for line in range(clean_lines.NumLines()):
@@ -6131,6 +6147,22 @@ def ProcessConfigOverrides(filename):
61316147
_line_length = int(val)
61326148
except ValueError:
61336149
sys.stderr.write('Line length must be numeric.')
6150+
elif name == 'extensions':
6151+
global _valid_extensions
6152+
try:
6153+
extensions = [ext.strip() for ext in val.split(',')]
6154+
_valid_extensions = _valid_extensions.union(set(extensions))
6155+
except ValueError:
6156+
sys.stderr.write('Extensions should be a comma-separated list of values;'
6157+
'for example: extensions=hpp,cpp\n'
6158+
'This could not be parsed: "%s"' % (val,))
6159+
try:
6160+
extensions = [ext.strip() for ext in val.split(',')]
6161+
_valid_extensions = _valid_extensions.union(set(extensions))
6162+
except ValueError:
6163+
sys.stderr.write('Extensions should be a comma-separated list of values;'
6164+
'for example: extensions=hpp,cpp\n'
6165+
'This could not be parsed: "%s"' % (values,))
61346166
else:
61356167
sys.stderr.write(
61366168
'Invalid configuration option (%s) in file %s\n' %
@@ -6277,7 +6309,8 @@ def ParseArguments(args):
62776309
'filter=',
62786310
'root=',
62796311
'linelength=',
6280-
'extensions='])
6312+
'extensions=',
6313+
'headers='])
62816314
except getopt.GetoptError:
62826315
PrintUsage('Invalid arguments.')
62836316

@@ -6318,6 +6351,12 @@ def ParseArguments(args):
63186351
_valid_extensions = set(val.split(','))
63196352
except ValueError:
63206353
PrintUsage('Extensions must be comma seperated list.')
6354+
elif opt == '--headers':
6355+
global _header_extensions
6356+
try:
6357+
_header_extensions = set(val.split(','))
6358+
except ValueError:
6359+
PrintUsage('Extensions must be comma seperated list.')
63216360

63226361
if not filenames:
63236362
PrintUsage('No files were specified.')

0 commit comments

Comments
 (0)