Added a --linelength flag so that the default of 80 can be changed.

Review URL: https://codereview.appspot.com/22180043

Patch from Matt Clarkson <mattyclarkson@gmail.com>.
This commit is contained in:
erg@google.com 2013-11-05 22:23:37 +00:00
parent 6d8d983b11
commit ab53edf6ff
2 changed files with 55 additions and 6 deletions

33
cpplint/cpplint.py vendored
View File

@ -55,7 +55,8 @@ import unicodedata
_USAGE = """
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
[--counting=total|toplevel|detailed]
[--counting=total|toplevel|detailed] [--root=subdir]
[--linelength=digits]
<file> [file] ...
The style guidelines this tries to follow are those in
@ -118,6 +119,13 @@ Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
No flag => CHROME_BROWSER_UI_BROWSER_H_
--root=chrome => BROWSER_UI_BROWSER_H_
--root=chrome/browser => UI_BROWSER_H_
linelength=digits
This is the allowed line length for the project. The default value is
80 characters.
Examples:
--linelength=120
"""
# We categorize each error message we print. Here are the categories.
@ -428,6 +436,10 @@ _error_suppressions = {}
# This is set by --root flag.
_root = None
# The allowed line length of files.
# This is set by --linelength flag.
_line_length = 80
def ParseNolintSuppressions(filename, raw_line, linenum, error):
"""Updates the global list of error-suppressions.
@ -3392,12 +3404,14 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
not Match(r'^\s*//.*http(s?)://\S*$', line) and
not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
line_width = GetLineWidth(line)
if line_width > 100:
extended_length = int((_line_length * 1.25))
if line_width > extended_length:
error(filename, linenum, 'whitespace/line_length', 4,
'Lines should very rarely be longer than 100 characters')
elif line_width > 80:
'Lines should very rarely be longer than %i characters' %
extended_length)
elif line_width > _line_length:
error(filename, linenum, 'whitespace/line_length', 2,
'Lines should be <= 80 characters long')
'Lines should be <= %i characters long' % _line_length)
if (cleansed_line.count(';') > 1 and
# for loops are allowed two ;'s (and may run over two lines).
@ -4649,7 +4663,8 @@ def ParseArguments(args):
(opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
'counting=',
'filter=',
'root='])
'root=',
'linelength='])
except getopt.GetoptError:
PrintUsage('Invalid arguments.')
@ -4678,6 +4693,12 @@ def ParseArguments(args):
elif opt == '--root':
global _root
_root = val
elif opt == '--linelength':
global _line_length
try:
_line_length = int(val)
except ValueError:
PrintUsage('Line length must be digits.')
if not filenames:
PrintUsage('No files were specified.')

View File

@ -2620,6 +2620,7 @@ class CpplintTest(CpplintTestBase):
old_output_format = cpplint._cpplint_state.output_format
old_verbose_level = cpplint._cpplint_state.verbose_level
old_filters = cpplint._cpplint_state.filters
old_line_length = cpplint._line_length
try:
# Don't print usage during the tests, or filter categories
cpplint._USAGE = ''
@ -2668,12 +2669,39 @@ class CpplintTest(CpplintTestBase):
self.assertEquals(['foo.cc', 'foo.h'],
cpplint.ParseArguments(['foo.cc', 'foo.h']))
self.assertEqual(['foo.h'],
cpplint.ParseArguments(['--linelength=120', 'foo.h']))
self.assertEqual(120, cpplint._line_length)
finally:
cpplint._USAGE = old_usage
cpplint._ERROR_CATEGORIES = old_error_categories
cpplint._cpplint_state.output_format = old_output_format
cpplint._cpplint_state.verbose_level = old_verbose_level
cpplint._cpplint_state.filters = old_filters
cpplint._line_length = old_line_length
def testLineLength(self):
old_line_length = cpplint._line_length
try:
cpplint._line_length = 80
self.TestLint(
'// %s' % ('H' * 77),
'')
self.TestLint(
'// %s' % ('H' * 78),
'Lines should be <= 80 characters long'
' [whitespace/line_length] [2]')
cpplint._line_length = 120
self.TestLint(
'// %s' % ('H' * 117),
'')
self.TestLint(
'// %s' % ('H' * 118),
'Lines should be <= 120 characters long'
' [whitespace/line_length] [2]')
finally:
cpplint._line_length = old_line_length
def testFilter(self):
old_filters = cpplint._cpplint_state.filters