build-unit-test-docker: add cppcheck static analysis tool
Add the cppcheck static analysis tool. This tool's output is only for
information while it's being tuned. The tool is meant to find security
flaws in cpp as well as finding performance improvements and style. In
this case, the style improvements have been primarily scope reduction
or using a standard library algorithm instead of a raw mechanism.
The author of a patch should check the log output from their presubmit
to see if this tool has identified anything. The reviewers can also
check.
The tuning here refers to the configuration. This tool was chosen
partially because it's under very active development and partially
because it's very quick.
If cppcheck reports a false positive, please file an issue against that
project including the pertinent information.
Change-Id: Ia43d4b9d0995dfa6667c2a8b09d072931936e437
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/build-unit-test-docker.sh b/build-unit-test-docker.sh
index 85bae94..03ea535 100755
--- a/build-unit-test-docker.sh
+++ b/build-unit-test-docker.sh
@@ -92,6 +92,7 @@
[libvncserver]=7b1ef0ffc4815cab9a96c7278394152bdc89dc4d
# version from meta-openembedded/meta-oe/recipes-support/libtinyxml2/libtinyxml2_5.0.1.bb
[tinyxml2]=37bc3aca429f0164adf68c23444540b4a24b5778
+ [cppcheck]=df32b0fb05f0c951ab0efa691292c7428f3f50a9
)
# Turn the depcache into a dictionary so we can reference the HEAD of each repo
@@ -225,6 +226,13 @@
RUN curl -L https://dl.bintray.com/boostorg/release/${PKG_REV['boost']}/source/boost_$(echo "${PKG_REV['boost']}" | tr '.' '_').tar.bz2 | tar -xj && \
cp -a -r boost_*/boost ${PREFIX}/include
+FROM openbmc-base as openbmc-cppcheck
+RUN curl -L https://github.com/danmar/cppcheck/archive/${PKG_REV['cppcheck']}.tar.gz | tar -xz && \
+cd cppcheck-* && \
+mkdir "${PREFIX}/cppcheck-cfg" && cp cfg/* "${PREFIX}/cppcheck-cfg/" && \
+make -j$(nproc) CFGDIR="${PREFIX}/cppcheck-cfg" CXXFLAGS="-O2 -DNDEBUG -Wall -Wno-sign-compare -Wno-unused-function" && \
+make PREFIX=${PREFIX} install
+
FROM openbmc-base as openbmc-tinyxml2
RUN curl -L https://github.com/leethomason/tinyxml2/archive/${PKG_REV['tinyxml2']}.tar.gz | tar -xz && \
cd tinyxml2-* && \
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index 16ffa11..1cf615b 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -399,6 +399,20 @@
check_call_cmd(root, 'cat', os.path.join(root, 'test-suite.log'))
raise Exception('Unit tests failed')
+def run_cppcheck(top_dir):
+ try:
+ # http://cppcheck.sourceforge.net/manual.pdf
+ ignore_list = ['-i%s' % path for path in os.listdir(top_dir) \
+ 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('.')
+
+ check_call_cmd(top_dir, *params)
+ except CalledProcessError:
+ raise Exception('Cppcheck failed')
def maybe_run_valgrind(top_dir):
"""
@@ -542,6 +556,7 @@
run_unit_tests(top_dir)
maybe_run_valgrind(top_dir)
maybe_run_coverage(top_dir)
+ run_cppcheck(top_dir)
os.umask(prev_umask)
@@ -556,6 +571,7 @@
check_call_cmd(top_dir, 'ctest', '.')
maybe_run_valgrind(top_dir)
maybe_run_coverage(top_dir)
+ run_cppcheck(top_dir)
else:
print "Not a supported repo for CI Tests, exit"