diff --git a/Makefile b/Makefile index 3cf7660c4..71651e37e 100644 --- a/Makefile +++ b/Makefile @@ -229,6 +229,7 @@ TESTOBJ = test/options.o \ test/testbool.o \ test/testboost.o \ test/testbufferoverrun.o \ + test/testbughuntingchecks.o \ test/testcharvar.o \ test/testclangimport.o \ test/testclass.o \ @@ -606,6 +607,9 @@ test/testboost.o: test/testboost.cpp lib/check.h lib/checkboost.h lib/config.h l test/testbufferoverrun.o: test/testbufferoverrun.cpp externals/tinyxml/tinyxml2.h lib/check.h lib/checkbufferoverrun.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testbufferoverrun.o test/testbufferoverrun.cpp +test/testbughuntingchecks.o: test/testbughuntingchecks.cpp lib/config.h lib/errorlogger.h lib/errortypes.h lib/exprengine.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h + $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testbughuntingchecks.o test/testbughuntingchecks.cpp + test/testcharvar.o: test/testcharvar.cpp lib/check.h lib/checkother.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testcharvar.o test/testcharvar.cpp diff --git a/test/testbughuntingchecks.cpp b/test/testbughuntingchecks.cpp new file mode 100644 index 000000000..8086e8620 --- /dev/null +++ b/test/testbughuntingchecks.cpp @@ -0,0 +1,59 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2020 Cppcheck team. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "config.h" +#include "exprengine.h" +#include "settings.h" +#include "tokenize.h" +#include "testsuite.h" + +class TestBughuntingChecks : public TestFixture { +public: + TestBughuntingChecks() : TestFixture("TestBughuntingChecks") { + } + +private: + void run() OVERRIDE { +#ifdef USE_Z3 + TEST_CASE(uninit); +#endif + } + + void check(const char code[]) { + Settings settings; + settings.platform(cppcheck::Platform::Unix64); + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + errout.str(""); + ExprEngine::runChecks(this, &tokenizer, &settings); + } + + void uninit() { + check("void foo() { int x; x = x + 1; }"); + TODO_ASSERT_EQUALS("[test.cpp:1]: (error) Cannot determine that 'x' is initialized\n", "", errout.str()); + + check("void foo() { int x; int y = x + 1; }"); + ASSERT_EQUALS("[test.cpp:1]: (error) Cannot determine that 'x' is initialized\n", errout.str()); + + check("void foo() { int x; x++; }"); + TODO_ASSERT_EQUALS("[test.cpp:1]: (error) Cannot determine that 'x' is initialized\n", "", errout.str()); + } +}; + +REGISTER_TEST(TestBughuntingChecks)