2014-01-04 20:57:02 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2013 Daniel Marjamäki and 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testsuite.h"
|
|
|
|
#include "testutils.h"
|
|
|
|
#include "valueflow.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "token.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
class TestValueFlow : public TestFixture {
|
|
|
|
public:
|
|
|
|
TestValueFlow() : TestFixture("TestValueFlow") {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void run() {
|
2014-01-06 10:09:49 +01:00
|
|
|
TEST_CASE(valueFlowBeforeCondition);
|
2014-01-07 19:20:56 +01:00
|
|
|
TEST_CASE(valueFlowForLoop);
|
2014-01-06 16:37:52 +01:00
|
|
|
TEST_CASE(valueFlowSubFunction);
|
2014-01-04 20:57:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool testValueOfX(const char code[], unsigned int linenr, int value) {
|
|
|
|
Settings settings;
|
|
|
|
settings.valueFlow = true; // temporary flag
|
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
|
|
|
|
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
|
|
|
|
if (tok->str() == "x" && tok->linenr() == linenr) {
|
|
|
|
std::list<ValueFlow::Value>::const_iterator it;
|
|
|
|
for (it = tok->values.begin(); it != tok->values.end(); ++it) {
|
|
|
|
if (it->intvalue == value)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-06 07:44:58 +01:00
|
|
|
|
|
|
|
void bailout(const char code[]) {
|
|
|
|
Settings settings;
|
|
|
|
settings.valueFlow = true; // temporary flag
|
|
|
|
settings.debugwarnings = true;
|
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
errout.str("");
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-04 20:57:02 +01:00
|
|
|
void valueFlowBeforeCondition() {
|
|
|
|
const char code[] = "void f(int x) {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 2U, 123));
|
2014-01-06 07:44:58 +01:00
|
|
|
|
2014-01-07 19:46:13 +01:00
|
|
|
// bailout: ?:
|
|
|
|
bailout("void f(int x) {\n"
|
|
|
|
" y = ((x<0) ? x : ((x==2)?3:4));\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: variable x stopping on :\n", errout.str());
|
|
|
|
|
2014-01-06 11:27:56 +01:00
|
|
|
// bailout: if/else/etc
|
|
|
|
bailout("void f(int x) {\n"
|
|
|
|
" if (x != 123) { b = x; }\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: variable x stopping on }\n", errout.str());
|
|
|
|
|
2014-01-06 07:44:58 +01:00
|
|
|
// bailout: assignment
|
|
|
|
bailout("void f(int x) {\n"
|
|
|
|
" x = y;\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: assignment of x\n", errout.str());
|
|
|
|
|
|
|
|
// bailout: global variables
|
|
|
|
bailout("int x;\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" int a = x;\n"
|
|
|
|
" if (x == 123) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (debug) ValueFlow bailout: global variable x\n", errout.str());
|
2014-01-04 20:57:02 +01:00
|
|
|
}
|
2014-01-06 16:37:52 +01:00
|
|
|
|
2014-01-07 19:20:56 +01:00
|
|
|
void valueFlowForLoop() {
|
|
|
|
const char code[] = "void f() {\n"
|
|
|
|
" for (int x = 0; x < 10; x++)\n"
|
|
|
|
" a[x] = 0;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
|
|
|
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
|
|
|
|
}
|
|
|
|
|
2014-01-06 16:37:52 +01:00
|
|
|
void valueFlowSubFunction() {
|
|
|
|
const char *code;
|
|
|
|
|
|
|
|
code = "void f1(int x) { return x; }\n"
|
|
|
|
"void f2(int x) {\n"
|
|
|
|
" f1(123);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 1U, 123));
|
|
|
|
|
|
|
|
code = "void f1(int x) { return x; }\n"
|
|
|
|
"void f2(int x) {\n"
|
|
|
|
" f1(x);\n"
|
|
|
|
" if (x==0){}\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS(true, testValueOfX(code, 1U, 0));
|
|
|
|
}
|
2014-01-04 20:57:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestValueFlow)
|