mirror of
https://github.com/google/styleguide.git
synced 2024-03-22 13:11:43 +08:00
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:
parent
6d8d983b11
commit
ab53edf6ff
33
cpplint/cpplint.py
vendored
33
cpplint/cpplint.py
vendored
|
@ -55,7 +55,8 @@ import unicodedata
|
||||||
|
|
||||||
_USAGE = """
|
_USAGE = """
|
||||||
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
|
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
|
||||||
[--counting=total|toplevel|detailed]
|
[--counting=total|toplevel|detailed] [--root=subdir]
|
||||||
|
[--linelength=digits]
|
||||||
<file> [file] ...
|
<file> [file] ...
|
||||||
|
|
||||||
The style guidelines this tries to follow are those in
|
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_
|
No flag => CHROME_BROWSER_UI_BROWSER_H_
|
||||||
--root=chrome => BROWSER_UI_BROWSER_H_
|
--root=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.
|
# We categorize each error message we print. Here are the categories.
|
||||||
|
@ -428,6 +436,10 @@ _error_suppressions = {}
|
||||||
# This is set by --root flag.
|
# This is set by --root flag.
|
||||||
_root = None
|
_root = None
|
||||||
|
|
||||||
|
# The allowed line length of files.
|
||||||
|
# This is set by --linelength flag.
|
||||||
|
_line_length = 80
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
|
@ -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'^\s*//.*http(s?)://\S*$', line) and
|
||||||
not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
|
not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
|
||||||
line_width = GetLineWidth(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,
|
error(filename, linenum, 'whitespace/line_length', 4,
|
||||||
'Lines should very rarely be longer than 100 characters')
|
'Lines should very rarely be longer than %i characters' %
|
||||||
elif line_width > 80:
|
extended_length)
|
||||||
|
elif line_width > _line_length:
|
||||||
error(filename, linenum, 'whitespace/line_length', 2,
|
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
|
if (cleansed_line.count(';') > 1 and
|
||||||
# for loops are allowed two ;'s (and may run over two lines).
|
# 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=',
|
(opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
|
||||||
'counting=',
|
'counting=',
|
||||||
'filter=',
|
'filter=',
|
||||||
'root='])
|
'root=',
|
||||||
|
'linelength='])
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError:
|
||||||
PrintUsage('Invalid arguments.')
|
PrintUsage('Invalid arguments.')
|
||||||
|
|
||||||
|
@ -4678,6 +4693,12 @@ def ParseArguments(args):
|
||||||
elif opt == '--root':
|
elif opt == '--root':
|
||||||
global _root
|
global _root
|
||||||
_root = val
|
_root = val
|
||||||
|
elif opt == '--linelength':
|
||||||
|
global _line_length
|
||||||
|
try:
|
||||||
|
_line_length = int(val)
|
||||||
|
except ValueError:
|
||||||
|
PrintUsage('Line length must be digits.')
|
||||||
|
|
||||||
if not filenames:
|
if not filenames:
|
||||||
PrintUsage('No files were specified.')
|
PrintUsage('No files were specified.')
|
||||||
|
|
|
@ -2620,6 +2620,7 @@ class CpplintTest(CpplintTestBase):
|
||||||
old_output_format = cpplint._cpplint_state.output_format
|
old_output_format = cpplint._cpplint_state.output_format
|
||||||
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
|
||||||
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 = ''
|
||||||
|
@ -2668,12 +2669,39 @@ class CpplintTest(CpplintTestBase):
|
||||||
|
|
||||||
self.assertEquals(['foo.cc', 'foo.h'],
|
self.assertEquals(['foo.cc', 'foo.h'],
|
||||||
cpplint.ParseArguments(['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:
|
finally:
|
||||||
cpplint._USAGE = old_usage
|
cpplint._USAGE = old_usage
|
||||||
cpplint._ERROR_CATEGORIES = old_error_categories
|
cpplint._ERROR_CATEGORIES = old_error_categories
|
||||||
cpplint._cpplint_state.output_format = old_output_format
|
cpplint._cpplint_state.output_format = old_output_format
|
||||||
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
|
||||||
|
|
||||||
|
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):
|
def testFilter(self):
|
||||||
old_filters = cpplint._cpplint_state.filters
|
old_filters = cpplint._cpplint_state.filters
|
||||||
|
|
Loading…
Reference in New Issue
Block a user