Skip to content

Commit

Permalink
Added a --linelength flag so that the default of 80 can be changed.
Browse files Browse the repository at this point in the history
Review URL: https://codereview.appspot.com/22180043

Patch from Matt Clarkson <[email protected]>.
  • Loading branch information
[email protected] committed Nov 5, 2013
1 parent 6d8d983 commit ab53edf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
33 changes: 27 additions & 6 deletions cpplint/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@

_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
Expand Down Expand Up @@ -118,6 +119,13 @@
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.
Expand Down Expand Up @@ -428,6 +436,10 @@
# 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.
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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.')

Expand Down Expand Up @@ -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.')
Expand Down
28 changes: 28 additions & 0 deletions cpplint/cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,7 @@ def testParseArguments(self):
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 = ''
Expand Down Expand Up @@ -2668,12 +2669,39 @@ def testParseArguments(self):

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
Expand Down

0 comments on commit ab53edf

Please sign in to comment.