2013-07-02 07:18:19 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2015-01-03 12:14:58 +01:00
|
|
|
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
|
2013-07-02 07:18:19 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
2013-07-08 18:26:18 +02:00
|
|
|
#include "library.h"
|
2013-07-20 17:12:56 +02:00
|
|
|
#include "path.h"
|
2013-07-07 11:33:25 +02:00
|
|
|
#include "tinyxml2.h"
|
2013-12-23 10:06:45 +01:00
|
|
|
#include "tokenlist.h"
|
|
|
|
#include "mathlib.h"
|
|
|
|
#include "token.h"
|
2014-12-27 11:07:36 +01:00
|
|
|
#include "symboldatabase.h"
|
2013-07-02 07:18:19 +02:00
|
|
|
|
2013-07-20 17:12:56 +02:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2013-07-08 18:26:18 +02:00
|
|
|
Library::Library() : allocid(0)
|
2013-07-02 07:18:19 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-26 17:00:41 +02:00
|
|
|
Library::Error Library::load(const char exename[], const char path[])
|
2013-07-02 07:18:19 +02:00
|
|
|
{
|
2014-02-15 08:41:17 +01:00
|
|
|
if (std::strchr(path,',') != nullptr) {
|
2013-07-20 10:21:05 +02:00
|
|
|
std::string p(path);
|
|
|
|
while (p.find(",") != std::string::npos) {
|
|
|
|
const std::string::size_type pos = p.find(",");
|
2014-05-26 17:00:41 +02:00
|
|
|
const Error &e = load(exename, p.substr(0,pos).c_str());
|
2014-06-04 19:18:27 +02:00
|
|
|
if (e.errorcode != OK)
|
2014-05-26 17:00:41 +02:00
|
|
|
return e;
|
2013-07-20 10:21:05 +02:00
|
|
|
p = p.substr(pos+1);
|
|
|
|
}
|
|
|
|
if (!p.empty())
|
2014-05-26 17:00:41 +02:00
|
|
|
return load(exename, p.c_str());
|
|
|
|
return Error();
|
2013-07-20 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2014-10-19 07:34:40 +02:00
|
|
|
std::string absolute_path;
|
2013-07-20 17:12:56 +02:00
|
|
|
// open file..
|
2013-10-27 17:32:38 +01:00
|
|
|
tinyxml2::XMLDocument doc;
|
2014-03-01 14:09:03 +01:00
|
|
|
tinyxml2::XMLError error = doc.LoadFile(path);
|
2013-10-27 17:32:38 +01:00
|
|
|
if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) {
|
2013-07-20 17:12:56 +02:00
|
|
|
// failed to open file.. is there no extension?
|
|
|
|
std::string fullfilename(path);
|
|
|
|
if (Path::getFilenameExtension(fullfilename) == "") {
|
|
|
|
fullfilename += ".cfg";
|
2014-03-01 14:09:03 +01:00
|
|
|
error = doc.LoadFile(fullfilename.c_str());
|
2014-10-19 07:34:40 +02:00
|
|
|
if (error != tinyxml2::XML_ERROR_FILE_NOT_FOUND)
|
|
|
|
absolute_path = Path::getAbsoluteFilePath(fullfilename.c_str());
|
2013-07-20 10:21:05 +02:00
|
|
|
}
|
2013-07-20 17:12:56 +02:00
|
|
|
|
2013-10-27 17:32:38 +01:00
|
|
|
if (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND) {
|
2013-07-20 17:12:56 +02:00
|
|
|
// Try to locate the library configuration in the installation folder..
|
2013-12-26 18:41:51 +01:00
|
|
|
#ifdef CFGDIR
|
|
|
|
const std::string cfgfolder(CFGDIR);
|
|
|
|
#else
|
2013-12-30 18:35:27 +01:00
|
|
|
if (!exename)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(FILE_NOT_FOUND);
|
2013-12-26 18:41:51 +01:00
|
|
|
const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(exename)) + "cfg");
|
|
|
|
#endif
|
|
|
|
const char *sep = (!cfgfolder.empty() && cfgfolder[cfgfolder.size()-1U]=='/' ? "" : "/");
|
|
|
|
const std::string filename(cfgfolder + sep + fullfilename);
|
2014-03-01 14:09:03 +01:00
|
|
|
error = doc.LoadFile(filename.c_str());
|
2014-10-19 07:34:40 +02:00
|
|
|
if (error != tinyxml2::XML_ERROR_FILE_NOT_FOUND)
|
|
|
|
absolute_path = Path::getAbsoluteFilePath(filename.c_str());
|
2013-07-20 17:12:56 +02:00
|
|
|
}
|
2014-10-19 07:34:40 +02:00
|
|
|
} else
|
|
|
|
absolute_path = Path::getAbsoluteFilePath(path);
|
|
|
|
|
|
|
|
if (error == tinyxml2::XML_NO_ERROR) {
|
|
|
|
if (_files.find(absolute_path) == _files.end()) {
|
|
|
|
Error err = load(doc);
|
|
|
|
if (err.errorcode == OK)
|
|
|
|
_files.insert(absolute_path);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error(OK); // ignore duplicates
|
2013-07-20 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2014-10-19 07:34:40 +02:00
|
|
|
return Error(error == tinyxml2::XML_ERROR_FILE_NOT_FOUND ? FILE_NOT_FOUND : BAD_XML);
|
2013-10-27 17:10:43 +01:00
|
|
|
}
|
|
|
|
|
2014-01-09 21:58:56 +01:00
|
|
|
bool Library::loadxmldata(const char xmldata[], std::size_t len)
|
|
|
|
{
|
|
|
|
tinyxml2::XMLDocument doc;
|
2014-06-04 19:18:27 +02:00
|
|
|
return (tinyxml2::XML_NO_ERROR == doc.Parse(xmldata, len)) && (load(doc).errorcode == OK);
|
2014-01-09 21:58:56 +01:00
|
|
|
}
|
|
|
|
|
2014-05-26 17:00:41 +02:00
|
|
|
Library::Error Library::load(const tinyxml2::XMLDocument &doc)
|
2013-10-27 17:10:43 +01:00
|
|
|
{
|
2013-07-02 07:18:19 +02:00
|
|
|
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
|
2013-10-31 19:49:36 +01:00
|
|
|
|
2014-02-15 08:41:17 +01:00
|
|
|
if (rootnode == nullptr)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(BAD_XML);
|
2013-10-31 19:49:36 +01:00
|
|
|
|
|
|
|
if (strcmp(rootnode->Name(),"def") != 0)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(BAD_ELEMENT, rootnode->Name());
|
2013-07-02 07:18:19 +02:00
|
|
|
|
2014-09-29 16:25:35 +02:00
|
|
|
const char* format_string = rootnode->Attribute("format");
|
|
|
|
int format = 1;
|
|
|
|
if (format_string)
|
|
|
|
format = atoi(format_string);
|
|
|
|
|
|
|
|
if (format > 1)
|
|
|
|
return Error(UNSUPPORTED_FORMAT);
|
|
|
|
|
2013-07-02 07:18:19 +02:00
|
|
|
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
2014-12-12 22:16:20 +01:00
|
|
|
const std::string nodename = node->Name();
|
2014-09-10 17:03:31 +02:00
|
|
|
if (nodename == "memory" || nodename == "resource") {
|
2014-04-19 13:15:06 +02:00
|
|
|
// get allocationId to use..
|
|
|
|
int allocationId = 0;
|
|
|
|
for (const tinyxml2::XMLElement *memorynode = node->FirstChildElement(); memorynode; memorynode = memorynode->NextSiblingElement()) {
|
|
|
|
if (strcmp(memorynode->Name(),"dealloc")==0) {
|
|
|
|
const std::map<std::string,int>::const_iterator it = _dealloc.find(memorynode->GetText());
|
|
|
|
if (it != _dealloc.end()) {
|
|
|
|
allocationId = it->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allocationId == 0) {
|
2014-12-12 22:16:20 +01:00
|
|
|
if (nodename == "memory")
|
2014-04-19 13:15:06 +02:00
|
|
|
while (!ismemory(++allocid));
|
|
|
|
else
|
|
|
|
while (!isresource(++allocid));
|
|
|
|
allocationId = allocid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add alloc/dealloc/use functions..
|
2013-07-02 07:18:19 +02:00
|
|
|
for (const tinyxml2::XMLElement *memorynode = node->FirstChildElement(); memorynode; memorynode = memorynode->NextSiblingElement()) {
|
2014-12-12 22:16:20 +01:00
|
|
|
const std::string memorynodename = memorynode->Name();
|
|
|
|
if (memorynodename == "alloc") {
|
2014-04-19 13:15:06 +02:00
|
|
|
_alloc[memorynode->GetText()] = allocationId;
|
2013-07-15 08:44:00 +02:00
|
|
|
const char *init = memorynode->Attribute("init");
|
|
|
|
if (init && strcmp(init,"false")==0) {
|
|
|
|
returnuninitdata.insert(memorynode->GetText());
|
|
|
|
}
|
2014-12-12 22:16:20 +01:00
|
|
|
} else if (memorynodename == "dealloc")
|
2014-04-19 13:15:06 +02:00
|
|
|
_dealloc[memorynode->GetText()] = allocationId;
|
2014-12-12 22:16:20 +01:00
|
|
|
else if (memorynodename == "use")
|
2013-07-05 20:55:31 +02:00
|
|
|
use.insert(memorynode->GetText());
|
2013-07-02 07:18:19 +02:00
|
|
|
else
|
2014-12-12 22:16:20 +01:00
|
|
|
return Error(BAD_ELEMENT, memorynodename);
|
2013-07-02 07:18:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (nodename == "define") {
|
2014-02-06 09:22:07 +01:00
|
|
|
const char *name = node->Attribute("name");
|
2014-02-15 08:41:17 +01:00
|
|
|
if (name == nullptr)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(MISSING_ATTRIBUTE, "name");
|
2014-02-06 09:22:07 +01:00
|
|
|
const char *value = node->Attribute("value");
|
2014-02-15 08:41:17 +01:00
|
|
|
if (value == nullptr)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(MISSING_ATTRIBUTE, "value");
|
2014-02-06 09:22:07 +01:00
|
|
|
defines.push_back(std::string("#define ") +
|
|
|
|
name +
|
|
|
|
" " +
|
|
|
|
value +
|
|
|
|
"\n");
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (nodename == "function") {
|
|
|
|
const char *name_char = node->Attribute("name");
|
|
|
|
if (name_char == nullptr)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(MISSING_ATTRIBUTE, "name");
|
2014-09-10 17:03:31 +02:00
|
|
|
std::string name = name_char;
|
2013-07-14 10:10:11 +02:00
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *functionnode = node->FirstChildElement(); functionnode; functionnode = functionnode->NextSiblingElement()) {
|
2014-12-12 22:16:20 +01:00
|
|
|
const std::string functionnodename = functionnode->Name();
|
2014-09-10 17:03:31 +02:00
|
|
|
if (functionnodename == "noreturn")
|
2013-07-14 10:10:11 +02:00
|
|
|
_noreturn[name] = (strcmp(functionnode->GetText(), "true") == 0);
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (functionnodename == "pure")
|
2014-03-14 06:38:45 +01:00
|
|
|
functionpure.insert(name);
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (functionnodename == "const") {
|
2014-03-14 06:38:45 +01:00
|
|
|
functionconst.insert(name);
|
2014-03-14 20:08:34 +01:00
|
|
|
functionpure.insert(name); // a constant function is pure
|
2014-09-10 17:03:31 +02:00
|
|
|
} else if (functionnodename == "leak-ignore")
|
2013-07-20 13:12:24 +02:00
|
|
|
leakignore.insert(name);
|
2014-09-27 21:31:04 +02:00
|
|
|
else if (functionnodename == "use-retval")
|
|
|
|
useretval.insert(name);
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (functionnodename == "arg" && functionnode->Attribute("nr") != nullptr) {
|
2014-03-06 06:16:14 +01:00
|
|
|
const bool bAnyArg = strcmp(functionnode->Attribute("nr"),"any")==0;
|
|
|
|
const int nr = (bAnyArg) ? -1 : atoi(functionnode->Attribute("nr"));
|
2013-12-23 10:06:45 +01:00
|
|
|
bool notbool = false;
|
2013-07-15 21:58:29 +02:00
|
|
|
bool notnull = false;
|
|
|
|
bool notuninit = false;
|
2013-07-22 20:21:45 +02:00
|
|
|
bool formatstr = false;
|
|
|
|
bool strz = false;
|
2014-09-10 17:03:31 +02:00
|
|
|
std::string& valid = argumentChecks[name][nr].valid;
|
|
|
|
std::list<ArgumentChecks::MinSize>& minsizes = argumentChecks[name][nr].minsizes;
|
2013-07-15 21:58:29 +02:00
|
|
|
for (const tinyxml2::XMLElement *argnode = functionnode->FirstChildElement(); argnode; argnode = argnode->NextSiblingElement()) {
|
2014-12-12 22:16:20 +01:00
|
|
|
const std::string argnodename = argnode->Name();
|
2014-09-10 17:03:31 +02:00
|
|
|
if (argnodename == "not-bool")
|
2013-12-23 10:06:45 +01:00
|
|
|
notbool = true;
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (argnodename == "not-null")
|
2013-07-15 21:58:29 +02:00
|
|
|
notnull = true;
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (argnodename == "not-uninit")
|
2013-07-15 21:58:29 +02:00
|
|
|
notuninit = true;
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (argnodename == "formatstr")
|
2013-11-21 16:32:53 +01:00
|
|
|
formatstr = true;
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (argnodename == "strz")
|
2013-11-21 16:32:53 +01:00
|
|
|
strz = true;
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (argnodename == "valid") {
|
2013-12-22 19:10:14 +01:00
|
|
|
// Validate the validation expression
|
|
|
|
const char *p = argnode->GetText();
|
2014-06-08 18:12:11 +02:00
|
|
|
bool error = false;
|
|
|
|
bool range = false;
|
2013-12-22 19:10:14 +01:00
|
|
|
for (; *p; p++) {
|
|
|
|
if (std::isdigit(*p))
|
2014-06-08 18:12:11 +02:00
|
|
|
error |= (*(p+1) == '-');
|
|
|
|
else if (*p == ':')
|
|
|
|
error |= range;
|
|
|
|
else if (*p == '-')
|
|
|
|
error |= (!std::isdigit(*(p+1)));
|
|
|
|
else if (*p == ',')
|
|
|
|
range = false;
|
|
|
|
else
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
range |= (*p == ':');
|
2013-12-22 19:10:14 +01:00
|
|
|
}
|
2014-06-08 18:12:11 +02:00
|
|
|
if (error)
|
|
|
|
return Error(BAD_ATTRIBUTE_VALUE, argnode->GetText());
|
2013-12-22 19:10:14 +01:00
|
|
|
|
|
|
|
// Set validation expression
|
|
|
|
valid = argnode->GetText();
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (argnodename == "minsize") {
|
2014-07-05 20:31:43 +02:00
|
|
|
const char *typeattr = argnode->Attribute("type");
|
|
|
|
if (!typeattr)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "type");
|
|
|
|
|
|
|
|
ArgumentChecks::MinSize::Type type;
|
|
|
|
if (strcmp(typeattr,"strlen")==0)
|
2014-07-05 22:47:10 +02:00
|
|
|
type = ArgumentChecks::MinSize::STRLEN;
|
2014-07-05 20:31:43 +02:00
|
|
|
else if (strcmp(typeattr,"argvalue")==0)
|
2014-07-05 22:47:10 +02:00
|
|
|
type = ArgumentChecks::MinSize::ARGVALUE;
|
2014-07-05 20:31:43 +02:00
|
|
|
else if (strcmp(typeattr,"sizeof")==0)
|
2014-07-05 22:47:10 +02:00
|
|
|
type = ArgumentChecks::MinSize::SIZEOF;
|
2014-07-05 20:31:43 +02:00
|
|
|
else if (strcmp(typeattr,"mul")==0)
|
2014-07-05 22:47:10 +02:00
|
|
|
type = ArgumentChecks::MinSize::MUL;
|
2014-07-05 20:31:43 +02:00
|
|
|
else
|
|
|
|
return Error(BAD_ATTRIBUTE_VALUE, typeattr);
|
|
|
|
|
|
|
|
const char *argattr = argnode->Attribute("arg");
|
|
|
|
if (!argattr)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "arg");
|
|
|
|
if (strlen(argattr) != 1 || argattr[0]<'0' || argattr[0]>'9')
|
|
|
|
return Error(BAD_ATTRIBUTE_VALUE, argattr);
|
|
|
|
|
|
|
|
minsizes.push_back(ArgumentChecks::MinSize(type,argattr[0]-'0'));
|
2014-07-05 22:47:10 +02:00
|
|
|
if (type == ArgumentChecks::MinSize::MUL) {
|
2014-07-05 20:31:43 +02:00
|
|
|
const char *arg2attr = argnode->Attribute("arg2");
|
|
|
|
if (!arg2attr)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "arg2");
|
|
|
|
if (strlen(arg2attr) != 1 || arg2attr[0]<'0' || arg2attr[0]>'9')
|
|
|
|
return Error(BAD_ATTRIBUTE_VALUE, arg2attr);
|
|
|
|
minsizes.back().arg2 = arg2attr[0] - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
else
|
2014-12-12 22:16:20 +01:00
|
|
|
return Error(BAD_ATTRIBUTE, argnodename);
|
2013-07-15 21:58:29 +02:00
|
|
|
}
|
2013-12-23 10:06:45 +01:00
|
|
|
argumentChecks[name][nr].notbool = notbool;
|
2013-12-22 19:10:14 +01:00
|
|
|
argumentChecks[name][nr].notnull = notnull;
|
2013-07-16 09:40:31 +02:00
|
|
|
argumentChecks[name][nr].notuninit = notuninit;
|
2013-07-22 20:21:45 +02:00
|
|
|
argumentChecks[name][nr].formatstr = formatstr;
|
2013-12-22 19:10:14 +01:00
|
|
|
argumentChecks[name][nr].strz = strz;
|
2014-09-10 17:03:31 +02:00
|
|
|
} else if (functionnodename == "ignorefunction") {
|
2014-01-02 17:28:24 +01:00
|
|
|
_ignorefunction.insert(name);
|
2014-09-10 17:03:31 +02:00
|
|
|
} else if (functionnodename == "formatstr") {
|
2014-01-12 12:44:24 +01:00
|
|
|
const tinyxml2::XMLAttribute* scan = functionnode->FindAttribute("scan");
|
|
|
|
const tinyxml2::XMLAttribute* secure = functionnode->FindAttribute("secure");
|
|
|
|
_formatstr[name] = std::make_pair(scan && scan->BoolValue(), secure && secure->BoolValue());
|
2013-07-14 10:10:11 +02:00
|
|
|
} else
|
2014-12-12 22:16:20 +01:00
|
|
|
return Error(BAD_ELEMENT, functionnodename);
|
2013-07-14 10:10:11 +02:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (nodename == "reflection") {
|
2014-03-11 15:57:28 +01:00
|
|
|
for (const tinyxml2::XMLElement *reflectionnode = node->FirstChildElement(); reflectionnode; reflectionnode = reflectionnode->NextSiblingElement()) {
|
|
|
|
if (strcmp(reflectionnode->Name(), "call") != 0)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(BAD_ELEMENT, reflectionnode->Name());
|
2014-03-11 15:57:28 +01:00
|
|
|
|
|
|
|
const char * const argString = reflectionnode->Attribute("arg");
|
|
|
|
if (!argString)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(MISSING_ATTRIBUTE, "arg");
|
2014-03-11 15:57:28 +01:00
|
|
|
|
|
|
|
_reflection[reflectionnode->GetText()] = atoi(argString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (nodename == "markup") {
|
2013-12-22 18:44:31 +01:00
|
|
|
const char * const extension = node->Attribute("ext");
|
|
|
|
if (!extension)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(MISSING_ATTRIBUTE, "ext");
|
2013-12-22 18:44:31 +01:00
|
|
|
_markupExtensions.insert(extension);
|
2013-10-20 14:09:10 +02:00
|
|
|
|
2013-12-22 18:44:31 +01:00
|
|
|
const char * const reporterrors = node->Attribute("reporterrors");
|
|
|
|
_reporterrors[extension] = (reporterrors && strcmp(reporterrors, "true") == 0);
|
2014-01-02 18:18:24 +01:00
|
|
|
const char * const aftercode = node->Attribute("aftercode");
|
|
|
|
_processAfterCode[extension] = (aftercode && strcmp(aftercode, "true") == 0);
|
2013-12-22 18:44:31 +01:00
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *markupnode = node->FirstChildElement(); markupnode; markupnode = markupnode->NextSiblingElement()) {
|
2014-12-12 22:16:20 +01:00
|
|
|
const std::string markupnodename = markupnode->Name();
|
2014-09-10 17:03:31 +02:00
|
|
|
if (markupnodename == "keywords") {
|
2013-12-22 18:44:31 +01:00
|
|
|
for (const tinyxml2::XMLElement *librarynode = markupnode->FirstChildElement(); librarynode; librarynode = librarynode->NextSiblingElement()) {
|
2014-08-12 09:14:28 +02:00
|
|
|
if (strcmp(librarynode->Name(), "keyword") == 0) {
|
|
|
|
const char* nodeName = librarynode->Attribute("name");
|
|
|
|
if (nodeName == nullptr)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "name");
|
|
|
|
_keywords[extension].insert(nodeName);
|
|
|
|
} else
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(BAD_ELEMENT, librarynode->Name());
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (markupnodename == "exported") {
|
2014-01-02 18:18:24 +01:00
|
|
|
for (const tinyxml2::XMLElement *exporter = markupnode->FirstChildElement(); exporter; exporter = exporter->NextSiblingElement()) {
|
2013-12-22 18:44:31 +01:00
|
|
|
if (strcmp(exporter->Name(), "exporter") != 0)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(BAD_ELEMENT, exporter->Name());
|
2013-12-22 18:44:31 +01:00
|
|
|
|
|
|
|
const char * const prefix = exporter->Attribute("prefix");
|
|
|
|
if (!prefix)
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(MISSING_ATTRIBUTE, "prefix");
|
2013-12-22 18:44:31 +01:00
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *e = exporter->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
2014-12-12 22:16:20 +01:00
|
|
|
const std::string ename = e->Name();
|
|
|
|
if (ename == "prefix")
|
2013-12-22 18:44:31 +01:00
|
|
|
_exporters[prefix].addPrefix(e->GetText());
|
2014-12-12 22:16:20 +01:00
|
|
|
else if (ename == "suffix")
|
2013-12-22 18:44:31 +01:00
|
|
|
_exporters[prefix].addSuffix(e->GetText());
|
|
|
|
else
|
2014-12-12 22:16:20 +01:00
|
|
|
return Error(BAD_ELEMENT, ename);
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (markupnodename == "imported") {
|
2013-12-22 18:44:31 +01:00
|
|
|
for (const tinyxml2::XMLElement *librarynode = markupnode->FirstChildElement(); librarynode; librarynode = librarynode->NextSiblingElement()) {
|
|
|
|
if (strcmp(librarynode->Name(), "importer") == 0)
|
|
|
|
_importers[extension].insert(librarynode->GetText());
|
|
|
|
else
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(BAD_ELEMENT, librarynode->Name());
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (markupnodename == "codeblocks") {
|
2013-12-23 12:27:00 +01:00
|
|
|
for (const tinyxml2::XMLElement *blocknode = markupnode->FirstChildElement(); blocknode; blocknode = blocknode->NextSiblingElement()) {
|
2014-12-12 22:16:20 +01:00
|
|
|
const std::string blocknodename = blocknode->Name();
|
|
|
|
if (blocknodename == "block") {
|
2014-08-04 08:00:53 +02:00
|
|
|
const char * blockName = blocknode->Attribute("name");
|
|
|
|
if (blockName)
|
|
|
|
_executableblocks[extension].addBlock(blockName);
|
2014-12-12 22:16:20 +01:00
|
|
|
} else if (blocknodename == "structure") {
|
2013-12-22 18:44:31 +01:00
|
|
|
const char * start = blocknode->Attribute("start");
|
2013-10-20 14:09:10 +02:00
|
|
|
if (start)
|
2013-10-27 13:55:13 +01:00
|
|
|
_executableblocks[extension].setStart(start);
|
2013-12-22 18:44:31 +01:00
|
|
|
const char * end = blocknode->Attribute("end");
|
2013-10-20 14:09:10 +02:00
|
|
|
if (end)
|
2013-10-27 13:55:13 +01:00
|
|
|
_executableblocks[extension].setEnd(end);
|
2013-12-22 18:44:31 +01:00
|
|
|
const char * offset = blocknode->Attribute("offset");
|
2013-10-20 14:09:10 +02:00
|
|
|
if (offset)
|
2013-10-27 13:55:13 +01:00
|
|
|
_executableblocks[extension].setOffset(atoi(offset));
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2014-12-12 22:16:20 +01:00
|
|
|
return Error(BAD_ELEMENT, blocknodename);
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
|
|
|
|
else
|
2014-12-12 22:16:20 +01:00
|
|
|
return Error(BAD_ELEMENT, markupnodename);
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
|
2015-01-03 20:35:33 +01:00
|
|
|
else if (nodename == "container") {
|
|
|
|
const char* const id = node->Attribute("id");
|
|
|
|
if (!id)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "id");
|
|
|
|
|
|
|
|
Container& container = containers[id];
|
|
|
|
|
|
|
|
const char* const inherits = node->Attribute("inherits");
|
|
|
|
if (inherits) {
|
|
|
|
std::map<std::string, Container>::const_iterator i = containers.find(inherits);
|
|
|
|
if (inherits)
|
|
|
|
container = i->second; // Take values from parent and overwrite them if necessary
|
|
|
|
else
|
|
|
|
return Error(BAD_ATTRIBUTE_VALUE, inherits);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* const startPattern = node->Attribute("startPattern");
|
|
|
|
if (startPattern)
|
|
|
|
container.startPattern = startPattern;
|
|
|
|
const char* const endPattern = node->Attribute("endPattern");
|
|
|
|
if (endPattern)
|
|
|
|
container.endPattern = endPattern;
|
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *containerNode = node->FirstChildElement(); containerNode; containerNode = containerNode->NextSiblingElement()) {
|
|
|
|
const std::string containerNodeName = containerNode->Name();
|
|
|
|
if (containerNodeName == "size" || containerNodeName == "access" || containerNodeName == "other") {
|
|
|
|
for (const tinyxml2::XMLElement *functionNode = containerNode->FirstChildElement(); functionNode; functionNode = functionNode->NextSiblingElement()) {
|
|
|
|
if (std::string(functionNode->Name()) != "function")
|
|
|
|
return Error(BAD_ELEMENT, functionNode->Name());
|
|
|
|
|
|
|
|
const char* const functionName = functionNode->Attribute("name");
|
|
|
|
if (!functionName)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "name");
|
|
|
|
|
|
|
|
const char* const action_ptr = functionNode->Attribute("action");
|
|
|
|
Container::Action action = Container::NO_ACTION;
|
|
|
|
if (action_ptr) {
|
|
|
|
std::string actionName = action_ptr;
|
|
|
|
if (actionName == "resize")
|
|
|
|
action = Container::RESIZE;
|
|
|
|
else if (actionName == "clear")
|
|
|
|
action = Container::CLEAR;
|
|
|
|
else if (actionName == "push")
|
|
|
|
action = Container::PUSH;
|
|
|
|
else if (actionName == "pop")
|
|
|
|
action = Container::POP;
|
2015-01-04 12:43:31 +01:00
|
|
|
else if (actionName == "find")
|
|
|
|
action = Container::FIND;
|
2015-01-03 20:35:33 +01:00
|
|
|
else
|
|
|
|
return Error(BAD_ATTRIBUTE_VALUE, actionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* const yield_ptr = functionNode->Attribute("yields");
|
|
|
|
Container::Yield yield = Container::NO_YIELD;
|
|
|
|
if (yield_ptr) {
|
|
|
|
std::string yieldName = yield_ptr;
|
|
|
|
if (yieldName == "at_index")
|
|
|
|
yield = Container::AT_INDEX;
|
|
|
|
else if (yieldName == "item")
|
|
|
|
yield = Container::ITEM;
|
|
|
|
else if (yieldName == "buffer")
|
|
|
|
yield = Container::BUFFER;
|
|
|
|
else if (yieldName == "buffer-nt")
|
|
|
|
yield = Container::BUFFER_NT;
|
|
|
|
else if (yieldName == "start-iterator")
|
|
|
|
yield = Container::START_ITERATOR;
|
|
|
|
else if (yieldName == "end-iterator")
|
|
|
|
yield = Container::END_ITERATOR;
|
|
|
|
else if (yieldName == "size")
|
|
|
|
yield = Container::SIZE;
|
|
|
|
else if (yieldName == "empty")
|
|
|
|
yield = Container::EMPTY;
|
|
|
|
else
|
|
|
|
return Error(BAD_ATTRIBUTE_VALUE, yieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
container.functions[functionName].action = action;
|
|
|
|
container.functions[functionName].yield = yield;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (containerNodeName == "size") {
|
|
|
|
const char* const templateArg = containerNode->Attribute("templateParameter");
|
|
|
|
if (templateArg)
|
|
|
|
container.size_templateArgNo = atoi(templateArg);
|
|
|
|
} else if (containerNodeName == "access") {
|
|
|
|
const char* const indexArg = containerNode->Attribute("indexOperator");
|
|
|
|
if (indexArg)
|
|
|
|
container.arrayLike_indexOp = std::string(indexArg) == "array-like";
|
|
|
|
}
|
|
|
|
} else if (containerNodeName == "type") {
|
|
|
|
const char* const templateArg = containerNode->Attribute("templateParameter");
|
|
|
|
if (templateArg)
|
|
|
|
container.type_templateArgNo = atoi(templateArg);
|
|
|
|
|
|
|
|
const char* const string = containerNode->Attribute("string");
|
|
|
|
if (string)
|
|
|
|
container.stdStringLike = std::string(string) == "std-like";
|
|
|
|
} else
|
|
|
|
return Error(BAD_ELEMENT, containerNodeName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:03:31 +02:00
|
|
|
else if (nodename == "podtype") {
|
2014-06-08 12:09:00 +02:00
|
|
|
const char * const name = node->Attribute("name");
|
|
|
|
if (!name)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "name");
|
2014-06-13 15:46:43 +02:00
|
|
|
PodType podType = {0};
|
2014-06-08 12:09:00 +02:00
|
|
|
const char * const size = node->Attribute("sizeof");
|
|
|
|
if (size)
|
|
|
|
podType.size = atoi(size);
|
|
|
|
const char * const sign = node->Attribute("sign");
|
|
|
|
if (sign)
|
|
|
|
podType.sign = *sign;
|
|
|
|
podtypes[name] = podType;
|
|
|
|
}
|
|
|
|
|
2014-10-19 07:34:40 +02:00
|
|
|
else if (nodename == "platformtype") {
|
|
|
|
const char * const type_name = node->Attribute("name");
|
|
|
|
if (type_name == nullptr)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "name");
|
|
|
|
const char *value = node->Attribute("value");
|
|
|
|
if (value == nullptr)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "value");
|
|
|
|
PlatformType type;
|
|
|
|
type._type = value;
|
|
|
|
std::set<std::string> platform;
|
|
|
|
for (const tinyxml2::XMLElement *typenode = node->FirstChildElement(); typenode; typenode = typenode->NextSiblingElement()) {
|
2014-12-12 22:16:20 +01:00
|
|
|
const std::string typenodename = typenode->Name();
|
|
|
|
if (typenodename == "platform") {
|
2014-10-19 07:34:40 +02:00
|
|
|
const char * const type_attribute = typenode->Attribute("type");
|
|
|
|
if (type_attribute == nullptr)
|
|
|
|
return Error(MISSING_ATTRIBUTE, "type");
|
|
|
|
platform.insert(type_attribute);
|
2014-12-12 22:16:20 +01:00
|
|
|
} else if (typenodename == "signed")
|
2014-10-19 07:34:40 +02:00
|
|
|
type._signed = true;
|
2014-12-12 22:16:20 +01:00
|
|
|
else if (typenodename == "unsigned")
|
2014-10-19 07:34:40 +02:00
|
|
|
type._unsigned = true;
|
2014-12-12 22:16:20 +01:00
|
|
|
else if (typenodename == "long")
|
2014-10-19 07:34:40 +02:00
|
|
|
type._long = true;
|
2014-12-12 22:16:20 +01:00
|
|
|
else if (typenodename == "pointer")
|
2014-10-19 07:34:40 +02:00
|
|
|
type._pointer= true;
|
2014-12-12 22:16:20 +01:00
|
|
|
else if (typenodename == "ptr_ptr")
|
2014-10-19 07:34:40 +02:00
|
|
|
type._ptr_ptr = true;
|
2014-12-12 22:16:20 +01:00
|
|
|
else if (typenodename == "const_ptr")
|
2014-10-19 07:34:40 +02:00
|
|
|
type._const_ptr = true;
|
|
|
|
else
|
2014-12-12 22:16:20 +01:00
|
|
|
return Error(BAD_ELEMENT, typenodename);
|
2014-10-19 07:34:40 +02:00
|
|
|
}
|
|
|
|
if (platform.empty()) {
|
|
|
|
const PlatformType * const type_ptr = platform_type(type_name, "");
|
|
|
|
if (type_ptr) {
|
|
|
|
if (*type_ptr == type)
|
|
|
|
return Error(DUPLICATE_PLATFORM_TYPE, type_name);
|
|
|
|
return Error(PLATFORM_TYPE_REDEFINED, type_name);
|
|
|
|
}
|
|
|
|
platform_types[type_name] = type;
|
|
|
|
} else {
|
|
|
|
std::set<std::string>::const_iterator it;
|
|
|
|
for (it = platform.begin(); it != platform.end(); ++it) {
|
|
|
|
const PlatformType * const type_ptr = platform_type(type_name, *it);
|
|
|
|
if (type_ptr) {
|
|
|
|
if (*type_ptr == type)
|
|
|
|
return Error(DUPLICATE_PLATFORM_TYPE, type_name);
|
|
|
|
return Error(PLATFORM_TYPE_REDEFINED, type_name);
|
|
|
|
}
|
|
|
|
platforms[*it]._platform_types[type_name] = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-22 18:44:31 +01:00
|
|
|
else
|
2014-12-12 22:16:20 +01:00
|
|
|
return Error(BAD_ELEMENT, nodename);
|
2013-07-02 07:18:19 +02:00
|
|
|
}
|
2014-06-04 19:18:27 +02:00
|
|
|
return Error(OK);
|
2013-07-02 07:18:19 +02:00
|
|
|
}
|
2013-12-23 10:06:45 +01:00
|
|
|
|
2015-01-08 19:31:41 +01:00
|
|
|
bool Library::isargvalid(const Token *ftok, int argnr, const MathLib::bigint argvalue) const
|
2013-12-23 10:06:45 +01:00
|
|
|
{
|
2015-01-08 19:31:41 +01:00
|
|
|
const ArgumentChecks *ac = getarg(ftok, argnr);
|
2013-12-23 10:06:45 +01:00
|
|
|
if (!ac || ac->valid.empty())
|
|
|
|
return true;
|
|
|
|
TokenList tokenList(0);
|
|
|
|
std::istringstream istr(ac->valid + ',');
|
2014-10-03 10:02:46 +02:00
|
|
|
tokenList.createTokens(istr);
|
2014-06-08 18:12:11 +02:00
|
|
|
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok,"- %num%")) {
|
|
|
|
tok->str("-" + tok->strAt(1));
|
|
|
|
tok->deleteNext();
|
|
|
|
}
|
2014-06-07 16:28:29 +02:00
|
|
|
}
|
2013-12-23 10:06:45 +01:00
|
|
|
for (const Token *tok = tokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (tok->isNumber() && argvalue == MathLib::toLongNumber(tok->str()))
|
|
|
|
return true;
|
2014-06-08 18:12:11 +02:00
|
|
|
if (Token::Match(tok, "%num% : %num%") && argvalue >= MathLib::toLongNumber(tok->str()) && argvalue <= MathLib::toLongNumber(tok->strAt(2)))
|
2013-12-23 10:06:45 +01:00
|
|
|
return true;
|
2014-06-08 18:12:11 +02:00
|
|
|
if (Token::Match(tok, "%num% : ,") && argvalue >= MathLib::toLongNumber(tok->str()))
|
2013-12-23 10:06:45 +01:00
|
|
|
return true;
|
2014-06-08 18:12:11 +02:00
|
|
|
if ((!tok->previous() || tok->previous()->str() == ",") && Token::Match(tok,": %num%") && argvalue <= MathLib::toLongNumber(tok->strAt(1)))
|
2013-12-23 10:06:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-06 06:16:14 +01:00
|
|
|
|
2015-01-08 19:31:41 +01:00
|
|
|
const Library::ArgumentChecks * Library::getarg(const Token *ftok, int argnr) const
|
2014-03-06 06:16:14 +01:00
|
|
|
{
|
2015-01-08 19:31:41 +01:00
|
|
|
if (isNotLibraryFunction(ftok))
|
|
|
|
return nullptr;
|
2014-03-06 06:16:14 +01:00
|
|
|
std::map<std::string, std::map<int, ArgumentChecks> >::const_iterator it1;
|
2015-01-08 19:31:41 +01:00
|
|
|
it1 = argumentChecks.find(ftok->str());
|
2014-03-06 06:16:14 +01:00
|
|
|
if (it1 == argumentChecks.end())
|
|
|
|
return nullptr;
|
|
|
|
const std::map<int,ArgumentChecks>::const_iterator it2 = it1->second.find(argnr);
|
|
|
|
if (it2 != it1->second.end())
|
|
|
|
return &it2->second;
|
|
|
|
const std::map<int,ArgumentChecks>::const_iterator it3 = it1->second.find(-1);
|
|
|
|
if (it3 != it1->second.end())
|
|
|
|
return &it3->second;
|
2014-03-06 06:27:01 +01:00
|
|
|
return nullptr;
|
2014-03-06 06:16:14 +01:00
|
|
|
}
|
2014-06-22 19:13:15 +02:00
|
|
|
|
|
|
|
bool Library::isScopeNoReturn(const Token *end, std::string *unknownFunc) const
|
|
|
|
{
|
|
|
|
if (unknownFunc)
|
|
|
|
unknownFunc->clear();
|
|
|
|
|
|
|
|
if (!Token::simpleMatch(end->tokAt(-2), ") ; }"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const Token *funcname = end->linkAt(-2)->previous();
|
|
|
|
const Token *start = funcname;
|
|
|
|
if (funcname && Token::Match(funcname->tokAt(-3),"( * %var% )")) {
|
|
|
|
funcname = funcname->previous();
|
|
|
|
start = funcname->tokAt(-3);
|
|
|
|
} else if (funcname->isName()) {
|
|
|
|
while (Token::Match(start, "%var%|.|::"))
|
|
|
|
start = start->previous();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Token::Match(start,"[;{}]") && Token::Match(funcname, "%var% )| (")) {
|
|
|
|
if (funcname->str() == "exit")
|
|
|
|
return true;
|
2015-01-08 19:31:41 +01:00
|
|
|
if (!isnotnoreturn(funcname)) {
|
|
|
|
if (unknownFunc && !isnoreturn(funcname))
|
2014-06-22 19:13:15 +02:00
|
|
|
*unknownFunc = funcname->str();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-03 20:35:33 +01:00
|
|
|
const Library::Container* Library::detectContainer(const Token* typeStart) const
|
|
|
|
{
|
|
|
|
for (std::map<std::string, Container>::const_iterator i = containers.begin(); i != containers.end(); ++i) {
|
|
|
|
const Container& container = i->second;
|
|
|
|
if (container.startPattern.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Token::Match(typeStart, container.startPattern.c_str())) {
|
|
|
|
if (container.endPattern.empty())
|
|
|
|
return &container;
|
|
|
|
|
|
|
|
for (const Token* tok = typeStart; tok && !tok->varId(); tok = tok->next()) {
|
|
|
|
if (tok->link()) {
|
|
|
|
if (Token::Match(tok->link(), container.endPattern.c_str()))
|
|
|
|
return &container;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|