2013-07-02 07:18:19 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2013 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/>.
|
|
|
|
*/
|
|
|
|
|
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"
|
2013-07-02 07:18:19 +02:00
|
|
|
|
2013-07-20 17:12:56 +02:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2013-12-28 21:57:13 +01:00
|
|
|
static tinyxml2::XMLError LoadFile(tinyxml2::XMLDocument &doc, const std::string &path)
|
|
|
|
{
|
|
|
|
FILE *f = fopen(path.c_str(),"rb");
|
|
|
|
if (!f)
|
|
|
|
return tinyxml2::XML_ERROR_FILE_NOT_FOUND;
|
|
|
|
|
|
|
|
// is file ok? if "path" is a folder then reading from it will cause ferror() to return a non-zero value
|
|
|
|
fgetc(f);
|
|
|
|
int errorcode = ferror(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
// if file is ok, try to load it
|
|
|
|
return (errorcode == 0) ? doc.LoadFile(path.c_str()) : tinyxml2::XML_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2013-07-08 18:26:18 +02:00
|
|
|
Library::Library() : allocid(0)
|
2013-07-02 07:18:19 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-20 10:21:05 +02:00
|
|
|
bool Library::load(const char exename[], const char path[])
|
2013-07-02 07:18:19 +02:00
|
|
|
{
|
2013-07-20 10:21:05 +02:00
|
|
|
if (std::strchr(path,',') != NULL) {
|
|
|
|
bool ret = true;
|
|
|
|
std::string p(path);
|
|
|
|
while (p.find(",") != std::string::npos) {
|
|
|
|
const std::string::size_type pos = p.find(",");
|
|
|
|
ret &= load(exename, p.substr(0,pos).c_str());
|
|
|
|
p = p.substr(pos+1);
|
|
|
|
}
|
|
|
|
if (!p.empty())
|
|
|
|
ret &= load(exename, p.c_str());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-20 17:12:56 +02:00
|
|
|
// open file..
|
2013-10-27 17:32:38 +01:00
|
|
|
tinyxml2::XMLDocument doc;
|
2013-12-28 21:57:13 +01:00
|
|
|
tinyxml2::XMLError error = LoadFile(doc,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";
|
2013-12-28 21:57:13 +01:00
|
|
|
error = LoadFile(doc,fullfilename);
|
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)
|
|
|
|
return false;
|
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);
|
2013-12-28 21:57:13 +01:00
|
|
|
error = LoadFile(doc,filename);
|
2013-07-20 17:12:56 +02:00
|
|
|
}
|
2013-07-20 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2013-10-27 17:38:39 +01:00
|
|
|
return (error == tinyxml2::XML_NO_ERROR) && load(doc);
|
2013-10-27 17:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Library::load(const tinyxml2::XMLDocument &doc)
|
|
|
|
{
|
2013-07-02 07:18:19 +02:00
|
|
|
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
|
2013-10-31 19:49:36 +01:00
|
|
|
|
|
|
|
if (rootnode == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (strcmp(rootnode->Name(),"def") != 0)
|
2013-07-02 07:18:19 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
2013-11-23 18:16:40 +01:00
|
|
|
if (strcmp(node->Name(),"memory")==0 || strcmp(node->Name(),"resource")==0) {
|
|
|
|
if (strcmp(node->Name(), "memory")==0)
|
|
|
|
while (!ismemory(++allocid));
|
|
|
|
else
|
|
|
|
while (!isresource(++allocid));
|
2013-07-02 07:18:19 +02:00
|
|
|
for (const tinyxml2::XMLElement *memorynode = node->FirstChildElement(); memorynode; memorynode = memorynode->NextSiblingElement()) {
|
2013-07-15 08:44:00 +02:00
|
|
|
if (strcmp(memorynode->Name(),"alloc")==0) {
|
2013-07-05 20:55:31 +02:00
|
|
|
_alloc[memorynode->GetText()] = allocid;
|
2013-07-15 08:44:00 +02:00
|
|
|
const char *init = memorynode->Attribute("init");
|
|
|
|
if (init && strcmp(init,"false")==0) {
|
|
|
|
returnuninitdata.insert(memorynode->GetText());
|
|
|
|
}
|
|
|
|
} else if (strcmp(memorynode->Name(),"dealloc")==0)
|
2013-07-05 20:55:31 +02:00
|
|
|
_dealloc[memorynode->GetText()] = allocid;
|
|
|
|
else if (strcmp(memorynode->Name(),"use")==0)
|
|
|
|
use.insert(memorynode->GetText());
|
2013-07-02 07:18:19 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-14 10:10:11 +02:00
|
|
|
else if (strcmp(node->Name(),"function")==0) {
|
|
|
|
const char *name = node->Attribute("name");
|
|
|
|
if (name == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *functionnode = node->FirstChildElement(); functionnode; functionnode = functionnode->NextSiblingElement()) {
|
|
|
|
if (strcmp(functionnode->Name(),"noreturn")==0)
|
|
|
|
_noreturn[name] = (strcmp(functionnode->GetText(), "true") == 0);
|
2013-07-20 13:12:24 +02:00
|
|
|
else if (strcmp(functionnode->Name(),"leak-ignore")==0)
|
|
|
|
leakignore.insert(name);
|
2013-10-20 14:09:10 +02:00
|
|
|
else if (strcmp(functionnode->Name(), "arg") == 0 && functionnode->Attribute("nr") != NULL) {
|
2013-07-14 10:10:11 +02:00
|
|
|
const int nr = 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;
|
2013-12-22 19:10:14 +01:00
|
|
|
std::string valid;
|
2013-07-15 21:58:29 +02:00
|
|
|
for (const tinyxml2::XMLElement *argnode = functionnode->FirstChildElement(); argnode; argnode = argnode->NextSiblingElement()) {
|
2013-12-23 10:06:45 +01:00
|
|
|
if (strcmp(argnode->Name(), "not-bool") == 0)
|
|
|
|
notbool = true;
|
|
|
|
else if (strcmp(argnode->Name(), "not-null") == 0)
|
2013-07-15 21:58:29 +02:00
|
|
|
notnull = true;
|
|
|
|
else if (strcmp(argnode->Name(), "not-uninit") == 0)
|
|
|
|
notuninit = true;
|
2013-07-22 20:21:45 +02:00
|
|
|
else if (strcmp(argnode->Name(), "formatstr") == 0)
|
2013-11-21 16:32:53 +01:00
|
|
|
formatstr = true;
|
2013-07-22 20:21:45 +02:00
|
|
|
else if (strcmp(argnode->Name(), "strz") == 0)
|
2013-11-21 16:32:53 +01:00
|
|
|
strz = true;
|
2013-12-22 19:10:14 +01:00
|
|
|
else if (strcmp(argnode->Name(), "valid") == 0) {
|
|
|
|
// Validate the validation expression
|
|
|
|
const char *p = argnode->GetText();
|
|
|
|
if (!std::isdigit(*p))
|
|
|
|
return false;
|
|
|
|
for (; *p; p++) {
|
|
|
|
if (std::isdigit(*p))
|
|
|
|
continue;
|
|
|
|
if (*p == '-' && std::isdigit(*(p-1)))
|
|
|
|
continue;
|
|
|
|
if (*p == ',' && *(p+1) != ',')
|
|
|
|
continue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set validation expression
|
|
|
|
valid = argnode->GetText();
|
|
|
|
}
|
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
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;
|
|
|
|
argumentChecks[name][nr].valid = valid;
|
2013-10-20 14:09:10 +02:00
|
|
|
} else if (strcmp(functionnode->Name(), "ignorefunction") == 0) {
|
2014-01-02 17:28:24 +01:00
|
|
|
_ignorefunction.insert(name);
|
2013-07-14 10:10:11 +02:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2013-12-22 18:44:31 +01:00
|
|
|
else if (strcmp(node->Name(), "markup") == 0) {
|
|
|
|
const char * const extension = node->Attribute("ext");
|
|
|
|
if (!extension)
|
|
|
|
return false;
|
|
|
|
_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()) {
|
|
|
|
if (strcmp(markupnode->Name(), "keywords") == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *librarynode = markupnode->FirstChildElement(); librarynode; librarynode = librarynode->NextSiblingElement()) {
|
|
|
|
if (strcmp(librarynode->Name(), "keyword") == 0)
|
2013-12-22 17:03:46 +01:00
|
|
|
_keywords[extension].insert(librarynode->Attribute("name"));
|
2013-12-22 18:44:31 +01:00
|
|
|
else
|
2013-10-20 14:09:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
|
2013-12-22 18:44:31 +01:00
|
|
|
else if (strcmp(markupnode->Name(), "exported") == 0) {
|
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)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char * const prefix = exporter->Attribute("prefix");
|
|
|
|
if (!prefix)
|
2013-10-20 14:09:10 +02:00
|
|
|
return false;
|
2013-12-22 18:44:31 +01:00
|
|
|
|
|
|
|
for (const tinyxml2::XMLElement *e = exporter->FirstChildElement(); e; e = e->NextSiblingElement()) {
|
|
|
|
if (strcmp(e->Name(), "prefix") == 0)
|
|
|
|
_exporters[prefix].addPrefix(e->GetText());
|
|
|
|
else if (strcmp(e->Name(), "suffix") == 0)
|
|
|
|
_exporters[prefix].addSuffix(e->GetText());
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
|
2013-12-22 18:44:31 +01:00
|
|
|
else if (strcmp(markupnode->Name(), "imported") == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *librarynode = markupnode->FirstChildElement(); librarynode; librarynode = librarynode->NextSiblingElement()) {
|
|
|
|
if (strcmp(librarynode->Name(), "importer") == 0)
|
|
|
|
_importers[extension].insert(librarynode->GetText());
|
|
|
|
else
|
2013-10-20 14:09:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-22 18:44:31 +01:00
|
|
|
else if (strcmp(markupnode->Name(), "reflection") == 0) {
|
|
|
|
for (const tinyxml2::XMLElement *reflectionnode = markupnode->FirstChildElement(); reflectionnode; reflectionnode = reflectionnode->NextSiblingElement()) {
|
|
|
|
if (strcmp(reflectionnode->Name(), "call") != 0)
|
2013-10-20 14:09:10 +02:00
|
|
|
return false;
|
2013-12-22 18:44:31 +01:00
|
|
|
|
|
|
|
const char * const argString = reflectionnode->Attribute("arg");
|
|
|
|
if (!argString)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_reflection[extension][reflectionnode->GetText()] = atoi(argString);
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
|
2013-12-22 18:44:31 +01:00
|
|
|
else if (strcmp(markupnode->Name(), "codeblocks") == 0) {
|
2013-12-23 12:27:00 +01:00
|
|
|
for (const tinyxml2::XMLElement *blocknode = markupnode->FirstChildElement(); blocknode; blocknode = blocknode->NextSiblingElement()) {
|
2013-12-22 18:44:31 +01:00
|
|
|
if (strcmp(blocknode->Name(), "block") == 0)
|
|
|
|
_executableblocks[extension].addBlock(blocknode->Attribute("name"));
|
|
|
|
|
|
|
|
else if (strcmp(blocknode->Name(), "structure") == 0) {
|
|
|
|
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
|
2013-10-20 14:09:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
return false;
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
2013-12-22 18:44:31 +01:00
|
|
|
}
|
2013-10-20 14:09:10 +02:00
|
|
|
|
2013-12-22 18:44:31 +01:00
|
|
|
else
|
2013-07-02 07:18:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-23 10:06:45 +01:00
|
|
|
|
|
|
|
bool Library::isargvalid(const std::string &functionName, int argnr, const MathLib::bigint argvalue) const
|
|
|
|
{
|
|
|
|
const ArgumentChecks *ac = getarg(functionName, argnr);
|
|
|
|
if (!ac || ac->valid.empty())
|
|
|
|
return true;
|
|
|
|
TokenList tokenList(0);
|
|
|
|
std::istringstream istr(ac->valid + ',');
|
|
|
|
tokenList.createTokens(istr,"");
|
|
|
|
for (const Token *tok = tokenList.front(); tok; tok = tok->next()) {
|
|
|
|
if (tok->isNumber() && argvalue == MathLib::toLongNumber(tok->str()))
|
|
|
|
return true;
|
|
|
|
if (Token::Match(tok, "%num% - %num%") && argvalue >= MathLib::toLongNumber(tok->str()) && argvalue <= MathLib::toLongNumber(tok->strAt(2)))
|
|
|
|
return true;
|
|
|
|
if (Token::Match(tok, "%num% - ,") && argvalue >= MathLib::toLongNumber(tok->str()))
|
|
|
|
return true;
|
|
|
|
if ((!tok->previous() || tok->previous()->str() == ",") && Token::Match(tok,"- %num%") && argvalue <= MathLib::toLongNumber(tok->strAt(1)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|