You’re working on a non-trivial project, with multiple source files, in multiple languages. You want to use cpplint to get a “style report” for all C/C++ files in the project.
You don’t want to get bogged down with hundreds of style warnings – you just want the final summary of warnings count per category.
In addition, you want to run only on C/C++ files, and avoid duplicates resulting from the build-directory.
How do you accomplish that?
Shell-Foo is a series of fun ways to take advantage of the powers of the shell. In the series, I highlight shell one-liners that I found useful or interesting. Most of the entries should work on bash on Linux, OS X and other UNIX-variants. Some probably work with other shells as well. Your mileage may vary.
Feel free to suggest your own Shell-Foo one-liners!
The solution
$ ./cpplint.py --counting=detailed \ $( find . -name \*.h -or -name \*.cc | grep -vE "^\.\/build\/" ) 2>&1 | \ grep -e "Category" -e "Total error"
Explanation
- cpplint needs to work on a list of files, so I generated a list of all C/C++ source files, excluding the build dir.
- The
--counting=detailed
flag makes cpplint print a summary with a count for each category. - cpplint writes to STDERR, so I redirect it to STDOUT (
2>&1
) so I can grep it. - grepping for “Category” prints the summary lines (that contain the word “Category”), and grepping for “Total error” prints the total error count. Passing both patterns with the
-e
flags means “either one” (logic “or”).
Example run
Here’s an example run, on a project that may or may not be real or related to me in any way 😉
$ ./cpplint.py --counting=detailed $( find . -name \*.h -or -name \*.cc | grep -vE "^\.\/build\/" ) 2>&1 | grep -e "Category" -e "Total error" Category 'runtime/threadsafe_fn' errors found: 3 Category 'readability/casting' errors found: 18 Category 'whitespace/semicolon' errors found: 16 Category 'runtime/references' errors found: 11 Category 'whitespace/operators' errors found: 130 Category 'runtime/int' errors found: 2 Category 'whitespace/comments' errors found: 72 Category 'build/include_what_you_use' errors found: 4 Category 'whitespace/line_length' errors found: 39 Category 'legal/copyright' errors found: 33 Category 'whitespace/end_of_line' errors found: 17 Category 'readability/todo' errors found: 14 Category 'whitespace/comma' errors found: 88 Category 'build/c++11' errors found: 1 Category 'readability/streams' errors found: 24 Category 'whitespace/indent' errors found: 14 Category 'runtime/explicit' errors found: 9 Category 'runtime/arrays' errors found: 1 Category 'whitespace/newline' errors found: 1 Category 'readability/braces' errors found: 2 Category 'build/include_order' errors found: 21 Category 'whitespace/tab' errors found: 35 Category 'whitespace/blank_line' errors found: 12 Category 'whitespace/todo' errors found: 4 Category 'whitespace/parens' errors found: 86 Category 'whitespace/braces' errors found: 84 Total errors found: 741
Leave a Reply