2013-10-27 17:10:43 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2014-02-15 07:45:39 +01:00
|
|
|
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
|
2013-10-27 17:10:43 +01:00
|
|
|
*
|
|
|
|
* 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 "library.h"
|
2013-12-23 10:06:45 +01:00
|
|
|
#include "token.h"
|
|
|
|
#include "tokenlist.h"
|
2013-10-27 17:10:43 +01:00
|
|
|
#include "testsuite.h"
|
2013-12-31 12:30:17 +01:00
|
|
|
#include <tinyxml2.h>
|
2013-10-27 17:10:43 +01:00
|
|
|
|
|
|
|
class TestLibrary : public TestFixture {
|
|
|
|
public:
|
|
|
|
TestLibrary() : TestFixture("TestLibrary") { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void run() {
|
|
|
|
TEST_CASE(empty);
|
|
|
|
TEST_CASE(function);
|
2013-11-21 16:32:53 +01:00
|
|
|
TEST_CASE(function_arg);
|
2014-03-06 06:16:14 +01:00
|
|
|
TEST_CASE(function_arg_any);
|
2014-06-07 16:28:29 +02:00
|
|
|
TEST_CASE(function_arg_valid);
|
2014-07-05 20:31:43 +02:00
|
|
|
TEST_CASE(function_arg_minsize);
|
2013-10-27 17:10:43 +01:00
|
|
|
TEST_CASE(memory);
|
2014-04-19 13:15:06 +02:00
|
|
|
TEST_CASE(memory2); // define extra "free" allocation functions
|
2013-11-23 18:16:40 +01:00
|
|
|
TEST_CASE(resource);
|
2014-06-08 12:09:00 +02:00
|
|
|
TEST_CASE(podtype);
|
2013-10-27 17:10:43 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 18:55:16 +01:00
|
|
|
void empty() const {
|
2013-10-27 17:10:43 +01:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n<def/>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
ASSERT(library.use.empty());
|
|
|
|
ASSERT(library.leakignore.empty());
|
|
|
|
ASSERT(library.argumentChecks.empty());
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:55:16 +01:00
|
|
|
void function() const {
|
2013-10-27 17:10:43 +01:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"foo\">\n"
|
|
|
|
" <noreturn>false</noreturn>\n"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
ASSERT(library.use.empty());
|
|
|
|
ASSERT(library.leakignore.empty());
|
|
|
|
ASSERT(library.argumentChecks.empty());
|
|
|
|
ASSERT(library.isnotnoreturn("foo"));
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:55:16 +01:00
|
|
|
void function_arg() const {
|
2013-11-21 16:32:53 +01:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"foo\">\n"
|
2013-12-23 10:06:45 +01:00
|
|
|
" <arg nr=\"1\"><not-uninit/></arg>\n"
|
|
|
|
" <arg nr=\"2\"><not-null/></arg>\n"
|
|
|
|
" <arg nr=\"3\"><formatstr/></arg>\n"
|
|
|
|
" <arg nr=\"4\"><strz/></arg>\n"
|
2014-06-07 16:28:29 +02:00
|
|
|
" <arg nr=\"5\"><not-bool/></arg>\n"
|
2013-11-21 16:32:53 +01:00
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
ASSERT_EQUALS(true, library.argumentChecks["foo"][1].notuninit);
|
|
|
|
ASSERT_EQUALS(true, library.argumentChecks["foo"][2].notnull);
|
|
|
|
ASSERT_EQUALS(true, library.argumentChecks["foo"][3].formatstr);
|
|
|
|
ASSERT_EQUALS(true, library.argumentChecks["foo"][4].strz);
|
2014-06-07 16:28:29 +02:00
|
|
|
ASSERT_EQUALS(true, library.argumentChecks["foo"][5].notbool);
|
2013-11-21 16:32:53 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 18:55:16 +01:00
|
|
|
void function_arg_any() const {
|
2014-03-06 06:16:14 +01:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
"<function name=\"foo\">\n"
|
|
|
|
" <arg nr=\"any\"><not-uninit/></arg>\n"
|
|
|
|
"</function>\n"
|
|
|
|
"</def>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
ASSERT_EQUALS(true, library.argumentChecks["foo"][-1].notuninit);
|
|
|
|
}
|
|
|
|
|
2014-06-07 16:28:29 +02:00
|
|
|
void function_arg_valid() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"foo\">\n"
|
2014-06-08 18:12:11 +02:00
|
|
|
" <arg nr=\"1\"><valid>1:</valid></arg>\n"
|
|
|
|
" <arg nr=\"2\"><valid>-7:0</valid></arg>\n"
|
|
|
|
" <arg nr=\"3\"><valid>1:5,8</valid></arg>\n"
|
|
|
|
" <arg nr=\"4\"><valid>-1,5</valid></arg>\n"
|
|
|
|
" <arg nr=\"5\"><valid>:1,5</valid></arg>\n"
|
2014-06-07 16:28:29 +02:00
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
|
|
|
|
// 1-
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 1, -10));
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 1, 0));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 1, 1));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 1, 10));
|
|
|
|
|
|
|
|
// -7-0
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 2, -10));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 2, -7));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 2, -3));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 2, 0));
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 2, 1));
|
|
|
|
|
|
|
|
// 1-5,8
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 3, 0));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 3, 1));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 3, 3));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 3, 5));
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 3, 6));
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 3, 7));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 3, 8));
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 3, 9));
|
2014-06-08 18:12:11 +02:00
|
|
|
|
|
|
|
// -1,5
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 4, -10));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 4, -1));
|
|
|
|
|
|
|
|
// :1,5
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 5, -10));
|
|
|
|
ASSERT_EQUALS(true, library.isargvalid("foo", 5, 1));
|
|
|
|
ASSERT_EQUALS(false, library.isargvalid("foo", 5, 2));
|
2014-06-07 16:28:29 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 20:31:43 +02:00
|
|
|
void function_arg_minsize() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"foo\">\n"
|
|
|
|
" <arg nr=\"1\"><minsize type=\"strlen\" arg=\"2\"/></arg>\n"
|
|
|
|
" <arg nr=\"2\"><minsize type=\"argvalue\" arg=\"3\"/></arg>\n"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
|
|
|
|
// arg1: type=strlen arg2
|
|
|
|
const std::list<Library::ArgumentChecks::MinSize> *minsizes = library.argminsizes("foo",1);
|
|
|
|
ASSERT_EQUALS(true, minsizes != nullptr);
|
|
|
|
ASSERT_EQUALS(1U, minsizes ? minsizes->size() : 1U);
|
|
|
|
if (minsizes && minsizes->size() == 1U) {
|
|
|
|
const Library::ArgumentChecks::MinSize &m = minsizes->front();
|
2014-07-06 13:08:22 +02:00
|
|
|
ASSERT_EQUALS(Library::ArgumentChecks::MinSize::STRLEN, m.type);
|
2014-07-05 20:31:43 +02:00
|
|
|
ASSERT_EQUALS(2, m.arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// arg2: type=argvalue arg3
|
|
|
|
minsizes = library.argminsizes("foo", 2);
|
|
|
|
ASSERT_EQUALS(true, minsizes != nullptr);
|
|
|
|
ASSERT_EQUALS(1U, minsizes ? minsizes->size() : 1U);
|
|
|
|
if (minsizes && minsizes->size() == 1U) {
|
|
|
|
const Library::ArgumentChecks::MinSize &m = minsizes->front();
|
2014-07-06 13:08:22 +02:00
|
|
|
ASSERT_EQUALS(Library::ArgumentChecks::MinSize::ARGVALUE, m.type);
|
2014-07-05 20:31:43 +02:00
|
|
|
ASSERT_EQUALS(3, m.arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:55:16 +01:00
|
|
|
void memory() const {
|
2013-10-27 17:10:43 +01:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <memory>\n"
|
|
|
|
" <alloc>CreateX</alloc>\n"
|
|
|
|
" <dealloc>DeleteX</dealloc>\n"
|
|
|
|
" </memory>\n"
|
|
|
|
"</def>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
ASSERT(library.use.empty());
|
|
|
|
ASSERT(library.leakignore.empty());
|
|
|
|
ASSERT(library.argumentChecks.empty());
|
|
|
|
|
|
|
|
ASSERT(Library::ismemory(library.alloc("CreateX")));
|
|
|
|
ASSERT_EQUALS(library.alloc("CreateX"), library.dealloc("DeleteX"));
|
|
|
|
}
|
2014-04-19 13:15:06 +02:00
|
|
|
void memory2() const {
|
|
|
|
const char xmldata1[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <memory>\n"
|
|
|
|
" <alloc>malloc</alloc>\n"
|
|
|
|
" <dealloc>free</dealloc>\n"
|
|
|
|
" </memory>\n"
|
|
|
|
"</def>";
|
|
|
|
const char xmldata2[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <memory>\n"
|
|
|
|
" <alloc>foo</alloc>\n"
|
|
|
|
" <dealloc>free</dealloc>\n"
|
|
|
|
" </memory>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.loadxmldata(xmldata1, sizeof(xmldata1));
|
|
|
|
library.loadxmldata(xmldata2, sizeof(xmldata2));
|
|
|
|
|
|
|
|
ASSERT_EQUALS(library.dealloc("free"), library.alloc("malloc"));
|
|
|
|
ASSERT_EQUALS(library.dealloc("free"), library.alloc("foo"));
|
|
|
|
}
|
2013-11-23 18:16:40 +01:00
|
|
|
|
2014-03-28 18:55:16 +01:00
|
|
|
void resource() const {
|
2013-11-23 18:16:40 +01:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <resource>\n"
|
|
|
|
" <alloc>CreateX</alloc>\n"
|
|
|
|
" <dealloc>DeleteX</dealloc>\n"
|
|
|
|
" </resource>\n"
|
|
|
|
"</def>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
ASSERT(library.use.empty());
|
|
|
|
ASSERT(library.leakignore.empty());
|
|
|
|
ASSERT(library.argumentChecks.empty());
|
|
|
|
|
|
|
|
ASSERT(Library::isresource(library.alloc("CreateX")));
|
|
|
|
ASSERT_EQUALS(library.alloc("CreateX"), library.dealloc("DeleteX"));
|
|
|
|
}
|
2014-06-08 12:09:00 +02:00
|
|
|
|
|
|
|
void podtype() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <podtype name=\"s16\" sizeof=\"2\"/>\n"
|
|
|
|
"</def>";
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata, sizeof(xmldata));
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
library.load(doc);
|
|
|
|
|
|
|
|
const struct Library::PodType *type = library.podtype("s16");
|
|
|
|
ASSERT_EQUALS(2U, type ? type->size : 0U);
|
|
|
|
ASSERT_EQUALS(0, type ? type->sign : '?');
|
|
|
|
}
|
2013-10-27 17:10:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestLibrary)
|