Skip to content

Commit

Permalink
c_languages: Add CPPCheckBear
Browse files Browse the repository at this point in the history
References coala#389
  • Loading branch information
Redridge committed May 23, 2016
1 parent 6128792 commit 7437fe7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .ci/deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sudo add-apt-repository -y ppa:staticfloat/juliareleases
sudo add-apt-repository -y ppa:staticfloat/julia-deps
sudo add-apt-repository -y ppa:avsm/ppa
sudo apt-get -y update
deps="espeak libclang1-3.4 indent mono-mcs chktex hlint r-base julia luarocks verilator"
deps="espeak libclang1-3.4 indent mono-mcs chktex hlint r-base julia luarocks verilator cppcheck"
deps_python_dbus="libdbus-glib-1-dev libdbus-1-dev"
deps_python_gi="glib2.0-dev gobject-introspection libgirepository1.0-dev python3-cairo-dev"
deps_perl="perl libperl-critic-perl"
Expand Down
38 changes: 38 additions & 0 deletions bears/c_languages/CPPCheckBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from coalib.bearlib.abstractions.Linter import linter
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.settings.Setting import typed_list


@linter(executable="cppcheck",
use_stdout=False,
use_stderr=True,
output_format="regex",
output_regex=r'(?P<line>\d+):(?P<severity>[a-zA-Z]+):'
r'(?P<origin>[a-zA-Z]+):(?P<message>.*)',
severity_map={"error": RESULT_SEVERITY.MAJOR,
"warning": RESULT_SEVERITY.NORMAL,
"style": RESULT_SEVERITY.INFO})
class CPPCheckBear:
"""
Report possible security weaknesses for C/C++.
For more information, consult <https://github.com/danmar/cppcheck>.
"""

LANGUAGES = "C", "C++"

@staticmethod
def create_arguments(filename, file, config_file,
enable: typed_list(str)=[]):
"""
:param enable:
Choose specific issues to report. Issues that can be
reported are: all, warning, style, performance,
portability, information, unusedFunction,
missingInclude
"""
args = ("--template={line}:{severity}:{id}:{message}",)

if enable:
args += ('--enable=' + ",".join(enable),)

return args + (filename,)
37 changes: 37 additions & 0 deletions tests/c_languages/CPPCheckBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from bears.c_languages.CPPCheckBear import CPPCheckBear
from tests.LocalBearTestHelper import verify_local_bear

good_file = """
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}""".splitlines(keepends=True)

warn_file = """
void f1(struct fred_t *p)
{
int x;
if (p)
do_something(x);
}""".splitlines(keepends=True)

bad_file = """
#define f(c) { \
char *p = new char[10]; \
p[c] = 42; \
}
int main() {
f(100);
return 0;
}""".splitlines(keepends=True)


CPPCheckBearTest1 = verify_local_bear(CPPCheckBear,
valid_files=(good_file, warn_file),
invalid_files=(bad_file,))

CPPCheckBearTest2 = verify_local_bear(CPPCheckBear,
valid_files=(good_file,),
invalid_files=(warn_file, bad_file),
settings={'enable': 'unusedFunction'})

0 comments on commit 7437fe7

Please sign in to comment.