From dacece665f82b929f08268bc8a6ae09e5ba32849 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 13:20:29 +0000 Subject: [PATCH 01/11] Add python encoding line --- scripts/cpplint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index 24662915ae5..d250e30b559 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- # # Copyright (c) 2009 Google Inc. All rights reserved. # From e6b1201c1fc4250702216633844b3dc863c7d5e1 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 12:23:10 +0000 Subject: [PATCH 02/11] Added "sed" output format that allow automatic fixes --- scripts/cpplint.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index d250e30b559..cec97736b58 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -534,6 +534,11 @@ # Match string that indicates we're working on a Linux Kernel file. _SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)') +# Commands for sed to fix the problem +_SED_FIXUPS = { + "Remove spaces around =": "s/ = /=/", +} + _regexp_compile_cache = {} # {str, set(int)}: a map from error categories to sets of linenumbers @@ -1220,6 +1225,13 @@ def Error(filename, linenum, category, confidence, message): elif _cpplint_state.output_format == 'eclipse': sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % ( filename, linenum, message, category, confidence)) + elif _cpplint_state.output_format == 'sed': + if message in _SED_FIXUPS: + sys.stderr.write("sed -i '%s%s' %s # %s [%s] [%d]\n" % ( + linenum, _SED_FIXUPS[message], filename, message, category, confidence)) + else: + sys.stderr.write('# %s:%s: "%s" [%s] [%d]\n' % ( + filename, linenum, message, category, confidence)) else: fileinfo = FileInfo(filename) path_from_root = fileinfo.RepositoryName() @@ -6563,7 +6575,7 @@ def ParseArguments(args): if opt == '--help': PrintUsage(None) elif opt == '--output': - if val not in ('emacs', 'vs7', 'eclipse'): + if val not in ('emacs', 'vs7', 'eclipse', 'sed'): PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.') output_format = val elif opt == '--verbose': From eb515e40c418762238230ec081dd41669b7d11e9 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 12:23:35 +0000 Subject: [PATCH 03/11] Add more types of fix to sed output format --- scripts/cpplint.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index cec97736b58..c28000a7ee8 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -537,6 +537,11 @@ # Commands for sed to fix the problem _SED_FIXUPS = { "Remove spaces around =": "s/ = /=/", + "Remove spaces around !=": "s/ != /!=/", + "Remove space before ( in if (": "s/if (/if(/", + "Remove space before ( in for (": "s/for (/for(/", + "Remove space before ( in while (": "s/while (/while(/", + "Remove space before ( in switch (": "s/switch (/switch(/", } _regexp_compile_cache = {} From b3d1c1e4873ad2b105e3209acaac97bd7747244d Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 12:27:52 +0000 Subject: [PATCH 04/11] Add commented Fix that removes extra lines Works, but breaks subsequent fixes in the same file. We will need to reverse the order of line numbers if we want to use this, so that we apply fixes at the end of the file first. --- scripts/cpplint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index c28000a7ee8..859a2589913 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -542,6 +542,7 @@ "Remove space before ( in for (": "s/for (/for(/", "Remove space before ( in while (": "s/while (/while(/", "Remove space before ( in switch (": "s/switch (/switch(/", + #"Redundant blank line at the end of a code block should be deleted.": "d", # messes up line numbers for other errors. } _regexp_compile_cache = {} From 58b8d75f7cbb32ab86fa78cd3f64a60e05f384f8 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 13:19:58 +0000 Subject: [PATCH 05/11] Add fix for space after `//` comments --- scripts/cpplint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index 859a2589913..ad1196cff07 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -542,6 +542,7 @@ "Remove space before ( in for (": "s/for (/for(/", "Remove space before ( in while (": "s/while (/while(/", "Remove space before ( in switch (": "s/switch (/switch(/", + "Should have a space between // and comment": 's/\/\//\/\/ /', #"Redundant blank line at the end of a code block should be deleted.": "d", # messes up line numbers for other errors. } From cb9bb30660b1661631fd8f8c33569c3ec37a8e8f Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 13:35:18 +0000 Subject: [PATCH 06/11] Add fixer for missing space before { --- scripts/cpplint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index ad1196cff07..d254295ec94 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -543,6 +543,7 @@ "Remove space before ( in while (": "s/while (/while(/", "Remove space before ( in switch (": "s/switch (/switch(/", "Should have a space between // and comment": 's/\/\//\/\/ /', + "Missing space before {": r's/\([^ ]\){/\1 {/', #"Redundant blank line at the end of a code block should be deleted.": "d", # messes up line numbers for other errors. } From 6646e4b7124c689e0f6951902dabe52ee8a71e12 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 13:58:34 +0000 Subject: [PATCH 07/11] Add tabs -> 2 spaces fixer --- scripts/cpplint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index d254295ec94..aff3bc9fe03 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -544,6 +544,7 @@ "Remove space before ( in switch (": "s/switch (/switch(/", "Should have a space between // and comment": 's/\/\//\/\/ /', "Missing space before {": r's/\([^ ]\){/\1 {/', + "Tab found, replace by spaces": r's/\t/ /', #"Redundant blank line at the end of a code block should be deleted.": "d", # messes up line numbers for other errors. } From a55cd86c37bd57120dae8241b445ee02e45580c7 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 14:39:57 +0000 Subject: [PATCH 08/11] Add fixer to delete whitespace at end of lines --- scripts/cpplint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index aff3bc9fe03..ae571ae74c2 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -545,6 +545,7 @@ "Should have a space between // and comment": 's/\/\//\/\/ /', "Missing space before {": r's/\([^ ]\){/\1 {/', "Tab found, replace by spaces": r's/\t/ /', + "Line ends in whitespace. Consider deleting these extra spaces.": r's/\s*$//', #"Redundant blank line at the end of a code block should be deleted.": "d", # messes up line numbers for other errors. } From 8ea1668b4a6b946ddb9f1748ee360b195eb318a5 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 13:39:18 +0000 Subject: [PATCH 09/11] Support gsed output format for Mac OS gnu sed is installed with this name by homebrew. --- scripts/cpplint.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index ae571ae74c2..4c1106c7afa 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -1235,9 +1235,9 @@ def Error(filename, linenum, category, confidence, message): elif _cpplint_state.output_format == 'eclipse': sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % ( filename, linenum, message, category, confidence)) - elif _cpplint_state.output_format == 'sed': + elif _cpplint_state.output_format in ['sed', 'gsed']: if message in _SED_FIXUPS: - sys.stderr.write("sed -i '%s%s' %s # %s [%s] [%d]\n" % ( + sys.stderr.write(_cpplint_state.output_format + " -i '%s%s' %s # %s [%s] [%d]\n" % ( linenum, _SED_FIXUPS[message], filename, message, category, confidence)) else: sys.stderr.write('# %s:%s: "%s" [%s] [%d]\n' % ( @@ -6585,7 +6585,7 @@ def ParseArguments(args): if opt == '--help': PrintUsage(None) elif opt == '--output': - if val not in ('emacs', 'vs7', 'eclipse', 'sed'): + if val not in ('emacs', 'vs7', 'eclipse', 'sed', 'gsed'): PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.') output_format = val elif opt == '--verbose': From 75cbe79e4e8b96463d0029e26aff7e05f413e551 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 13:41:22 +0000 Subject: [PATCH 10/11] Write sed output to stdout for ease of piping to bash Also fix other writes to stdout to be preceeded by # --- scripts/cpplint.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index 4c1106c7afa..8dd4ef7cacd 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -955,7 +955,7 @@ def PrintErrorCounts(self): for category, count in self.errors_by_category.iteritems(): sys.stderr.write('Category \'%s\' errors found: %d\n' % (category, count)) - sys.stdout.write('Total errors found: %d\n' % self.error_count) + sys.stdout.write('# Total errors found: %d\n' % self.error_count) _cpplint_state = _CppLintState() @@ -1237,7 +1237,7 @@ def Error(filename, linenum, category, confidence, message): filename, linenum, message, category, confidence)) elif _cpplint_state.output_format in ['sed', 'gsed']: if message in _SED_FIXUPS: - sys.stderr.write(_cpplint_state.output_format + " -i '%s%s' %s # %s [%s] [%d]\n" % ( + sys.stdout.write(_cpplint_state.output_format + " -i '%s%s' %s # %s [%s] [%d]\n" % ( linenum, _SED_FIXUPS[message], filename, message, category, confidence)) else: sys.stderr.write('# %s:%s: "%s" [%s] [%d]\n' % ( @@ -6528,7 +6528,7 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]): Error(filename, linenum, 'whitespace/newline', 1, 'Unexpected \\r (^M) found; better to use only \\n') - sys.stdout.write('Done processing %s\n' % path_from_root) + sys.stdout.write('# Done processing %s\n' % path_from_root) _RestoreFilters() From 8ccd570e094863658b9c58a71cd8e900eccf8ab6 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 16 Feb 2017 13:35:03 +0000 Subject: [PATCH 11/11] Document output formats --- scripts/cpplint.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index 8dd4ef7cacd..7fdc937ecf5 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -77,9 +77,15 @@ Flags: - output=vs7 + output=emacs|vs7|eclipse|sed|gsed By default, the output is formatted to ease emacs parsing. Visual Studio - compatible output (vs7) may also be used. Other formats are unsupported. + (vs7) or eclipse (eclipse) compatible output may also be used. + + The sed format outputs sed commands that should fix the reported errors. + Note that this requires gnu sed. If that is installed as gsed on your system + (common on MacOS e.g. with homebrew) you can use the gsed output format. + Sed commands are written to stdout, not stderr, so you should be able to + pipe output straight to bash to run the fixes. verbose=# Specify a number 0-5 to restrict errors to certain verbosity levels.