2019-09-17 21:00:59 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2019 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 "exprengine.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "symboldatabase.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "testsuite.h"
|
|
|
|
|
2019-09-22 21:14:20 +02:00
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
class TestExprEngine : public TestFixture {
|
|
|
|
public:
|
|
|
|
TestExprEngine() : TestFixture("TestExprEngine") {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run() OVERRIDE {
|
|
|
|
TEST_CASE(argPointer);
|
2019-09-22 10:56:49 +02:00
|
|
|
TEST_CASE(argSmartPointer);
|
2019-09-17 21:00:59 +02:00
|
|
|
TEST_CASE(argStruct);
|
|
|
|
|
2019-09-24 22:22:16 +02:00
|
|
|
TEST_CASE(dynamicAllocation1);
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
TEST_CASE(expr1);
|
|
|
|
TEST_CASE(expr2);
|
|
|
|
TEST_CASE(expr3);
|
|
|
|
TEST_CASE(expr4);
|
|
|
|
TEST_CASE(expr5);
|
2019-09-21 21:15:51 +02:00
|
|
|
TEST_CASE(exprAssign1);
|
2019-09-23 20:27:13 +02:00
|
|
|
TEST_CASE(exprAssign2); // Truncation
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2019-09-22 21:14:20 +02:00
|
|
|
TEST_CASE(floatValue1);
|
|
|
|
TEST_CASE(floatValue2);
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
TEST_CASE(functionCall1);
|
2019-09-22 16:40:48 +02:00
|
|
|
TEST_CASE(functionCall2);
|
2019-09-24 19:53:33 +02:00
|
|
|
TEST_CASE(functionCall3);
|
2019-09-17 21:00:59 +02:00
|
|
|
|
|
|
|
TEST_CASE(if1);
|
|
|
|
TEST_CASE(if2);
|
|
|
|
TEST_CASE(if3);
|
2019-09-20 21:27:51 +02:00
|
|
|
TEST_CASE(if4);
|
|
|
|
TEST_CASE(if5);
|
2019-09-17 21:00:59 +02:00
|
|
|
|
|
|
|
TEST_CASE(ifelse1);
|
|
|
|
|
|
|
|
TEST_CASE(localArray1);
|
2019-09-21 11:36:34 +02:00
|
|
|
TEST_CASE(localArray2);
|
2019-09-24 20:10:51 +02:00
|
|
|
TEST_CASE(localArrayInit1);
|
|
|
|
TEST_CASE(localArrayInit2);
|
2019-09-17 21:00:59 +02:00
|
|
|
TEST_CASE(localArrayUninit);
|
|
|
|
|
|
|
|
TEST_CASE(pointerAlias1);
|
|
|
|
TEST_CASE(pointerAlias2);
|
2019-09-22 15:58:55 +02:00
|
|
|
TEST_CASE(pointerAlias3);
|
|
|
|
TEST_CASE(pointerAlias4);
|
2019-09-23 18:10:06 +02:00
|
|
|
TEST_CASE(pointerNull1);
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-22 15:58:55 +02:00
|
|
|
std::string getRange(const char code[], const std::string &str, int linenr = 0) {
|
2019-09-17 21:00:59 +02:00
|
|
|
Settings settings;
|
|
|
|
settings.platform(cppcheck::Platform::Unix64);
|
2019-09-22 10:56:49 +02:00
|
|
|
settings.library.smartPointers.insert("std::shared_ptr");
|
2019-09-17 21:00:59 +02:00
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
std::string ret;
|
|
|
|
std::function<void(const Token *, const ExprEngine::Value &)> f = [&](const Token *tok, const ExprEngine::Value &value) {
|
2019-09-22 15:58:55 +02:00
|
|
|
if ((linenr == 0 || linenr == tok->linenr()) && tok->expressionString() == str) {
|
2019-09-21 14:17:16 +02:00
|
|
|
if (!ret.empty())
|
|
|
|
ret += ",";
|
2019-09-17 21:00:59 +02:00
|
|
|
ret += value.getRange();
|
2019-09-21 14:17:16 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
};
|
|
|
|
std::vector<ExprEngine::Callback> callbacks;
|
|
|
|
callbacks.push_back(f);
|
|
|
|
ExprEngine::executeAllFunctions(&tokenizer, &settings, callbacks);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void argPointer() {
|
2019-09-22 10:56:49 +02:00
|
|
|
ASSERT_EQUALS("->0:255,null,->?", getRange("void f(unsigned char *p) { a = *p; }", "p"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void argSmartPointer() {
|
|
|
|
ASSERT_EQUALS("->$1,null", getRange("struct S { int x; }; void f(std::shared_ptr<S> ptr) { x = ptr; }", "ptr"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void argStruct() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("0:510",
|
2019-09-17 21:00:59 +02:00
|
|
|
getRange("struct S {\n"
|
|
|
|
" unsigned char a;\n"
|
|
|
|
" unsigned char b;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f(struct S s) { return s.a + s.b; }", "s.a+s.b"));
|
|
|
|
}
|
|
|
|
|
2019-09-24 22:22:16 +02:00
|
|
|
void dynamicAllocation1() {
|
2019-09-25 18:33:21 +02:00
|
|
|
ASSERT_EQUALS("size=1,[:]=0", getRange("char *f() { char *p = calloc(1,1); return p; }", "p"));
|
2019-09-24 22:22:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void expr1() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("-32768:32767", getRange("void f(short x) { a = x; }", "x"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void expr2() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("-65536:65534", getRange("void f(short x) { a = x + x; }", "x+x"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void expr3() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("-65536:65534", getRange("int f(short x) { int a = x + x; return a; }", "return a"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void expr4() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("0", getRange("int f(short x) { int a = x - x; return a; }", "return a"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void expr5() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("-65536:65534", getRange("void f(short a, short b, short c, short d) { if (a+b<c+d) {} }", "a+b"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 21:15:51 +02:00
|
|
|
void exprAssign1() {
|
|
|
|
ASSERT_EQUALS("1:256", getRange("void f(unsigned char a) { a += 1; }", "a+=1"));
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:27:13 +02:00
|
|
|
void exprAssign2() {
|
|
|
|
ASSERT_EQUALS("2", getRange("void f(unsigned char x) { x = 258; int a = x }", "a=x"));
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:14:20 +02:00
|
|
|
void floatValue1() {
|
|
|
|
ASSERT_EQUALS(std::to_string(std::numeric_limits<float>::min()) + ":" + std::to_string(std::numeric_limits<float>::max()), getRange("float f; void func() { f=f; }", "f=f"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void floatValue2() {
|
|
|
|
ASSERT_EQUALS("14.500000", getRange("void func() { float f = 29.0; f = f / 2.0; }", "f/2.0"));
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void functionCall1() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("-2147483648:2147483647", getRange("int atoi(const char *p); void f() { int x = atoi(a); x = x; }", "x=x"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-22 16:40:48 +02:00
|
|
|
void functionCall2() {
|
|
|
|
const char code[] = "namespace NS {\n"
|
|
|
|
" short getValue();\n"
|
|
|
|
"}"
|
|
|
|
"void f() {\n"
|
|
|
|
" short value = NS::getValue();\n"
|
|
|
|
" value = value;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS("-32768:32767", getRange(code, "value=value"));
|
|
|
|
}
|
|
|
|
|
2019-09-24 19:53:33 +02:00
|
|
|
void functionCall3() {
|
|
|
|
ASSERT_EQUALS("-2147483648:2147483647", getRange("void f() { int x = -1; fgets(stdin, \"%d\", &x); x=x; }", "x=x"));
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void if1() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("7:32768", getRange("inf f(short x) { if (x > 5) a = x + 1; }", "x+1"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void if2() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("7:32768,-32767:6", getRange("inf f(short x) { if (x > 5) {} a = x + 1; }", "x+1"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void if3() {
|
2019-09-25 18:33:21 +02:00
|
|
|
ASSERT_EQUALS("1,-2147483648:2147483647,-2147483648:2147483647", getRange("void f() { int x; if (a) { if (b) x=1; } x=x; }", "x=x"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 21:27:51 +02:00
|
|
|
void if4() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("1:2147483647,-2147483648:-1", getRange("int x; void f() { if (x) { a=x; }}", "a=x"));
|
2019-09-20 21:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void if5() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("0", getRange("int x; void f() { if (x) {} else { a=x; }}", "a=x"));
|
2019-09-20 21:27:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void ifelse1() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("-32767:6", getRange("inf f(short x) { if (x > 5) ; else a = x + 1; }", "x+1"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void localArray1() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("5", getRange("inf f() { int arr[10]; arr[4] = 5; return arr[4]; }", "arr[4]"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 11:36:34 +02:00
|
|
|
void localArray2() {
|
2019-09-24 20:10:51 +02:00
|
|
|
ASSERT_EQUALS("0:255", getRange("int f() { unsigned char arr[10] = \"\"; dostuff(arr); return arr[4]; }", "arr[4]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void localArrayInit1() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("0", getRange("inf f() { char arr[10] = \"\"; return arr[4]; }", "arr[4]"));
|
2019-09-21 11:36:34 +02:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:51 +02:00
|
|
|
void localArrayInit2() {
|
|
|
|
ASSERT_EQUALS("66", getRange("void f() { char str[] = \"hello\"; str[0] = \'B\'; }", "str[0]=\'B\'"));
|
2019-09-21 19:34:06 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void localArrayUninit() {
|
2019-09-22 15:58:55 +02:00
|
|
|
ASSERT_EQUALS("?", getRange("int f() { int arr[10]; return arr[4]; }", "arr[4]"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pointerAlias1() {
|
2019-09-22 15:58:55 +02:00
|
|
|
ASSERT_EQUALS("3", getRange("int f() { int x; int *p = &x; x = 3; return *p; }", "return*p"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pointerAlias2() {
|
2019-09-22 15:58:55 +02:00
|
|
|
ASSERT_EQUALS("1", getRange("int f() { int x; int *p = &x; *p = 1; return *p; }", "return*p"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pointerAlias3() {
|
|
|
|
ASSERT_EQUALS("7", getRange("int f() {\n"
|
|
|
|
" int x = 18;\n"
|
|
|
|
" int *p = &x;\n"
|
|
|
|
" *p = 7;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}", "x", 5));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pointerAlias4() {
|
|
|
|
ASSERT_EQUALS("71", getRange("int f() { int x[10]; int *p = x+3; *p = 71; return x[3]; }", "x[3]"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
2019-09-23 18:10:06 +02:00
|
|
|
|
|
|
|
void pointerNull1() {
|
2019-09-25 18:33:21 +02:00
|
|
|
ASSERT_EQUALS("0", getRange("void f(void *p) { p = NULL; p += 1; }", "p+=1"));
|
2019-09-23 18:10:06 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestExprEngine)
|