Skip to content

Commit 7197a24

Browse files
committed
Configurable header extensions
Fix for google#50, google#79 and google#113
1 parent 1342002 commit 7197a24

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

cpplint/cpplint.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
_USAGE = """
5757
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
5858
[--counting=total|toplevel|detailed] [--root=subdir]
59-
[--linelength=digits]
59+
[--linelength=digits] [--headers=x,y,...]
6060
<file> [file] ...
6161
6262
The style guidelines this tries to follow are those in
@@ -134,6 +134,14 @@
134134
Examples:
135135
--extensions=hpp,cpp
136136
137+
headers=x,y,...
138+
The header extensions that cpplint will treat as .h in checks. Values are
139+
automatically added to --extensions list.
140+
141+
Examples:
142+
--headers=hpp,hxx
143+
--headers=hpp
144+
137145
cpplint.py supports per-directory configurations specified in CPPLINT.cfg
138146
files. CPPLINT.cfg file can contain a number of key=value pairs.
139147
Currently the following options are supported:
@@ -143,6 +151,7 @@
143151
exclude_files=regex
144152
linelength=80
145153
root=subdir
154+
headers=x,y,...
146155
147156
"set noparent" option prevents cpplint from traversing directory tree
148157
upwards looking for more .cfg files in parent directories. This option
@@ -160,6 +169,9 @@
160169
161170
The "root" option is similar in function to the --root flag (see example
162171
above).
172+
173+
The "headers" option is similar in function to the --headers flag
174+
(see example above).
163175
164176
CPPLINT.cfg has an effect on files in the same directory and all
165177
sub-directories, unless overridden by a nested configuration file.
@@ -536,10 +548,28 @@
536548
# This is set by --extensions flag.
537549
_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
538550

551+
# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
552+
# This is set by --headers flag.
553+
_hpp_headers = None
554+
539555
# {str, bool}: a map from error categories to booleans which indicate if the
540556
# category should be suppressed for every line.
541557
_global_error_suppressions = {}
542558

559+
def ProcessHppHeadersOption(val):
560+
global _hpp_headers
561+
try:
562+
_hpp_headers = set(val.split(','))
563+
# Automatically append to extensions list so it does not have to be set 2 times
564+
_valid_extensions.update(_hpp_headers)
565+
except ValueError:
566+
PrintUsage('Header extensions must be comma seperated list.')
567+
568+
def IsHeaderExtension(file_extension):
569+
if _hpp_headers and file_extension in _hpp_headers:
570+
return True
571+
else:
572+
return file_extension == 'h'
543573

544574
def ParseNolintSuppressions(filename, raw_line, linenum, error):
545575
"""Updates the global list of line error-suppressions.
@@ -4272,7 +4302,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
42724302

42734303
# Check if the line is a header guard.
42744304
is_header_guard = False
4275-
if file_extension == 'h':
4305+
if IsHeaderExtension(file_extension):
42764306
cppvar = GetHeaderGuardCPPVariable(filename)
42774307
if (line.startswith('#ifndef %s' % cppvar) or
42784308
line.startswith('#define %s' % cppvar) or
@@ -4622,7 +4652,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
46224652
CheckGlobalStatic(filename, clean_lines, linenum, error)
46234653
CheckPrintf(filename, clean_lines, linenum, error)
46244654

4625-
if file_extension == 'h':
4655+
if IsHeaderExtension(file_extension):
46264656
# TODO(unknown): check that 1-arg constructors are explicit.
46274657
# How to tell it's a constructor?
46284658
# (handled in CheckForNonStandardConstructs for now)
@@ -4729,7 +4759,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
47294759
# Check for use of unnamed namespaces in header files. Registration
47304760
# macros are typically OK, so we allow use of "namespace {" on lines
47314761
# that end with backslashes.
4732-
if (file_extension == 'h'
4762+
if (IsHeaderExtension(file_extension)
47334763
and Search(r'\bnamespace\s*{', line)
47344764
and line[-1] != '\\'):
47354765
error(filename, linenum, 'build/namespaces', 4,
@@ -5819,7 +5849,7 @@ def ProcessFileData(filename, file_extension, lines, error,
58195849
RemoveMultiLineComments(filename, lines, error)
58205850
clean_lines = CleansedLines(lines)
58215851

5822-
if file_extension == 'h':
5852+
if IsHeaderExtension(file_extension):
58235853
CheckForHeaderGuard(filename, clean_lines, error)
58245854

58255855
for line in xrange(clean_lines.NumLines()):
@@ -5902,6 +5932,8 @@ def ProcessConfigOverrides(filename):
59025932
elif name == 'root':
59035933
global _root
59045934
_root = val
5935+
elif name == 'headers':
5936+
ProcessHppHeadersOption(val)
59055937
else:
59065938
sys.stderr.write(
59075939
'Invalid configuration option (%s) in file %s\n' %
@@ -6047,7 +6079,8 @@ def ParseArguments(args):
60476079
'filter=',
60486080
'root=',
60496081
'linelength=',
6050-
'extensions='])
6082+
'extensions=',
6083+
'headers='])
60516084
except getopt.GetoptError:
60526085
PrintUsage('Invalid arguments.')
60536086

@@ -6088,6 +6121,8 @@ def ParseArguments(args):
60886121
_valid_extensions = set(val.split(','))
60896122
except ValueError:
60906123
PrintUsage('Extensions must be comma seperated list.')
6124+
elif opt == '--headers':
6125+
ProcessHppHeadersOption(val)
60916126

60926127
if not filenames:
60936128
PrintUsage('No files were specified.')

0 commit comments

Comments
 (0)