Skip to content

Commit 389efa2

Browse files
authored
Merge pull request google#183 from LukeCz/hpp-headers
Configurable header extensions
2 parents 1342002 + 8920b13 commit 389efa2

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

cpplint/cpplint.py

Lines changed: 38 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,25 @@
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 = set(['h'])
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+
return file_extension in _hpp_headers
543570

544571
def ParseNolintSuppressions(filename, raw_line, linenum, error):
545572
"""Updates the global list of line error-suppressions.
@@ -4272,7 +4299,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
42724299

42734300
# Check if the line is a header guard.
42744301
is_header_guard = False
4275-
if file_extension == 'h':
4302+
if IsHeaderExtension(file_extension):
42764303
cppvar = GetHeaderGuardCPPVariable(filename)
42774304
if (line.startswith('#ifndef %s' % cppvar) or
42784305
line.startswith('#define %s' % cppvar) or
@@ -4622,7 +4649,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
46224649
CheckGlobalStatic(filename, clean_lines, linenum, error)
46234650
CheckPrintf(filename, clean_lines, linenum, error)
46244651

4625-
if file_extension == 'h':
4652+
if IsHeaderExtension(file_extension):
46264653
# TODO(unknown): check that 1-arg constructors are explicit.
46274654
# How to tell it's a constructor?
46284655
# (handled in CheckForNonStandardConstructs for now)
@@ -4729,7 +4756,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
47294756
# Check for use of unnamed namespaces in header files. Registration
47304757
# macros are typically OK, so we allow use of "namespace {" on lines
47314758
# that end with backslashes.
4732-
if (file_extension == 'h'
4759+
if (IsHeaderExtension(file_extension)
47334760
and Search(r'\bnamespace\s*{', line)
47344761
and line[-1] != '\\'):
47354762
error(filename, linenum, 'build/namespaces', 4,
@@ -5819,7 +5846,7 @@ def ProcessFileData(filename, file_extension, lines, error,
58195846
RemoveMultiLineComments(filename, lines, error)
58205847
clean_lines = CleansedLines(lines)
58215848

5822-
if file_extension == 'h':
5849+
if IsHeaderExtension(file_extension):
58235850
CheckForHeaderGuard(filename, clean_lines, error)
58245851

58255852
for line in xrange(clean_lines.NumLines()):
@@ -5902,6 +5929,8 @@ def ProcessConfigOverrides(filename):
59025929
elif name == 'root':
59035930
global _root
59045931
_root = val
5932+
elif name == 'headers':
5933+
ProcessHppHeadersOption(val)
59055934
else:
59065935
sys.stderr.write(
59075936
'Invalid configuration option (%s) in file %s\n' %
@@ -6047,7 +6076,8 @@ def ParseArguments(args):
60476076
'filter=',
60486077
'root=',
60496078
'linelength=',
6050-
'extensions='])
6079+
'extensions=',
6080+
'headers='])
60516081
except getopt.GetoptError:
60526082
PrintUsage('Invalid arguments.')
60536083

@@ -6088,6 +6118,8 @@ def ParseArguments(args):
60886118
_valid_extensions = set(val.split(','))
60896119
except ValueError:
60906120
PrintUsage('Extensions must be comma seperated list.')
6121+
elif opt == '--headers':
6122+
ProcessHppHeadersOption(val)
60916123

60926124
if not filenames:
60936125
PrintUsage('No files were specified.')

cpplint/cpplint_unittest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3778,6 +3778,7 @@ def testParseArguments(self):
37783778
old_error_categories = cpplint._ERROR_CATEGORIES
37793779
old_output_format = cpplint._cpplint_state.output_format
37803780
old_verbose_level = cpplint._cpplint_state.verbose_level
3781+
old_headers = cpplint._hpp_headers
37813782
old_filters = cpplint._cpplint_state.filters
37823783
old_line_length = cpplint._line_length
37833784
old_valid_extensions = cpplint._valid_extensions
@@ -3795,6 +3796,7 @@ def testParseArguments(self):
37953796
self.assertRaises(SystemExit, cpplint.ParseArguments, ['--filter=foo'])
37963797
self.assertRaises(SystemExit, cpplint.ParseArguments,
37973798
['--filter=+a,b,-c'])
3799+
self.assertRaises(SystemExit, cpplint.ParseArguments, ['--headers'])
37983800

37993801
self.assertEquals(['foo.cc'], cpplint.ParseArguments(['foo.cc']))
38003802
self.assertEquals(old_output_format, cpplint._cpplint_state.output_format)
@@ -3837,6 +3839,13 @@ def testParseArguments(self):
38373839
self.assertEqual(['foo.h'],
38383840
cpplint.ParseArguments(['--extensions=hpp,cpp,cpp', 'foo.h']))
38393841
self.assertEqual(set(['hpp', 'cpp']), cpplint._valid_extensions)
3842+
3843+
self.assertEqual(set(['h']), cpplint._hpp_headers) # Default value
3844+
self.assertEqual(['foo.h'],
3845+
cpplint.ParseArguments(['--extensions=cpp,cpp', '--headers=hpp,h', 'foo.h']))
3846+
self.assertEqual(set(['hpp', 'h']), cpplint._hpp_headers)
3847+
self.assertEqual(set(['hpp', 'h', 'cpp']), cpplint._valid_extensions)
3848+
38403849
finally:
38413850
cpplint._USAGE = old_usage
38423851
cpplint._ERROR_CATEGORIES = old_error_categories
@@ -3845,6 +3854,7 @@ def testParseArguments(self):
38453854
cpplint._cpplint_state.filters = old_filters
38463855
cpplint._line_length = old_line_length
38473856
cpplint._valid_extensions = old_valid_extensions
3857+
cpplint._hpp_headers = old_headers
38483858

38493859
def testLineLength(self):
38503860
old_line_length = cpplint._line_length

0 commit comments

Comments
 (0)