2011-07-06 08:55:17 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2011 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 "tokenize.h"
|
|
|
|
#include "check64bit.h"
|
|
|
|
#include "testsuite.h"
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class Test64BitPortability : public TestFixture {
|
2011-07-06 08:55:17 +02:00
|
|
|
public:
|
|
|
|
Test64BitPortability() : TestFixture("Test64BitPortability")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void run() {
|
2011-07-06 08:55:17 +02:00
|
|
|
TEST_CASE(novardecl);
|
|
|
|
TEST_CASE(functionpar);
|
|
|
|
TEST_CASE(structmember);
|
2011-07-07 15:14:33 +02:00
|
|
|
TEST_CASE(ptrcompare);
|
2011-09-04 13:03:29 +02:00
|
|
|
TEST_CASE(ptrarithmetic);
|
2011-07-06 08:55:17 +02:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void check(const char code[]) {
|
2011-07-06 08:55:17 +02:00
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
Settings settings;
|
2011-09-03 15:30:30 +02:00
|
|
|
settings.addEnabled("portability");
|
2011-07-06 08:55:17 +02:00
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
|
|
|
|
// Check char variable usage..
|
|
|
|
Check64BitPortability check64BitPortability(&tokenizer, &settings, this);
|
|
|
|
check64BitPortability.pointerassignment();
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void novardecl() {
|
2011-07-06 08:55:17 +02:00
|
|
|
// if the variable declarations can't be seen then skip the warning
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" a = p;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void functionpar() {
|
2011-07-06 08:55:17 +02:00
|
|
|
check("int foo(int *p)\n"
|
|
|
|
"{\n"
|
|
|
|
" int a = p;\n"
|
|
|
|
" return a + 4;\n"
|
|
|
|
"}\n");
|
2011-07-06 12:57:45 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an address value to the integer (int/long/etc) type is not portable\n", errout.str());
|
2011-07-06 08:55:17 +02:00
|
|
|
|
|
|
|
check("int foo(int p[])\n"
|
|
|
|
"{\n"
|
|
|
|
" int a = p;\n"
|
|
|
|
" return a + 4;\n"
|
|
|
|
"}\n");
|
2011-07-06 17:57:39 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an address value to the integer (int/long/etc) type is not portable\n", errout.str());
|
2011-07-06 08:55:17 +02:00
|
|
|
|
2011-07-16 17:08:03 +02:00
|
|
|
check("int foo(int p[])\n"
|
|
|
|
"{\n"
|
|
|
|
" int *a = p;\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2011-07-06 08:55:17 +02:00
|
|
|
check("void foo(int x)\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = x;\n"
|
|
|
|
" *p = 0;\n"
|
|
|
|
"}\n");
|
2011-07-06 17:57:39 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an integer (int/long/etc) to a pointer is not portable\n", errout.str());
|
2011-07-06 08:55:17 +02:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void structmember() {
|
2011-07-06 08:55:17 +02:00
|
|
|
check("struct Foo { int *p };\n"
|
|
|
|
"void f(struct Foo *foo) {\n"
|
|
|
|
" int i = foo->p;\n"
|
|
|
|
"}\n");
|
2011-07-06 12:57:45 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an address value to the integer (int/long/etc) type is not portable\n", "", errout.str());
|
2011-07-06 08:55:17 +02:00
|
|
|
}
|
2011-07-07 15:14:33 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void ptrcompare() {
|
2011-07-07 15:14:33 +02:00
|
|
|
// Ticket #2892
|
|
|
|
check("void foo(int *p) {\n"
|
|
|
|
" int a = (p != NULL);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2011-09-04 13:03:29 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void ptrarithmetic() {
|
2011-09-04 13:03:29 +02:00
|
|
|
// #3073
|
|
|
|
check("void foo(int *p) {\n"
|
|
|
|
" int x = 10;\n"
|
|
|
|
" int *a = p + x;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void foo(int *p) {\n"
|
|
|
|
" int x = 10;\n"
|
|
|
|
" int *a = x + p;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void foo(int *p) {\n"
|
|
|
|
" int x = 10;\n"
|
|
|
|
" int *a = x * x;\n"
|
|
|
|
"}\n");
|
|
|
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
2011-10-12 19:01:44 +02:00
|
|
|
|
|
|
|
check("void foo(int *start, int *end) {\n"
|
|
|
|
" int len;\n"
|
|
|
|
" int len = end + 10 - start;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-09-04 13:03:29 +02:00
|
|
|
}
|
2011-07-06 08:55:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(Test64BitPortability)
|
|
|
|
|