cppcheck: rewrite input file selection
Use git ls-files to build the initial file list so build artifacts don't
need to be manually specified like -src or -build.
Run the resulting list through a regex that filters out mako templates
and non c/c++ sources. This removes the need to manually specify
scripts on the ignore list.
Run cppcheck on test code. It should pass too.
Skip cppcheck when there aren't any files found since cppcheck will exit
with non-zero status and abort the overall CI run.
Pass the file list to cppcheck via stdin. As a result, cppcheck needs
to be invoked with Popen rather than check_call so the file list can be
sent to cppcheck stdin.
Change-Id: I927b867b45a6c73cc64101af0588dc05a4da41ab
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index 0dd9fda..c0e80a8 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -538,19 +538,35 @@
raise Exception('Unit tests failed')
def run_cppcheck():
- try:
- # http://cppcheck.sourceforge.net/manual.pdf
- ignore_list = ['-i%s' % path for path in os.listdir(os.getcwd()) \
- if path.endswith('-src') or path.endswith('-build')]
- ignore_list.extend(('-itest', '-iscripts'))
- params = ['cppcheck', '-j', str(multiprocessing.cpu_count()),
- '--enable=all']
- params.extend(ignore_list)
- params.append('.')
+ match_re = re.compile('((?!\.mako\.).)*\.[ch](?:pp)?$', re.I)
+ cppcheck_files = []
+ stdout = subprocess.check_output(['git', 'ls-files'])
- check_call_cmd(*params)
- except CalledProcessError:
+ for f in stdout.decode('utf-8').split():
+ if match_re.match(f):
+ cppcheck_files.append(f)
+
+ if not cppcheck_files:
+ # skip cppcheck if there arent' any c or cpp sources.
+ print("no files")
+ return None
+
+ # http://cppcheck.sourceforge.net/manual.pdf
+ params = ['cppcheck', '-j', str(multiprocessing.cpu_count()),
+ '--enable=all', '--file-list=-']
+
+ cppcheck_process = subprocess.Popen(
+ params,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ stdin=subprocess.PIPE)
+ (stdout, stderr) = cppcheck_process.communicate(
+ input='\n'.join(cppcheck_files))
+
+ if cppcheck_process.wait():
raise Exception('Cppcheck failed')
+ print(stdout)
+ print(stderr)
def is_valgrind_safe():
"""