Skip to content

Commit 58c7561

Browse files
committed
fix cpplint#123: Inconsistent behavior of --headers and --extensions
1 parent 948e8b4 commit 58c7561

2 files changed

Lines changed: 38 additions & 28 deletions

File tree

cpplint.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def unicode_escape_decode(x):
678678

679679
# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
680680
# This is set by --headers flag.
681-
_hpp_headers = set(['h', 'hh', 'hpp', 'hxx', 'h++', 'cuh'])
681+
_hpp_headers = set([])
682682

683683
# {str, bool}: a map from error categories to booleans which indicate if the
684684
# category should be suppressed for every line.
@@ -687,30 +687,38 @@ def unicode_escape_decode(x):
687687
def ProcessHppHeadersOption(val):
688688
global _hpp_headers
689689
try:
690-
_hpp_headers = set(val.split(','))
691-
# Automatically append to extensions list so it does not have to be set 2 times
692-
_valid_extensions.update(_hpp_headers)
690+
_hpp_headers = set([ext.strip() for ext in val.split(',')])
693691
except ValueError:
694692
PrintUsage('Header extensions must be comma separated list.')
695693

696694
def IsHeaderExtension(file_extension):
697-
return file_extension in _hpp_headers
695+
return file_extension in GetHeaderExtensions()
698696

699697
def GetHeaderExtensions():
700-
return _hpp_headers or ['h']
698+
if _hpp_headers:
699+
return _hpp_headers
700+
if _valid_extensions:
701+
return set([h for h in _valid_extensions if 'h' in h])
702+
return set(['h', 'hh', 'hpp', 'hxx', 'h++', 'cuh'])
701703

702704
# The allowed extensions for file names
703705
# This is set by --extensions flag
704706
def GetAllExtensions():
705-
if not _valid_extensions:
706-
return GetHeaderExtensions().union(set(['c', 'cc', 'cpp', 'cxx', 'c++', 'cu']))
707-
return _valid_extensions
707+
return GetHeaderExtensions().union(_valid_extensions or set(['c', 'cc', 'cpp', 'cxx', 'c++', 'cu']))
708+
709+
def ProcessExtensionsOption(val):
710+
global _valid_extensions
711+
try:
712+
extensions = [ext.strip() for ext in val.split(',')]
713+
_valid_extensions = set(extensions)
714+
except ValueError:
715+
PrintUsage('Extensions should be a comma-separated list of values;'
716+
'for example: extensions=hpp,cpp\n'
717+
'This could not be parsed: "%s"' % (val,))
708718

709719
def GetNonHeaderExtensions():
710720
return GetAllExtensions().difference(GetHeaderExtensions())
711721

712-
713-
714722
def ParseNolintSuppressions(filename, raw_line, linenum, error):
715723
"""Updates the global list of line error-suppressions.
716724
@@ -6281,14 +6289,7 @@ def ProcessConfigOverrides(filename):
62816289
except ValueError:
62826290
_cpplint_state.PrintError('Line length must be numeric.')
62836291
elif name == 'extensions':
6284-
global _valid_extensions
6285-
try:
6286-
extensions = [ext.strip() for ext in val.split(',')]
6287-
_valid_extensions = set(extensions)
6288-
except ValueError:
6289-
sys.stderr.write('Extensions should be a comma-separated list of values;'
6290-
'for example: extensions=hpp,cpp\n'
6291-
'This could not be parsed: "%s"' % (val,))
6292+
ProcessExtensionsOption(val)
62926293
elif name == 'root':
62936294
global _root
62946295
# root directories are specified relative to CPPLINT.cfg dir.
@@ -6511,11 +6512,7 @@ def ParseArguments(args):
65116512
_excludes = set()
65126513
_excludes.update(glob.glob(val))
65136514
elif opt == '--extensions':
6514-
global _valid_extensions
6515-
try:
6516-
_valid_extensions = set(val.split(','))
6517-
except ValueError:
6518-
PrintUsage('Extensions must be comma seperated list.')
6515+
ProcessExtensionsOption(val)
65196516
elif opt == '--headers':
65206517
ProcessHppHeadersOption(val)
65216518
elif opt == '--recursive':

cpplint_unittest.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4046,19 +4046,32 @@ def testParseArguments(self):
40464046
self.assertEquals(['foo.cc', 'foo.h'],
40474047
cpplint.ParseArguments(['foo.cc', 'foo.h']))
40484048

4049+
cpplint._hpp_headers = old_headers
4050+
cpplint._valid_extensions = old_valid_extensions
40494051
self.assertEqual(['foo.h'],
40504052
cpplint.ParseArguments(['--linelength=120', 'foo.h']))
40514053
self.assertEqual(120, cpplint._line_length)
4054+
self.assertEqual(set(['h', 'hh', 'hpp', 'hxx', 'h++', 'cuh']), cpplint.GetHeaderExtensions()) # Default value
4055+
4056+
cpplint._hpp_headers = old_headers
4057+
cpplint._valid_extensions = old_valid_extensions
4058+
self.assertEqual(['foo.h'],
4059+
cpplint.ParseArguments(['--headers=h', 'foo.h']))
4060+
self.assertEqual(set(['h', 'c', 'cc', 'cpp', 'cxx', 'c++', 'cu']), cpplint.GetAllExtensions())
40524061

4062+
cpplint._hpp_headers = old_headers
4063+
cpplint._valid_extensions = old_valid_extensions
40534064
self.assertEqual(['foo.h'],
40544065
cpplint.ParseArguments(['--extensions=hpp,cpp,cpp', 'foo.h']))
4055-
self.assertEqual(set(['hpp', 'cpp']), cpplint._valid_extensions)
4066+
self.assertEqual(set(['hpp', 'cpp']), cpplint.GetAllExtensions())
4067+
self.assertEqual(set(['hpp']), cpplint.GetHeaderExtensions())
40564068

4057-
self.assertEqual(set(['h', 'hh', 'hpp', 'hxx', 'h++', 'cuh']), cpplint._hpp_headers) # Default value
4069+
cpplint._hpp_headers = old_headers
4070+
cpplint._valid_extensions = old_valid_extensions
40584071
self.assertEqual(['foo.h'],
40594072
cpplint.ParseArguments(['--extensions=cpp,cpp', '--headers=hpp,h', 'foo.h']))
4060-
self.assertEqual(set(['hpp', 'h']), cpplint._hpp_headers)
4061-
self.assertEqual(set(['hpp', 'h', 'cpp']), cpplint._valid_extensions)
4073+
self.assertEqual(set(['hpp', 'h']), cpplint.GetHeaderExtensions())
4074+
self.assertEqual(set(['hpp', 'h', 'cpp']), cpplint.GetAllExtensions())
40624075

40634076
finally:
40644077
sys.stdout == sys.__stdout__

0 commit comments

Comments
 (0)