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-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2013-07-08 18:26:18 +02:00
|
|
|
#ifndef libraryH
|
|
|
|
#define libraryH
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2013-07-02 07:18:19 +02:00
|
|
|
|
2013-07-04 13:09:34 +02:00
|
|
|
#include "config.h"
|
2013-10-27 17:10:43 +01:00
|
|
|
#include "path.h"
|
|
|
|
|
|
|
|
#include <tinyxml2.h>
|
2013-07-02 07:18:19 +02:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2013-10-20 14:09:10 +02:00
|
|
|
#include <list>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2013-07-02 07:18:19 +02:00
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/**
|
2013-07-08 18:26:18 +02:00
|
|
|
* @brief Library definitions handling
|
2013-07-02 07:18:19 +02:00
|
|
|
*/
|
2013-07-08 18:26:18 +02:00
|
|
|
class CPPCHECKLIB Library {
|
2013-07-02 07:18:19 +02:00
|
|
|
public:
|
2013-07-08 18:26:18 +02:00
|
|
|
Library();
|
2013-07-02 07:18:19 +02:00
|
|
|
|
2013-10-20 14:09:10 +02:00
|
|
|
bool load(const char exename [], const char path []);
|
2013-10-27 17:10:43 +01:00
|
|
|
bool load(const tinyxml2::XMLDocument &doc);
|
2013-07-02 07:18:19 +02:00
|
|
|
|
|
|
|
/** get allocation id for function (by name) */
|
|
|
|
int alloc(const std::string &name) const {
|
|
|
|
return getid(_alloc, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** get deallocation id for function (by name) */
|
|
|
|
int dealloc(const std::string &name) const {
|
|
|
|
return getid(_dealloc, name);
|
|
|
|
}
|
|
|
|
|
2013-07-20 17:12:56 +02:00
|
|
|
/** set allocation id for function */
|
|
|
|
void setalloc(const std::string &functionname, int id) {
|
|
|
|
_alloc[functionname] = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setdealloc(const std::string &functionname, int id) {
|
|
|
|
_dealloc[functionname] = id;
|
|
|
|
}
|
|
|
|
|
2013-08-06 11:11:59 +02:00
|
|
|
/** add noreturn function setting */
|
|
|
|
void setnoreturn(const std::string& funcname, bool noreturn) {
|
|
|
|
_noreturn[funcname] = noreturn;
|
|
|
|
}
|
|
|
|
|
2013-07-02 07:18:19 +02:00
|
|
|
/** is allocation type memory? */
|
|
|
|
static bool ismemory(int id) {
|
|
|
|
return ((id > 0) && ((id & 1) == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** is allocation type resource? */
|
|
|
|
static bool isresource(int id) {
|
|
|
|
return ((id > 0) && ((id & 1) == 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::string> use;
|
2013-07-20 13:12:24 +02:00
|
|
|
std::set<std::string> leakignore;
|
2013-07-14 10:10:11 +02:00
|
|
|
|
|
|
|
bool isnoreturn(const std::string &name) const {
|
2013-10-20 14:09:10 +02:00
|
|
|
std::map<std::string, bool>::const_iterator it = _noreturn.find(name);
|
2013-07-14 10:10:11 +02:00
|
|
|
return (it != _noreturn.end() && it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isnotnoreturn(const std::string &name) const {
|
2013-10-20 14:09:10 +02:00
|
|
|
std::map<std::string, bool>::const_iterator it = _noreturn.find(name);
|
2013-07-14 10:10:11 +02:00
|
|
|
return (it != _noreturn.end() && !it->second);
|
|
|
|
}
|
|
|
|
|
2013-07-16 09:40:31 +02:00
|
|
|
struct ArgumentChecks {
|
2013-07-22 20:21:45 +02:00
|
|
|
ArgumentChecks() {
|
|
|
|
notnull = notuninit = formatstr = strz = false;
|
|
|
|
}
|
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
bool notnull;
|
|
|
|
bool notuninit;
|
2013-07-22 20:21:45 +02:00
|
|
|
bool formatstr;
|
|
|
|
bool strz;
|
2013-07-14 10:10:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// function name, argument nr => argument data
|
2013-07-16 09:40:31 +02:00
|
|
|
std::map<std::string, std::map<int, ArgumentChecks> > argumentChecks;
|
2013-07-02 07:18:19 +02:00
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
bool isnullargbad(const std::string &functionName, int argnr) const {
|
2013-10-20 14:09:10 +02:00
|
|
|
const ArgumentChecks *arg = getarg(functionName, argnr);
|
2013-07-15 21:58:29 +02:00
|
|
|
return arg && arg->notnull;
|
|
|
|
}
|
2013-07-15 18:55:40 +02:00
|
|
|
|
2013-07-15 21:58:29 +02:00
|
|
|
bool isuninitargbad(const std::string &functionName, int argnr) const {
|
2013-10-20 14:09:10 +02:00
|
|
|
const ArgumentChecks *arg = getarg(functionName, argnr);
|
2013-07-15 21:58:29 +02:00
|
|
|
return arg && arg->notuninit;
|
|
|
|
}
|
2013-07-15 18:55:40 +02:00
|
|
|
|
2013-07-22 20:21:45 +02:00
|
|
|
bool isargformatstr(const std::string &functionName, int argnr) const {
|
2013-10-20 14:09:10 +02:00
|
|
|
const ArgumentChecks *arg = getarg(functionName, argnr);
|
2013-07-22 20:21:45 +02:00
|
|
|
return arg && arg->formatstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isargstrz(const std::string &functionName, int argnr) const {
|
2013-10-20 14:09:10 +02:00
|
|
|
const ArgumentChecks *arg = getarg(functionName, argnr);
|
2013-07-22 20:21:45 +02:00
|
|
|
return arg && arg->strz;
|
|
|
|
}
|
|
|
|
|
2013-10-31 19:09:01 +01:00
|
|
|
bool markupFile(const std::string &path) const {
|
2013-10-31 19:50:43 +01:00
|
|
|
return _markupExtensions.find(Path::getFilenameExtensionInLowerCase(path)) != _markupExtensions.end();
|
2013-10-31 19:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::set<std::string> &markupExtensions() const {
|
|
|
|
return _markupExtensions;
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool reportErrors(const std::string &path) const {
|
|
|
|
const std::map<std::string, bool>::const_iterator it =
|
|
|
|
_reporterrors.find(Path::getFilenameExtensionInLowerCase(path));
|
|
|
|
if (it != _reporterrors.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
// assume true if we don't know as it'll be a core-type (c/cpp etc)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ignorefunction(const std::string &function) const {
|
|
|
|
const std::map<std::string, bool>::const_iterator it = _ignorefunction.find(function);
|
|
|
|
return (it != _ignorefunction.end() && it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isexecutableblock(const std::string &file, const std::string &token) const {
|
|
|
|
bool isexecblock;
|
|
|
|
const std::map<std::string, CodeBlock>::const_iterator map_it
|
|
|
|
= _executableblocks.find(Path::getFilenameExtensionInLowerCase(file));
|
|
|
|
|
|
|
|
if (map_it != _executableblocks.end()) {
|
|
|
|
isexecblock = map_it->second.isBlock(token);
|
|
|
|
} else {
|
|
|
|
isexecblock = false;
|
|
|
|
}
|
|
|
|
return isexecblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
int blockstartoffset(const std::string &file) const {
|
|
|
|
int offset = -1;
|
|
|
|
const std::map<std::string, CodeBlock>::const_iterator map_it
|
|
|
|
= _executableblocks.find(Path::getFilenameExtensionInLowerCase(file));
|
|
|
|
|
|
|
|
if (map_it != _executableblocks.end()) {
|
|
|
|
offset = map_it->second.offset();
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string blockstart(const std::string &file) const {
|
|
|
|
std::string start;
|
|
|
|
const std::map<std::string, CodeBlock>::const_iterator map_it
|
|
|
|
= _executableblocks.find(Path::getFilenameExtensionInLowerCase(file));
|
|
|
|
|
|
|
|
if (map_it != _executableblocks.end()) {
|
|
|
|
start = map_it->second.start();
|
|
|
|
}
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string blockend(const std::string &file) const {
|
|
|
|
std::string end;
|
|
|
|
const std::map<std::string, CodeBlock>::const_iterator map_it
|
|
|
|
= _executableblocks.find(Path::getFilenameExtensionInLowerCase(file));
|
|
|
|
|
|
|
|
if (map_it != _executableblocks.end()) {
|
|
|
|
end = map_it->second.end();
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool iskeyword(const std::string &file, const std::string &keyword) const {
|
|
|
|
bool iskw;
|
|
|
|
const std::map<std::string, std::list<std::string> >::const_iterator it =
|
|
|
|
_keywords.find(Path::getFilenameExtensionInLowerCase(file));
|
|
|
|
|
|
|
|
if (it != _keywords.end()) {
|
|
|
|
const std::list<std::string> list = it->second;
|
|
|
|
const std::list<std::string>::const_iterator list_it =
|
|
|
|
std::find(list.begin(), list.end(), keyword);
|
|
|
|
iskw = list_it != list.end();
|
|
|
|
} else {
|
|
|
|
iskw = false;
|
|
|
|
}
|
|
|
|
return iskw;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isexporter(const std::string &prefix) const {
|
|
|
|
const std::map<std::string, ExportedFunctions>::const_iterator it =
|
|
|
|
_exporters.find(prefix);
|
|
|
|
return it != _exporters.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isexportedprefix(const std::string &prefix, const std::string &token) const {
|
|
|
|
const std::map<std::string, ExportedFunctions>::const_iterator it = _exporters.find(prefix);
|
|
|
|
if (it != _exporters.end()) {
|
|
|
|
return it->second.isPrefix(token);
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isexportedsuffix(const std::string &prefix, const std::string &token) const {
|
|
|
|
const std::map<std::string, ExportedFunctions>::const_iterator it = _exporters.find(prefix);
|
|
|
|
if (it != _exporters.end()) {
|
|
|
|
return it->second.isSuffix(token);
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isimporter(const std::string& file, const std::string &importer) const {
|
|
|
|
bool isImporter;
|
|
|
|
const std::map<std::string, std::list<std::string> >::const_iterator it =
|
|
|
|
_importers.find(Path::getFilenameExtensionInLowerCase(file));
|
|
|
|
|
|
|
|
if (it != _importers.end()) {
|
|
|
|
const std::list<std::string> list = it->second;
|
|
|
|
const std::list<std::string>::const_iterator it2 =
|
|
|
|
std::find(list.begin(), list.end(), importer);
|
|
|
|
isImporter = (it2 != list.end());
|
|
|
|
} else {
|
|
|
|
isImporter = false;
|
|
|
|
}
|
|
|
|
return isImporter;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isreflection(const std::string& file, const std::string &token) const {
|
|
|
|
bool isReflecMethod;
|
|
|
|
const std::map<std::string,std::map<std::string,int> >::const_iterator it
|
|
|
|
= _reflection.find(Path::getFilenameExtensionInLowerCase(file));
|
|
|
|
if (it != _reflection.end()) {
|
|
|
|
const std::map<std::string,int>::const_iterator it2 =
|
|
|
|
it->second.find(token);
|
|
|
|
isReflecMethod = it2 != it->second.end();
|
|
|
|
} else {
|
|
|
|
isReflecMethod = false;
|
|
|
|
}
|
|
|
|
return isReflecMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reflectionArgument(const std::string& file, const std::string &token) const {
|
|
|
|
int argIndex = -1;
|
|
|
|
const std::map<std::string,std::map<std::string,int> >::const_iterator it
|
|
|
|
= _reflection.find(Path::getFilenameExtensionInLowerCase(file));
|
|
|
|
if (it != _reflection.end()) {
|
|
|
|
const std::map<std::string,int>::const_iterator it2 =
|
|
|
|
it->second.find(token);
|
|
|
|
if (it2 != it->second.end()) {
|
|
|
|
argIndex = it2->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return argIndex;
|
|
|
|
}
|
|
|
|
|
2013-07-15 08:44:00 +02:00
|
|
|
std::set<std::string> returnuninitdata;
|
|
|
|
|
2013-07-02 07:18:19 +02:00
|
|
|
private:
|
2013-10-20 14:09:10 +02:00
|
|
|
class ExportedFunctions {
|
|
|
|
public:
|
|
|
|
void addPrefix(const std::string& prefix) {
|
|
|
|
_prefixes.push_back(prefix);
|
|
|
|
}
|
|
|
|
void addSuffix(const std::string& suffix) {
|
|
|
|
_suffixes.push_back(suffix);
|
|
|
|
}
|
|
|
|
bool isPrefix(const std::string& prefix) const {
|
|
|
|
return std::find(_prefixes.begin(), _prefixes.end(), prefix)
|
|
|
|
!= _prefixes.end();
|
|
|
|
}
|
|
|
|
bool isSuffix(const std::string& suffix) const {
|
|
|
|
return std::find(_suffixes.begin(), _suffixes.end(), suffix)
|
|
|
|
!= _suffixes.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::list<std::string> _prefixes;
|
|
|
|
std::list<std::string> _suffixes;
|
|
|
|
};
|
|
|
|
class CodeBlock {
|
|
|
|
public:
|
2013-10-23 15:03:31 +02:00
|
|
|
CodeBlock() : _offset(0) {}
|
|
|
|
|
2013-10-20 14:09:10 +02:00
|
|
|
void setStart(const std::string& s) {
|
|
|
|
_start = s;
|
|
|
|
}
|
|
|
|
void setEnd(const std::string& e) {
|
|
|
|
_end = e;
|
|
|
|
}
|
|
|
|
void setOffset(const int o) {
|
|
|
|
_offset = o;
|
|
|
|
}
|
|
|
|
void addBlock(const std::string& blockName) {
|
2013-10-23 15:03:31 +02:00
|
|
|
_blocks.insert(blockName);
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
|
|
|
std::string start() const {
|
|
|
|
return _start;
|
|
|
|
}
|
|
|
|
std::string end() const {
|
|
|
|
return _end;
|
|
|
|
}
|
|
|
|
int offset() const {
|
|
|
|
return _offset;
|
|
|
|
}
|
|
|
|
bool isBlock(const std::string& blockName) const {
|
2013-10-23 15:03:31 +02:00
|
|
|
return _blocks.find(blockName) != _blocks.end();
|
2013-10-20 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string _start;
|
|
|
|
std::string _end;
|
|
|
|
int _offset;
|
2013-10-23 15:03:31 +02:00
|
|
|
std::set<std::string> _blocks;
|
2013-10-20 14:09:10 +02:00
|
|
|
};
|
2013-07-02 07:18:19 +02:00
|
|
|
int allocid;
|
|
|
|
std::map<std::string, int> _alloc; // allocation functions
|
|
|
|
std::map<std::string, int> _dealloc; // deallocation functions
|
2013-07-14 10:10:11 +02:00
|
|
|
std::map<std::string, bool> _noreturn; // is function noreturn?
|
2013-10-20 14:09:10 +02:00
|
|
|
std::map<std::string, bool> _ignorefunction; // ignore functions/macros from a library (gtk, qt etc)
|
|
|
|
std::map<std::string, bool> _reporterrors;
|
2013-10-31 19:09:01 +01:00
|
|
|
std::set<std::string> _markupExtensions; // file extensions of markup files
|
2013-10-20 14:09:10 +02:00
|
|
|
std::map<std::string, std::list<std::string> > _keywords; // keywords for code in the library
|
|
|
|
std::map<std::string, CodeBlock> _executableblocks; // keywords for blocks of executable code
|
|
|
|
std::map<std::string, ExportedFunctions> _exporters; // keywords that export variables/functions to libraries (meta-code/macros)
|
|
|
|
std::map<std::string, std::list<std::string> > _importers; // keywords that import variables/functions
|
|
|
|
std::map<std::string,std::map<std::string,int> > _reflection; // invokation of reflection
|
|
|
|
|
2013-07-02 07:18:19 +02:00
|
|
|
|
2013-07-16 09:40:31 +02:00
|
|
|
const ArgumentChecks * getarg(const std::string &functionName, int argnr) const {
|
|
|
|
std::map<std::string, std::map<int, ArgumentChecks> >::const_iterator it1;
|
|
|
|
it1 = argumentChecks.find(functionName);
|
|
|
|
if (it1 != argumentChecks.end()) {
|
|
|
|
const std::map<int,ArgumentChecks>::const_iterator it2 = it1->second.find(argnr);
|
2013-07-15 21:58:29 +02:00
|
|
|
if (it2 != it1->second.end())
|
|
|
|
return &it2->second;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-15 18:55:40 +02:00
|
|
|
|
2013-07-30 12:52:27 +02:00
|
|
|
static int getid(const std::map<std::string,int> &data, const std::string &name) {
|
2013-07-02 07:18:19 +02:00
|
|
|
const std::map<std::string,int>::const_iterator it = data.find(name);
|
|
|
|
return (it == data.end()) ? 0 : it->second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2013-07-08 18:26:18 +02:00
|
|
|
#endif // libraryH
|