|
1 | 1 | #!/usr/bin/env python
|
| 2 | +# -*- coding: utf-8 -*- |
2 | 3 | #
|
3 | 4 | # Copyright (c) 2009 Google Inc. All rights reserved.
|
4 | 5 | #
|
|
76 | 77 |
|
77 | 78 | Flags:
|
78 | 79 |
|
79 |
| - output=vs7 |
| 80 | + output=emacs|vs7|eclipse|sed|gsed |
80 | 81 | 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. |
82 | 89 |
|
83 | 90 | verbose=#
|
84 | 91 | Specify a number 0-5 to restrict errors to certain verbosity levels.
|
|
533 | 540 | # Match string that indicates we're working on a Linux Kernel file.
|
534 | 541 | _SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
|
535 | 542 |
|
| 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 | + |
536 | 558 | _regexp_compile_cache = {}
|
537 | 559 |
|
538 | 560 | # {str, set(int)}: a map from error categories to sets of linenumbers
|
@@ -939,7 +961,7 @@ def PrintErrorCounts(self):
|
939 | 961 | for category, count in self.errors_by_category.iteritems():
|
940 | 962 | sys.stderr.write('Category \'%s\' errors found: %d\n' %
|
941 | 963 | (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) |
943 | 965 |
|
944 | 966 | _cpplint_state = _CppLintState()
|
945 | 967 |
|
@@ -1219,6 +1241,13 @@ def Error(filename, linenum, category, confidence, message):
|
1219 | 1241 | elif _cpplint_state.output_format == 'eclipse':
|
1220 | 1242 | sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
|
1221 | 1243 | 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)) |
1222 | 1251 | else:
|
1223 | 1252 | fileinfo = FileInfo(filename)
|
1224 | 1253 | path_from_root = fileinfo.RepositoryName()
|
@@ -6505,7 +6534,7 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]):
|
6505 | 6534 | Error(filename, linenum, 'whitespace/newline', 1,
|
6506 | 6535 | 'Unexpected \\r (^M) found; better to use only \\n')
|
6507 | 6536 |
|
6508 |
| - sys.stdout.write('Done processing %s\n' % path_from_root) |
| 6537 | + sys.stdout.write('# Done processing %s\n' % path_from_root) |
6509 | 6538 | _RestoreFilters()
|
6510 | 6539 |
|
6511 | 6540 |
|
@@ -6562,7 +6591,7 @@ def ParseArguments(args):
|
6562 | 6591 | if opt == '--help':
|
6563 | 6592 | PrintUsage(None)
|
6564 | 6593 | elif opt == '--output':
|
6565 |
| - if val not in ('emacs', 'vs7', 'eclipse'): |
| 6594 | + if val not in ('emacs', 'vs7', 'eclipse', 'sed', 'gsed'): |
6566 | 6595 | PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
|
6567 | 6596 | output_format = val
|
6568 | 6597 | elif opt == '--verbose':
|
|
0 commit comments