Add an --extensions flag to change valid file extensions.

The valid cpplint extensions are .h, .cpp, .cc, .cuh and .cu. It is
common for .hpp to be a valid extension to check, the --extensions flag
allows the user to specify which extensions they think are valid.

Patch by Matt Clarkson <mattyclarkson@gmail.com>
This commit is contained in:
erg@google.com 2013-12-16 22:48:54 +00:00
parent 0075d1469a
commit 1968027290
2 changed files with 28 additions and 5 deletions

27
cpplint/cpplint.py vendored
View File

@ -71,7 +71,8 @@ Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
suppresses errors of all categories on that line. suppresses errors of all categories on that line.
The files passed in will be linted; at least one file must be provided. The files passed in will be linted; at least one file must be provided.
Linted extensions are .cc, .cpp, and .h. Other file types will be ignored. Default linted extensions are .cc, .cpp, .cu, .cuh and .h. Change the
extensions with the --extensions flag.
Flags: Flags:
@ -126,6 +127,12 @@ Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
Examples: Examples:
--linelength=120 --linelength=120
extensions=extension,extension,...
The allowed file extensions that cpplint will check
Examples:
--extensions=hpp,cpp
""" """
# We categorize each error message we print. Here are the categories. # We categorize each error message we print. Here are the categories.
@ -440,6 +447,10 @@ _root = None
# This is set by --linelength flag. # This is set by --linelength flag.
_line_length = 80 _line_length = 80
# The allowed extensions for file names
# This is set by --extensions flag.
_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
def ParseNolintSuppressions(filename, raw_line, linenum, error): def ParseNolintSuppressions(filename, raw_line, linenum, error):
"""Updates the global list of error-suppressions. """Updates the global list of error-suppressions.
@ -4612,10 +4623,9 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]):
# When reading from stdin, the extension is unknown, so no cpplint tests # When reading from stdin, the extension is unknown, so no cpplint tests
# should rely on the extension. # should rely on the extension.
valid_extensions = ['cc', 'h', 'cpp', 'cu', 'cuh'] if filename != '-' and file_extension not in _valid_extensions:
if filename != '-' and file_extension not in valid_extensions:
sys.stderr.write('Ignoring %s; not a valid file name ' sys.stderr.write('Ignoring %s; not a valid file name '
'(.cc, .h, .cpp, .cu, .cuh)\n' % filename) '(%s)\n' % (filename, ', '.join(_valid_extensions)))
else: else:
ProcessFileData(filename, file_extension, lines, Error, ProcessFileData(filename, file_extension, lines, Error,
extra_check_functions) extra_check_functions)
@ -4667,7 +4677,8 @@ def ParseArguments(args):
'counting=', 'counting=',
'filter=', 'filter=',
'root=', 'root=',
'linelength=']) 'linelength=',
'extensions='])
except getopt.GetoptError: except getopt.GetoptError:
PrintUsage('Invalid arguments.') PrintUsage('Invalid arguments.')
@ -4702,6 +4713,12 @@ def ParseArguments(args):
_line_length = int(val) _line_length = int(val)
except ValueError: except ValueError:
PrintUsage('Line length must be digits.') PrintUsage('Line length must be digits.')
elif opt == '--extensions':
global _valid_extensions
try:
_valid_extensions = set(val.split(','))
except ValueError:
PrintUsage('Extensions must be comma seperated list.')
if not filenames: if not filenames:
PrintUsage('No files were specified.') PrintUsage('No files were specified.')

View File

@ -2623,6 +2623,7 @@ class CpplintTest(CpplintTestBase):
old_verbose_level = cpplint._cpplint_state.verbose_level old_verbose_level = cpplint._cpplint_state.verbose_level
old_filters = cpplint._cpplint_state.filters old_filters = cpplint._cpplint_state.filters
old_line_length = cpplint._line_length old_line_length = cpplint._line_length
old_valid_extensions = cpplint._valid_extensions
try: try:
# Don't print usage during the tests, or filter categories # Don't print usage during the tests, or filter categories
cpplint._USAGE = '' cpplint._USAGE = ''
@ -2675,6 +2676,10 @@ class CpplintTest(CpplintTestBase):
self.assertEqual(['foo.h'], self.assertEqual(['foo.h'],
cpplint.ParseArguments(['--linelength=120', 'foo.h'])) cpplint.ParseArguments(['--linelength=120', 'foo.h']))
self.assertEqual(120, cpplint._line_length) self.assertEqual(120, cpplint._line_length)
self.assertEqual(['foo.h'],
cpplint.ParseArguments(['--extensions=hpp,cpp,cpp', 'foo.h']))
self.assertEqual(set(['hpp', 'cpp']), cpplint._valid_extensions)
finally: finally:
cpplint._USAGE = old_usage cpplint._USAGE = old_usage
cpplint._ERROR_CATEGORIES = old_error_categories cpplint._ERROR_CATEGORIES = old_error_categories
@ -2682,6 +2687,7 @@ class CpplintTest(CpplintTestBase):
cpplint._cpplint_state.verbose_level = old_verbose_level cpplint._cpplint_state.verbose_level = old_verbose_level
cpplint._cpplint_state.filters = old_filters cpplint._cpplint_state.filters = old_filters
cpplint._line_length = old_line_length cpplint._line_length = old_line_length
cpplint._valid_extensions = old_valid_extensions
def testLineLength(self): def testLineLength(self):
old_line_length = cpplint._line_length old_line_length = cpplint._line_length