2013-10-27 17:10:43 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2019-02-09 07:24:06 +01:00
|
|
|
* Copyright (C) 2007-2019 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/>.
|
|
|
|
*/
|
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "errorlogger.h"
|
2013-10-27 17:10:43 +01:00
|
|
|
#include "library.h"
|
2016-08-19 10:47:44 +02:00
|
|
|
#include "settings.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "standards.h"
|
|
|
|
#include "testsuite.h"
|
2013-12-23 10:06:45 +01:00
|
|
|
#include "token.h"
|
2016-08-19 10:47:44 +02:00
|
|
|
#include "tokenize.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "tokenlist.h"
|
|
|
|
|
2013-12-31 12:30:17 +01:00
|
|
|
#include <tinyxml2.h>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-10-27 17:10:43 +01:00
|
|
|
|
2015-03-11 22:26:33 +01:00
|
|
|
|
2013-10-27 17:10:43 +01:00
|
|
|
class TestLibrary : public TestFixture {
|
|
|
|
public:
|
|
|
|
TestLibrary() : TestFixture("TestLibrary") { }
|
|
|
|
|
|
|
|
private:
|
2016-12-06 22:11:40 +01:00
|
|
|
Settings settings;
|
2013-10-27 17:10:43 +01:00
|
|
|
|
2019-01-12 15:45:25 +01:00
|
|
|
void run() OVERRIDE {
|
2013-10-27 17:10:43 +01:00
|
|
|
TEST_CASE(empty);
|
|
|
|
TEST_CASE(function);
|
2015-01-31 16:55:55 +01:00
|
|
|
TEST_CASE(function_match_scope);
|
|
|
|
TEST_CASE(function_match_args);
|
2016-07-09 12:42:46 +02:00
|
|
|
TEST_CASE(function_match_args_default);
|
2015-03-01 11:46:43 +01:00
|
|
|
TEST_CASE(function_match_var);
|
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);
|
2017-03-14 17:41:34 +01:00
|
|
|
TEST_CASE(function_arg_variadic);
|
2019-03-04 22:57:40 +01:00
|
|
|
TEST_CASE(function_arg_direction);
|
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);
|
2015-08-09 19:55:33 +02:00
|
|
|
TEST_CASE(function_namespace);
|
2016-08-19 10:47:44 +02:00
|
|
|
TEST_CASE(function_method);
|
2016-09-04 14:14:21 +02:00
|
|
|
TEST_CASE(function_baseClassMethod); // calling method in base class
|
2015-11-21 20:24:30 +01:00
|
|
|
TEST_CASE(function_warn);
|
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
|
2016-05-22 17:18:50 +02:00
|
|
|
TEST_CASE(memory3);
|
2013-11-23 18:16:40 +01:00
|
|
|
TEST_CASE(resource);
|
2014-06-08 12:09:00 +02:00
|
|
|
TEST_CASE(podtype);
|
2015-01-03 20:35:33 +01:00
|
|
|
TEST_CASE(container);
|
2014-09-29 16:25:35 +02:00
|
|
|
TEST_CASE(version);
|
2018-02-17 23:33:24 +01:00
|
|
|
TEST_CASE(loadLibErrors);
|
2013-10-27 17:10:43 +01:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:39:21 +02:00
|
|
|
static Library::Error readLibrary(Library& library, const char* xmldata) {
|
2015-10-18 20:34:38 +02:00
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xmldata);
|
|
|
|
return library.load(doc);
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void empty() const {
|
2018-02-17 23:33:24 +01:00
|
|
|
// Reading an empty library file is considered to be OK
|
2013-10-27 17:10:43 +01:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n<def/>";
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 12:31:16 +01:00
|
|
|
ASSERT(library.functions.empty());
|
2013-10-27 17:10:43 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +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>";
|
|
|
|
|
2015-01-08 19:31:41 +01:00
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("foo();");
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
tokenList.front()->next()->astOperand1(tokenList.front());
|
|
|
|
|
2013-10-27 17:10:43 +01:00
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 14:47:45 +01:00
|
|
|
ASSERT_EQUALS(library.functions.size(), 1U);
|
|
|
|
ASSERT(library.functions.at("foo").argumentChecks.empty());
|
2015-01-08 19:31:41 +01:00
|
|
|
ASSERT(library.isnotnoreturn(tokenList.front()));
|
2013-10-27 17:10:43 +01:00
|
|
|
}
|
|
|
|
|
2015-01-31 16:55:55 +01:00
|
|
|
void function_match_scope() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"foo\">\n"
|
|
|
|
" <arg nr=\"1\"/>"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
2015-10-18 20:34:38 +02:00
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2015-01-31 16:55:55 +01:00
|
|
|
{
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("fred.foo(123);"); // <- wrong scope, not library function
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
|
|
|
|
ASSERT(library.isNotLibraryFunction(tokenList.front()->tokAt(2)));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("Fred::foo(123);"); // <- wrong scope, not library function
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
|
|
|
|
ASSERT(library.isNotLibraryFunction(tokenList.front()->tokAt(2)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void function_match_args() const {
|
2015-01-27 17:55:18 +01:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"foo\">\n"
|
|
|
|
" <arg nr=\"1\"/>"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("foo();"); // <- too few arguments, not library function
|
|
|
|
tokenList.createTokens(istr);
|
2016-07-09 12:42:46 +02:00
|
|
|
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
|
|
|
|
tokenList.createAst();
|
2015-01-27 17:55:18 +01:00
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2015-01-27 17:55:18 +01:00
|
|
|
ASSERT(library.isNotLibraryFunction(tokenList.front()));
|
|
|
|
}
|
|
|
|
|
2016-07-09 12:42:46 +02:00
|
|
|
void function_match_args_default() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"foo\">\n"
|
|
|
|
" <arg nr=\"1\"/>"
|
|
|
|
" <arg nr=\"2\" default=\"0\"/>"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-07-09 12:42:46 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("foo();"); // <- too few arguments, not library function
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
|
|
|
|
tokenList.createAst();
|
|
|
|
|
|
|
|
ASSERT(library.isNotLibraryFunction(tokenList.front()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("foo(a);"); // <- library function
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
|
|
|
|
tokenList.createAst();
|
|
|
|
|
|
|
|
ASSERT(!library.isNotLibraryFunction(tokenList.front()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("foo(a, b);"); // <- library function
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
|
|
|
|
tokenList.createAst();
|
|
|
|
|
|
|
|
ASSERT(!library.isNotLibraryFunction(tokenList.front()));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("foo(a, b, c);"); // <- too much arguments, not library function
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
|
|
|
|
tokenList.createAst();
|
|
|
|
|
|
|
|
ASSERT(library.isNotLibraryFunction(tokenList.front()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-01 11:46:43 +01:00
|
|
|
void function_match_var() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"foo\">\n"
|
|
|
|
" <arg nr=\"1\"/>"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("Fred foo(123);"); // <- Variable declaration, not library function
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
tokenList.front()->next()->astOperand1(tokenList.front());
|
|
|
|
tokenList.front()->next()->varId(1);
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2015-03-01 11:46:43 +01:00
|
|
|
ASSERT(library.isNotLibraryFunction(tokenList.front()->next()));
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +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"
|
2016-07-09 12:42:46 +02:00
|
|
|
" <arg nr=\"5\" default=\"0\"><not-bool/></arg>\n"
|
2013-11-21 16:32:53 +01:00
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 12:31:16 +01:00
|
|
|
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[1].notuninit);
|
|
|
|
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[2].notnull);
|
|
|
|
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[3].formatstr);
|
|
|
|
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[4].strz);
|
|
|
|
ASSERT_EQUALS(false, library.functions["foo"].argumentChecks[4].optional);
|
|
|
|
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[5].notbool);
|
|
|
|
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[5].optional);
|
2013-11-21 16:32:53 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +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>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 12:31:16 +01:00
|
|
|
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[-1].notuninit);
|
2014-03-06 06:16:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 17:41:34 +01:00
|
|
|
void function_arg_variadic() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
"<function name=\"foo\">\n"
|
|
|
|
" <arg nr=\"1\"></arg>\n"
|
|
|
|
" <arg nr=\"variadic\"><not-uninit/></arg>\n"
|
|
|
|
"</function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2017-03-14 17:41:34 +01:00
|
|
|
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[-1].notuninit);
|
|
|
|
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("foo(a,b,c,d,e);");
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
tokenList.front()->next()->astOperand1(tokenList.front());
|
|
|
|
|
|
|
|
ASSERT_EQUALS(false, library.isuninitargbad(tokenList.front(), 1));
|
|
|
|
ASSERT_EQUALS(true, library.isuninitargbad(tokenList.front(), 2));
|
|
|
|
ASSERT_EQUALS(true, library.isuninitargbad(tokenList.front(), 3));
|
|
|
|
ASSERT_EQUALS(true, library.isuninitargbad(tokenList.front(), 4));
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:57:40 +01:00
|
|
|
void function_arg_direction() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
"<function name=\"foo\">\n"
|
|
|
|
" <arg nr=\"1\" direction=\"in\"></arg>\n"
|
|
|
|
" <arg nr=\"2\" direction=\"out\"></arg>\n"
|
|
|
|
" <arg nr=\"3\" direction=\"inout\"></arg>\n"
|
|
|
|
" <arg nr=\"4\"></arg>\n"
|
|
|
|
"</function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
|
|
|
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("foo(a,b,c,d);");
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
tokenList.front()->next()->astOperand1(tokenList.front());
|
|
|
|
|
|
|
|
ASSERT_EQUALS(Library::ArgumentChecks::Direction::DIR_IN, library.getArgDirection(tokenList.front(), 1));
|
|
|
|
ASSERT_EQUALS(Library::ArgumentChecks::Direction::DIR_OUT, library.getArgDirection(tokenList.front(), 2));
|
|
|
|
ASSERT_EQUALS(Library::ArgumentChecks::Direction::DIR_INOUT, library.getArgDirection(tokenList.front(), 3));
|
|
|
|
ASSERT_EQUALS(Library::ArgumentChecks::Direction::DIR_UNKNOWN, library.getArgDirection(tokenList.front(), 4));
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void function_arg_valid() const {
|
2014-06-07 16:28:29 +02:00
|
|
|
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"
|
2018-07-15 22:47:56 +02:00
|
|
|
" <arg nr=\"6\"><valid>1.5:</valid></arg>\n"
|
|
|
|
" <arg nr=\"7\"><valid>-6.7:-5.5,-3.3:-2.7</valid></arg>\n"
|
|
|
|
" <arg nr=\"8\"><valid>0.0:</valid></arg>\n"
|
|
|
|
" <arg nr=\"9\"><valid>:2.0</valid></arg>\n"
|
|
|
|
" <arg nr=\"10\"><valid>0.0</valid></arg>\n"
|
2014-06-07 16:28:29 +02:00
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2014-06-07 16:28:29 +02:00
|
|
|
|
2015-01-08 19:31:41 +01:00
|
|
|
TokenList tokenList(nullptr);
|
2018-07-15 22:47:56 +02:00
|
|
|
std::istringstream istr("foo(a,b,c,d,e,f,g,h,i,j);");
|
2015-01-08 19:31:41 +01:00
|
|
|
tokenList.createTokens(istr);
|
|
|
|
tokenList.front()->next()->astOperand1(tokenList.front());
|
|
|
|
|
2014-06-07 16:28:29 +02:00
|
|
|
// 1-
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 1, -10));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 1, -10.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 1, 0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 1, 0.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 1, 1));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 1, 1.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 1, 10));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 1, 10.0));
|
2014-06-07 16:28:29 +02:00
|
|
|
|
|
|
|
// -7-0
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 2, -10));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, -10.0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, -7.5));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, -7.1));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 2, -7));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 2, -7.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 2, -3));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 2, -3.0));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 2, -3.5));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 2, 0));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 2, 0.0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, 0.5));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 2, 1));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, 1.0));
|
2014-06-07 16:28:29 +02:00
|
|
|
|
|
|
|
// 1-5,8
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 3, 0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 0.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 3, 1));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 3, 1.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 3, 3));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 3, 3.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 3, 5));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 3, 5.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 3, 6));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 6.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 3, 7));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 7.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 3, 8));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 8.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 3, 9));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 9.0));
|
2014-06-08 18:12:11 +02:00
|
|
|
|
|
|
|
// -1,5
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 4, -10));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 4, -10.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 4, -1));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 4, -1.0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 4, 5.000001));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 4, 5.5));
|
2014-06-08 18:12:11 +02:00
|
|
|
|
|
|
|
// :1,5
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 5, -10));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 5, -10.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 5, 1));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 5, 1.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 5, 2));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 5, 2.0));
|
2018-07-15 22:47:56 +02:00
|
|
|
|
|
|
|
// 1.5:
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 6, 0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 6, 0.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 6, 1));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 6, 1.499999));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 6, 1.5));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 6, 2));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 6, 10));
|
2018-07-15 22:47:56 +02:00
|
|
|
|
|
|
|
// -6.7:-5.5,-3.3:-2.7
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, -7));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -7.0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -6.7000001));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -6.7));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 7, -6));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -6.0));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -5.5));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -5.4999999));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -3.3000001));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -3.3));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 7, -3));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -3.0));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -2.7));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -2.6999999));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, -2));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -2.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, 0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, 0.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, 3));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, 3.0));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, 6));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, 6.0));
|
2018-07-15 22:47:56 +02:00
|
|
|
|
|
|
|
// 0.0:
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 8, -1));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 8, -1.0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 8, -0.00000001));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 8, 0));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 8, 0.0));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 8, 0.000000001));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 8, 1));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 8, 1.0));
|
2018-07-15 22:47:56 +02:00
|
|
|
|
|
|
|
// :2.0
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 9, -1));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 9, -1.0));
|
|
|
|
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 9, 2));
|
|
|
|
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 9, 2.0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 9, 2.00000001));
|
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 9, 200));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 9, 200.0));
|
2018-07-15 22:47:56 +02:00
|
|
|
|
|
|
|
// 0.0
|
2018-07-15 23:05:48 +02:00
|
|
|
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 10, 0));
|
|
|
|
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 10, 0.0));
|
2014-06-07 16:28:29 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void function_arg_minsize() const {
|
2014-07-05 20:31:43 +02:00
|
|
|
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"
|
2015-01-27 17:55:18 +01:00
|
|
|
" <arg nr=\"3\"/>\n"
|
2019-03-17 14:22:26 +01:00
|
|
|
" <arg nr=\"4\"><minsize type=\"value\" value=\"500\"/></arg>\n"
|
2014-07-05 20:31:43 +02:00
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2014-07-05 20:31:43 +02:00
|
|
|
|
2015-01-08 19:31:41 +01:00
|
|
|
TokenList tokenList(nullptr);
|
2019-03-17 14:22:26 +01:00
|
|
|
std::istringstream istr("foo(a,b,c,d);");
|
2015-01-08 19:31:41 +01:00
|
|
|
tokenList.createTokens(istr);
|
|
|
|
tokenList.front()->next()->astOperand1(tokenList.front());
|
|
|
|
|
2014-07-05 20:31:43 +02:00
|
|
|
// arg1: type=strlen arg2
|
2017-03-27 17:33:26 +02:00
|
|
|
const std::vector<Library::ArgumentChecks::MinSize> *minsizes = library.argminsizes(tokenList.front(),1);
|
2014-07-05 20:31:43 +02:00
|
|
|
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
|
2015-01-08 19:31:41 +01:00
|
|
|
minsizes = library.argminsizes(tokenList.front(), 2);
|
2014-07-05 20:31:43 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-03-17 14:22:26 +01:00
|
|
|
|
|
|
|
// arg4: type=value
|
|
|
|
minsizes = library.argminsizes(tokenList.front(), 4);
|
|
|
|
ASSERT_EQUALS(true, minsizes != nullptr);
|
|
|
|
ASSERT_EQUALS(1U, minsizes ? minsizes->size() : 1U);
|
|
|
|
if (minsizes && minsizes->size() == 1U) {
|
|
|
|
const Library::ArgumentChecks::MinSize &m = minsizes->front();
|
|
|
|
ASSERT_EQUALS(Library::ArgumentChecks::MinSize::VALUE, m.type);
|
|
|
|
ASSERT_EQUALS(500, m.value);
|
|
|
|
}
|
2014-07-05 20:31:43 +02:00
|
|
|
}
|
|
|
|
|
2015-08-09 19:55:33 +02:00
|
|
|
void function_namespace() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
2015-08-09 21:27:57 +02:00
|
|
|
" <function name=\"Foo::foo,bar\">\n"
|
2015-08-09 19:55:33 +02:00
|
|
|
" <noreturn>false</noreturn>\n"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 14:47:45 +01:00
|
|
|
ASSERT_EQUALS(library.functions.size(), 2U);
|
|
|
|
ASSERT(library.functions.at("Foo::foo").argumentChecks.empty());
|
|
|
|
ASSERT(library.functions.at("bar").argumentChecks.empty());
|
2015-08-09 21:27:57 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("Foo::foo();");
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
ASSERT(library.isnotnoreturn(tokenList.front()->tokAt(2)));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("bar();");
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
ASSERT(library.isnotnoreturn(tokenList.front()));
|
|
|
|
}
|
2015-08-09 19:55:33 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 10:47:44 +02:00
|
|
|
void function_method() const {
|
2016-08-19 17:34:30 +02:00
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"CString::Format\">\n"
|
|
|
|
" <noreturn>false</noreturn>\n"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 14:47:45 +01:00
|
|
|
ASSERT_EQUALS(library.functions.size(), 1U);
|
2016-08-19 17:34:30 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Tokenizer tokenizer(&settings, nullptr);
|
|
|
|
std::istringstream istr("CString str; str.Format();");
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
ASSERT(library.isnotnoreturn(Token::findsimplematch(tokenizer.tokens(), "Format")));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Tokenizer tokenizer(&settings, nullptr);
|
|
|
|
std::istringstream istr("HardDrive hd; hd.Format();");
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
ASSERT(!library.isnotnoreturn(Token::findsimplematch(tokenizer.tokens(), "Format")));
|
|
|
|
}
|
2016-08-19 10:47:44 +02:00
|
|
|
}
|
|
|
|
|
2016-09-04 14:14:21 +02:00
|
|
|
void function_baseClassMethod() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"Base::f\">\n"
|
|
|
|
" <arg nr=\"1\"><not-null/></arg>\n"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-09-04 14:14:21 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Tokenizer tokenizer(&settings, nullptr);
|
|
|
|
std::istringstream istr("struct X : public Base { void dostuff() { f(0); } };");
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
ASSERT(library.isnullargbad(Token::findsimplematch(tokenizer.tokens(), "f"),1));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Tokenizer tokenizer(&settings, nullptr);
|
|
|
|
std::istringstream istr("struct X : public Base { void dostuff() { f(1,2); } };");
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
ASSERT(!library.isnullargbad(Token::findsimplematch(tokenizer.tokens(), "f"),1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 20:24:30 +01:00
|
|
|
void function_warn() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"a\">\n"
|
|
|
|
" <warn severity=\"style\" cstd=\"c99\">Message</warn>\n"
|
|
|
|
" </function>\n"
|
|
|
|
" <function name=\"b\">\n"
|
|
|
|
" <warn severity=\"performance\" cppstd=\"c++11\" reason=\"Obsolescent\" alternatives=\"c,d,e\"/>\n"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2015-11-21 20:24:30 +01:00
|
|
|
|
|
|
|
TokenList tokenList(nullptr);
|
|
|
|
std::istringstream istr("a(); b();");
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
|
|
|
|
const Library::WarnInfo* a = library.getWarnInfo(tokenList.front());
|
|
|
|
const Library::WarnInfo* b = library.getWarnInfo(tokenList.front()->tokAt(4));
|
|
|
|
|
|
|
|
ASSERT_EQUALS(2, library.functionwarn.size());
|
|
|
|
ASSERT(a && b);
|
|
|
|
if (a && b) {
|
|
|
|
ASSERT_EQUALS("Message", a->message);
|
|
|
|
ASSERT_EQUALS(Severity::style, a->severity);
|
|
|
|
ASSERT_EQUALS(Standards::C99, a->standards.c);
|
|
|
|
ASSERT_EQUALS(Standards::CPP03, a->standards.cpp);
|
|
|
|
|
|
|
|
ASSERT_EQUALS("Obsolescent function 'b' called. It is recommended to use 'c', 'd' or 'e' instead.", b->message);
|
|
|
|
ASSERT_EQUALS(Severity::performance, b->severity);
|
|
|
|
ASSERT_EQUALS(Standards::C89, b->standards.c);
|
|
|
|
ASSERT_EQUALS(Standards::CPP11, b->standards.cpp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +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>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 12:31:16 +01:00
|
|
|
ASSERT(library.functions.empty());
|
2013-10-27 17:10:43 +01:00
|
|
|
|
|
|
|
ASSERT(Library::ismemory(library.alloc("CreateX")));
|
2016-05-22 17:18:50 +02:00
|
|
|
ASSERT_EQUALS(library.allocId("CreateX"), library.deallocId("DeleteX"));
|
|
|
|
const Library::AllocFunc* af = library.alloc("CreateX");
|
|
|
|
ASSERT(af && af->arg == -1);
|
|
|
|
const Library::AllocFunc* df = library.dealloc("DeleteX");
|
|
|
|
ASSERT(df && df->arg == 1);
|
2013-10-27 17:10:43 +01:00
|
|
|
}
|
2014-11-20 14:20:09 +01:00
|
|
|
void memory2() const {
|
2014-04-19 13:15:06 +02:00
|
|
|
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));
|
|
|
|
|
2016-05-22 17:18:50 +02:00
|
|
|
ASSERT_EQUALS(library.deallocId("free"), library.allocId("malloc"));
|
|
|
|
ASSERT_EQUALS(library.deallocId("free"), library.allocId("foo"));
|
|
|
|
}
|
|
|
|
void memory3() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <memory>\n"
|
|
|
|
" <alloc arg=\"5\" init=\"false\">CreateX</alloc>\n"
|
|
|
|
" <dealloc arg=\"2\">DeleteX</dealloc>\n"
|
|
|
|
" </memory>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 12:31:16 +01:00
|
|
|
ASSERT(library.functions.empty());
|
2016-05-22 17:18:50 +02:00
|
|
|
|
|
|
|
const Library::AllocFunc* af = library.alloc("CreateX");
|
|
|
|
ASSERT(af && af->arg == 5);
|
|
|
|
const Library::AllocFunc* df = library.dealloc("DeleteX");
|
|
|
|
ASSERT(df && df->arg == 2);
|
|
|
|
|
|
|
|
ASSERT(library.returnuninitdata.find("CreateX") != library.returnuninitdata.cend());
|
2014-04-19 13:15:06 +02:00
|
|
|
}
|
2013-11-23 18:16:40 +01:00
|
|
|
|
2014-11-20 14:20:09 +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>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2016-12-06 12:31:16 +01:00
|
|
|
ASSERT(library.functions.empty());
|
2013-11-23 18:16:40 +01:00
|
|
|
|
2016-05-22 17:18:50 +02:00
|
|
|
ASSERT(Library::isresource(library.allocId("CreateX")));
|
|
|
|
ASSERT_EQUALS(library.allocId("CreateX"), library.deallocId("DeleteX"));
|
2013-11-23 18:16:40 +01:00
|
|
|
}
|
2014-06-08 12:09:00 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void podtype() const {
|
2018-02-17 23:33:24 +01:00
|
|
|
{
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <podtype name=\"s8\" sign=\"s\" size=\"1\"/>\n"
|
|
|
|
" <podtype name=\"u8\" sign=\"u\" size=\"1\"/>\n"
|
|
|
|
" <podtype name=\"u16\" sign=\"u\" size=\"2\"/>\n"
|
|
|
|
" <podtype name=\"s16\" sign=\"s\" size=\"2\"/>\n"
|
|
|
|
"</def>";
|
|
|
|
Library library;
|
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
|
|
|
// s8
|
|
|
|
{
|
|
|
|
const struct Library::PodType * const type = library.podtype("s8");
|
2019-07-02 20:37:44 +02:00
|
|
|
ASSERT_EQUALS(true, type != nullptr);
|
2018-02-17 23:33:24 +01:00
|
|
|
if (type) {
|
|
|
|
ASSERT_EQUALS(1U, type->size);
|
|
|
|
ASSERT_EQUALS('s', type->sign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// u8
|
|
|
|
{
|
|
|
|
const struct Library::PodType * const type = library.podtype("u8");
|
2019-07-02 20:37:44 +02:00
|
|
|
ASSERT_EQUALS(true, type != nullptr);
|
2018-02-17 23:33:24 +01:00
|
|
|
if (type) {
|
|
|
|
ASSERT_EQUALS(1U, type->size);
|
|
|
|
ASSERT_EQUALS('u', type->sign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// u16
|
|
|
|
{
|
|
|
|
const struct Library::PodType * const type = library.podtype("u16");
|
2019-07-02 20:37:44 +02:00
|
|
|
ASSERT_EQUALS(true, type != nullptr);
|
2018-02-17 23:33:24 +01:00
|
|
|
if (type) {
|
|
|
|
ASSERT_EQUALS(2U, type->size);
|
|
|
|
ASSERT_EQUALS('u', type->sign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// s16
|
|
|
|
{
|
|
|
|
const struct Library::PodType * const type = library.podtype("s16");
|
2019-07-02 20:37:44 +02:00
|
|
|
ASSERT_EQUALS(true, type != nullptr);
|
2018-02-17 23:33:24 +01:00
|
|
|
if (type) {
|
|
|
|
ASSERT_EQUALS(2U, type->size);
|
|
|
|
ASSERT_EQUALS('s', type->sign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// robustness test: provide cfg without PodType
|
|
|
|
{
|
|
|
|
const struct Library::PodType * const type = library.podtype("nonExistingPodType");
|
2019-07-02 20:37:44 +02:00
|
|
|
ASSERT_EQUALS(true, type == nullptr);
|
2018-02-17 23:33:24 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 12:09:00 +02:00
|
|
|
}
|
2014-09-29 16:25:35 +02:00
|
|
|
|
2015-01-03 20:35:33 +01:00
|
|
|
void container() const {
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
2015-11-20 12:46:59 +01:00
|
|
|
" <container id=\"A\" startPattern=\"std :: A <\" endPattern=\"> !!::\" itEndPattern=\"> :: iterator\">\n"
|
2015-01-03 20:35:33 +01:00
|
|
|
" <type templateParameter=\"1\"/>\n"
|
|
|
|
" <size templateParameter=\"4\">\n"
|
|
|
|
" <function name=\"resize\" action=\"resize\"/>\n"
|
|
|
|
" <function name=\"clear\" action=\"clear\"/>\n"
|
|
|
|
" <function name=\"size\" yields=\"size\"/>\n"
|
|
|
|
" <function name=\"empty\" yields=\"empty\"/>\n"
|
|
|
|
" <function name=\"push_back\" action=\"push\"/>\n"
|
|
|
|
" <function name=\"pop_back\" action=\"pop\"/>\n"
|
|
|
|
" </size>\n"
|
|
|
|
" <access>\n"
|
|
|
|
" <function name=\"at\" yields=\"at_index\"/>\n"
|
|
|
|
" <function name=\"begin\" yields=\"start-iterator\"/>\n"
|
|
|
|
" <function name=\"end\" yields=\"end-iterator\"/>\n"
|
|
|
|
" <function name=\"data\" yields=\"buffer\"/>\n"
|
|
|
|
" <function name=\"c_str\" yields=\"buffer-nt\"/>\n"
|
|
|
|
" <function name=\"front\" yields=\"item\"/>\n"
|
2015-01-04 12:43:31 +01:00
|
|
|
" <function name=\"find\" action=\"find\"/>\n"
|
2015-01-03 20:35:33 +01:00
|
|
|
" </access>\n"
|
|
|
|
" </container>\n"
|
2015-11-20 12:46:59 +01:00
|
|
|
" <container id=\"B\" startPattern=\"std :: B <\" inherits=\"A\" opLessAllowed=\"false\">\n"
|
2015-01-03 20:35:33 +01:00
|
|
|
" <size templateParameter=\"3\"/>\n" // Inherits all but templateParameter
|
|
|
|
" </container>\n"
|
|
|
|
" <container id=\"C\">\n"
|
|
|
|
" <type string=\"std-like\"/>\n"
|
|
|
|
" <access indexOperator=\"array-like\"/>\n"
|
|
|
|
" </container>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
ASSERT_EQUALS(true, Library::OK == (readLibrary(library, xmldata)).errorcode);
|
2015-01-03 20:35:33 +01:00
|
|
|
|
|
|
|
Library::Container& A = library.containers["A"];
|
|
|
|
Library::Container& B = library.containers["B"];
|
|
|
|
Library::Container& C = library.containers["C"];
|
|
|
|
|
|
|
|
ASSERT_EQUALS(A.type_templateArgNo, 1);
|
|
|
|
ASSERT_EQUALS(A.size_templateArgNo, 4);
|
|
|
|
ASSERT_EQUALS(A.startPattern, "std :: A <");
|
|
|
|
ASSERT_EQUALS(A.endPattern, "> !!::");
|
2015-11-20 12:46:59 +01:00
|
|
|
ASSERT_EQUALS(A.itEndPattern, "> :: iterator");
|
2015-01-03 20:35:33 +01:00
|
|
|
ASSERT_EQUALS(A.stdStringLike, false);
|
|
|
|
ASSERT_EQUALS(A.arrayLike_indexOp, false);
|
2015-11-20 12:46:59 +01:00
|
|
|
ASSERT_EQUALS(A.opLessAllowed, true);
|
2015-01-03 20:35:33 +01:00
|
|
|
ASSERT_EQUALS(Library::Container::SIZE, A.getYield("size"));
|
|
|
|
ASSERT_EQUALS(Library::Container::EMPTY, A.getYield("empty"));
|
|
|
|
ASSERT_EQUALS(Library::Container::AT_INDEX, A.getYield("at"));
|
|
|
|
ASSERT_EQUALS(Library::Container::START_ITERATOR, A.getYield("begin"));
|
|
|
|
ASSERT_EQUALS(Library::Container::END_ITERATOR, A.getYield("end"));
|
|
|
|
ASSERT_EQUALS(Library::Container::BUFFER, A.getYield("data"));
|
|
|
|
ASSERT_EQUALS(Library::Container::BUFFER_NT, A.getYield("c_str"));
|
|
|
|
ASSERT_EQUALS(Library::Container::ITEM, A.getYield("front"));
|
|
|
|
ASSERT_EQUALS(Library::Container::NO_YIELD, A.getYield("foo"));
|
|
|
|
ASSERT_EQUALS(Library::Container::RESIZE, A.getAction("resize"));
|
|
|
|
ASSERT_EQUALS(Library::Container::CLEAR, A.getAction("clear"));
|
|
|
|
ASSERT_EQUALS(Library::Container::PUSH, A.getAction("push_back"));
|
|
|
|
ASSERT_EQUALS(Library::Container::POP, A.getAction("pop_back"));
|
2015-01-04 12:43:31 +01:00
|
|
|
ASSERT_EQUALS(Library::Container::FIND, A.getAction("find"));
|
2015-01-03 20:35:33 +01:00
|
|
|
ASSERT_EQUALS(Library::Container::NO_ACTION, A.getAction("foo"));
|
|
|
|
|
|
|
|
ASSERT_EQUALS(B.type_templateArgNo, 1);
|
|
|
|
ASSERT_EQUALS(B.size_templateArgNo, 3);
|
|
|
|
ASSERT_EQUALS(B.startPattern, "std :: B <");
|
|
|
|
ASSERT_EQUALS(B.endPattern, "> !!::");
|
2015-11-20 12:46:59 +01:00
|
|
|
ASSERT_EQUALS(B.itEndPattern, "> :: iterator");
|
2015-01-03 20:35:33 +01:00
|
|
|
ASSERT_EQUALS(B.functions.size(), A.functions.size());
|
2015-11-20 12:46:59 +01:00
|
|
|
ASSERT_EQUALS(B.opLessAllowed, false);
|
2015-01-03 20:35:33 +01:00
|
|
|
|
|
|
|
ASSERT(C.functions.empty());
|
|
|
|
ASSERT_EQUALS(C.type_templateArgNo, -1);
|
|
|
|
ASSERT_EQUALS(C.size_templateArgNo, -1);
|
|
|
|
ASSERT_EQUALS(C.stdStringLike, true);
|
|
|
|
ASSERT_EQUALS(C.arrayLike_indexOp, true);
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void version() const {
|
2014-09-29 16:25:35 +02:00
|
|
|
{
|
|
|
|
const char xmldata [] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
"</def>";
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
const Library::Error err = readLibrary(library, xmldata);
|
2014-09-29 16:25:35 +02:00
|
|
|
ASSERT_EQUALS(err.errorcode, Library::OK);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char xmldata [] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def format=\"1\">\n"
|
|
|
|
"</def>";
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
const Library::Error err = readLibrary(library, xmldata);
|
2014-09-29 16:25:35 +02:00
|
|
|
ASSERT_EQUALS(err.errorcode, Library::OK);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const char xmldata [] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def format=\"42\">\n"
|
|
|
|
"</def>";
|
|
|
|
Library library;
|
2018-02-17 23:33:24 +01:00
|
|
|
const Library::Error err = readLibrary(library, xmldata);
|
2014-09-29 16:25:35 +02:00
|
|
|
ASSERT_EQUALS(err.errorcode, Library::UNSUPPORTED_FORMAT);
|
|
|
|
}
|
|
|
|
}
|
2018-02-17 23:33:24 +01:00
|
|
|
|
2018-07-15 22:47:56 +02:00
|
|
|
void loadLibError(const char xmldata [], Library::ErrorCode errorcode, const char* file, unsigned line) const {
|
|
|
|
Library library;
|
|
|
|
assertEquals(file, line, errorcode, readLibrary(library, xmldata).errorcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOADLIBERROR(xmldata, errorcode) loadLibError(xmldata, errorcode, __FILE__, __LINE__)
|
|
|
|
#define LOADLIB_ERROR_INVALID_RANGE(valid) LOADLIBERROR("<?xml version=\"1.0\"?>\n" \
|
|
|
|
"<def>\n" \
|
|
|
|
"<function name=\"f\">\n" \
|
|
|
|
"<arg nr=\"1\">\n" \
|
|
|
|
"<valid>" valid "</valid>\n" \
|
|
|
|
"</arg>\n" \
|
|
|
|
"</function>\n" \
|
|
|
|
"</def>", \
|
|
|
|
Library::BAD_ATTRIBUTE_VALUE)
|
|
|
|
|
2018-02-17 23:33:24 +01:00
|
|
|
void loadLibErrors() const {
|
2018-07-15 22:47:56 +02:00
|
|
|
|
|
|
|
LOADLIBERROR("<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <X name=\"uint8_t,std::uint8_t\" size=\"1\"/>\n"
|
|
|
|
"</def>",
|
|
|
|
Library::UNKNOWN_ELEMENT);
|
|
|
|
|
|
|
|
// #define without attributes
|
|
|
|
LOADLIBERROR("<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <define />\n" // no attributes provided at all
|
|
|
|
"</def>",
|
|
|
|
Library::MISSING_ATTRIBUTE);
|
|
|
|
|
|
|
|
// #define with name but without value
|
|
|
|
LOADLIBERROR("<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <define name=\"foo\" />\n" // no value provided
|
|
|
|
"</def>",
|
|
|
|
Library::MISSING_ATTRIBUTE);
|
|
|
|
|
|
|
|
LOADLIBERROR("<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <define value=\"1\" />\n" // no name provided
|
|
|
|
"</def>",
|
|
|
|
Library::MISSING_ATTRIBUTE);
|
|
|
|
|
|
|
|
LOADLIBERROR("<?xml version=\"1.0\"?>\n"
|
|
|
|
"<X>\n"
|
|
|
|
"</X>",
|
|
|
|
Library::UNSUPPORTED_FORMAT);
|
|
|
|
|
|
|
|
// empty range
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("");
|
|
|
|
|
|
|
|
// letter as range
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("a");
|
|
|
|
|
|
|
|
// letter and number as range
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("1a");
|
|
|
|
|
|
|
|
// digit followed by dash
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("0:2-1");
|
|
|
|
|
|
|
|
// single dash
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("-");
|
|
|
|
|
|
|
|
// range with multiple colons
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("1:2:3");
|
|
|
|
|
|
|
|
// extra dot
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("1.0.0:10");
|
|
|
|
|
|
|
|
// consecutive dots
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("1..0:10");
|
|
|
|
|
|
|
|
// dot followed by dash
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("1.-0:10");
|
|
|
|
|
|
|
|
// dot without preceding number
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE(".5:10");
|
|
|
|
|
|
|
|
// dash followed by dot
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("-.5:10");
|
|
|
|
|
|
|
|
// colon followed by dot without preceding number
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("0:.5");
|
|
|
|
|
|
|
|
// colon followed by dash followed by dot
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("-10:-.5");
|
|
|
|
|
|
|
|
// dot not followed by number
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("1:5.");
|
|
|
|
|
|
|
|
// dot not followed by number
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("1.:5");
|
|
|
|
|
|
|
|
// dot followed by comma
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("1:5.,6:10");
|
|
|
|
|
|
|
|
// comma followed by dot
|
|
|
|
LOADLIB_ERROR_INVALID_RANGE("-10:0,.5:");
|
2018-02-17 23:33:24 +01:00
|
|
|
}
|
2013-10-27 17:10:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestLibrary)
|