Skip to content

Commit 29e0f32

Browse files
author
Daniel Kroening
authored
Merge pull request #553 from rjmunro/feature/cpplint-sed-format
cpplint sed output format
2 parents 0cccfe3 + 8ccd570 commit 29e0f32

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

scripts/cpplint.py

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
23
#
34
# Copyright (c) 2009 Google Inc. All rights reserved.
45
#
@@ -76,9 +77,15 @@
7677
7778
Flags:
7879
79-
output=vs7
80+
output=emacs|vs7|eclipse|sed|gsed
8081
By default, the output is formatted to ease emacs parsing. Visual Studio
81-
compatible output (vs7) may also be used. Other formats are unsupported.
82+
(vs7) or eclipse (eclipse) compatible output may also be used.
83+
84+
The sed format outputs sed commands that should fix the reported errors.
85+
Note that this requires gnu sed. If that is installed as gsed on your system
86+
(common on MacOS e.g. with homebrew) you can use the gsed output format.
87+
Sed commands are written to stdout, not stderr, so you should be able to
88+
pipe output straight to bash to run the fixes.
8289
8390
verbose=#
8491
Specify a number 0-5 to restrict errors to certain verbosity levels.
@@ -533,6 +540,21 @@
533540
# Match string that indicates we're working on a Linux Kernel file.
534541
_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
535542

543+
# Commands for sed to fix the problem
544+
_SED_FIXUPS = {
545+
"Remove spaces around =": "s/ = /=/",
546+
"Remove spaces around !=": "s/ != /!=/",
547+
"Remove space before ( in if (": "s/if (/if(/",
548+
"Remove space before ( in for (": "s/for (/for(/",
549+
"Remove space before ( in while (": "s/while (/while(/",
550+
"Remove space before ( in switch (": "s/switch (/switch(/",
551+
"Should have a space between // and comment": 's/\/\//\/\/ /',
552+
"Missing space before {": r's/\([^ ]\){/\1 {/',
553+
"Tab found, replace by spaces": r's/\t/ /',
554+
"Line ends in whitespace. Consider deleting these extra spaces.": r's/\s*$//',
555+
#"Redundant blank line at the end of a code block should be deleted.": "d", # messes up line numbers for other errors.
556+
}
557+
536558
_regexp_compile_cache = {}
537559

538560
# {str, set(int)}: a map from error categories to sets of linenumbers
@@ -939,7 +961,7 @@ def PrintErrorCounts(self):
939961
for category, count in self.errors_by_category.iteritems():
940962
sys.stderr.write('Category \'%s\' errors found: %d\n' %
941963
(category, count))
942-
sys.stdout.write('Total errors found: %d\n' % self.error_count)
964+
sys.stdout.write('Total errors found: %d\n' % self.error_count)
943965

944966
_cpplint_state = _CppLintState()
945967

@@ -1219,6 +1241,13 @@ def Error(filename, linenum, category, confidence, message):
12191241
elif _cpplint_state.output_format == 'eclipse':
12201242
sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
12211243
filename, linenum, message, category, confidence))
1244+
elif _cpplint_state.output_format in ['sed', 'gsed']:
1245+
if message in _SED_FIXUPS:
1246+
sys.stdout.write(_cpplint_state.output_format + " -i '%s%s' %s # %s [%s] [%d]\n" % (
1247+
linenum, _SED_FIXUPS[message], filename, message, category, confidence))
1248+
else:
1249+
sys.stderr.write('# %s:%s: "%s" [%s] [%d]\n' % (
1250+
filename, linenum, message, category, confidence))
12221251
else:
12231252
fileinfo = FileInfo(filename)
12241253
path_from_root = fileinfo.RepositoryName()
@@ -6505,7 +6534,7 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]):
65056534
Error(filename, linenum, 'whitespace/newline', 1,
65066535
'Unexpected \\r (^M) found; better to use only \\n')
65076536

6508-
sys.stdout.write('Done processing %s\n' % path_from_root)
6537+
sys.stdout.write('# Done processing %s\n' % path_from_root)
65096538
_RestoreFilters()
65106539

65116540

@@ -6562,7 +6591,7 @@ def ParseArguments(args):
65626591
if opt == '--help':
65636592
PrintUsage(None)
65646593
elif opt == '--output':
6565-
if val not in ('emacs', 'vs7', 'eclipse'):
6594+
if val not in ('emacs', 'vs7', 'eclipse', 'sed', 'gsed'):
65666595
PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
65676596
output_format = val
65686597
elif opt == '--verbose':

0 commit comments

Comments
 (0)