cppcheck/lib/library.h

524 lines
18 KiB
C
Raw Normal View History

2013-07-02 07:18:19 +02:00
/*
* Cppcheck - A tool for static C/C++ code analysis
2016-01-01 14:34:45 +01:00
* Copyright (C) 2007-2016 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/>.
*/
//---------------------------------------------------------------------------
#ifndef libraryH
#define libraryH
//---------------------------------------------------------------------------
2013-07-02 07:18:19 +02:00
2013-07-04 13:09:34 +02:00
#include "config.h"
#include "mathlib.h"
#include "standards.h"
#include "errorlogger.h"
2013-10-27 17:10:43 +01:00
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 <vector>
2013-10-20 14:09:10 +02:00
namespace tinyxml2 {
class XMLDocument;
class XMLElement;
}
2013-07-02 07:18:19 +02:00
/// @addtogroup Core
/// @{
/**
* @brief Library definitions handling
2013-07-02 07:18:19 +02:00
*/
class CPPCHECKLIB Library {
friend class TestSymbolDatabase; // For testing only
2013-07-02 07:18:19 +02:00
public:
Library();
2013-07-02 07:18:19 +02:00
enum ErrorCode { OK, FILE_NOT_FOUND, BAD_XML, UNKNOWN_ELEMENT, MISSING_ATTRIBUTE, BAD_ATTRIBUTE_VALUE, UNSUPPORTED_FORMAT, DUPLICATE_PLATFORM_TYPE, PLATFORM_TYPE_REDEFINED };
class Error {
public:
2016-12-06 12:31:16 +01:00
Error() : errorcode(OK) {}
explicit Error(ErrorCode e) : errorcode(e) {}
template<typename T>
Error(ErrorCode e, T&& r) : errorcode(e), reason(r) {}
ErrorCode errorcode;
std::string reason;
};
Error load(const char exename [], const char path []);
Error load(const tinyxml2::XMLDocument &doc);
/** this is primarily meant for unit tests. it only returns true/false */
bool loadxmldata(const char xmldata[], std::size_t len);
2013-07-02 07:18:19 +02:00
struct AllocFunc {
int groupId;
int arg;
};
/** get allocation info for function */
const AllocFunc* alloc(const Token *tok) const;
/** get deallocation info for function */
const AllocFunc* dealloc(const Token *tok) const;
2013-07-02 07:18:19 +02:00
/** get allocation id for function */
int alloc(const Token *tok, int arg) const;
/** get deallocation id for function */
int dealloc(const Token *tok, int arg) const;
/** get allocation info for function by name (deprecated, use other alloc) */
const AllocFunc* alloc(const char name[]) const {
return getAllocDealloc(_alloc, name);
}
/** get deallocation info for function by name (deprecated, use other alloc) */
const AllocFunc* dealloc(const char name[]) const {
return getAllocDealloc(_dealloc, name);
}
/** get allocation id for function by name (deprecated, use other alloc) */
int allocId(const char name[]) const {
const AllocFunc* af = getAllocDealloc(_alloc, name);
return af ? af->groupId : 0;
}
/** get deallocation id for function by name (deprecated, use other alloc) */
int deallocId(const char name[]) const {
const AllocFunc* af = getAllocDealloc(_dealloc, name);
return af ? af->groupId : 0;
2013-07-02 07:18:19 +02:00
}
/** set allocation id for function */
void setalloc(const std::string &functionname, int id, int arg) {
_alloc[functionname].groupId = id;
_alloc[functionname].arg = arg;
}
void setdealloc(const std::string &functionname, int id, int arg) {
_dealloc[functionname].groupId = id;
_dealloc[functionname].arg = arg;
}
/** add noreturn function setting */
2014-11-20 14:20:09 +01:00
void setnoreturn(const std::string& funcname, bool noreturn) {
_noreturn[funcname] = noreturn;
}
2013-07-02 07:18:19 +02:00
/** is allocation type memory? */
2014-11-20 14:20:09 +01:00
static bool ismemory(int id) {
2013-07-02 07:18:19 +02:00
return ((id > 0) && ((id & 1) == 0));
}
static bool ismemory(const AllocFunc* func) {
return ((func->groupId > 0) && ((func->groupId & 1) == 0));
}
2013-07-02 07:18:19 +02:00
/** is allocation type resource? */
2014-11-20 14:20:09 +01:00
static bool isresource(int id) {
2013-07-02 07:18:19 +02:00
return ((id > 0) && ((id & 1) == 1));
}
static bool isresource(const AllocFunc* func) {
return ((func->groupId > 0) && ((func->groupId & 1) == 1));
}
2013-07-02 07:18:19 +02:00
bool formatstr_function(const Token* ftok) const;
int formatstr_argno(const Token* ftok) const;
bool formatstr_scan(const Token* ftok) const;
bool formatstr_secure(const Token* ftok) const;
struct WarnInfo {
std::string message;
Standards standards;
Severity::SeverityType severity;
};
std::map<std::string, WarnInfo> functionwarn;
const WarnInfo* getWarnInfo(const Token* ftok) const;
// returns true if ftok is not a library function
bool isNotLibraryFunction(const Token *ftok) const;
bool matchArguments(const Token *ftok, const std::string &functionName) const;
bool isUseRetVal(const Token* ftok) const;
const std::string& returnValue(const Token *ftok) const;
const std::string& returnValueType(const Token *ftok) const;
int returnValueContainer(const Token *ftok) const;
bool isnoreturn(const Token *ftok) const;
bool isnotnoreturn(const Token *ftok) const;
bool isScopeNoReturn(const Token *end, std::string *unknownFunc) const;
class Container {
public:
Container() :
type_templateArgNo(-1),
size_templateArgNo(-1),
arrayLike_indexOp(false),
stdStringLike(false),
opLessAllowed(true) {
}
enum Action {
RESIZE, CLEAR, PUSH, POP, FIND, INSERT, ERASE, CHANGE_CONTENT, CHANGE, CHANGE_INTERNAL,
NO_ACTION
};
enum Yield {
AT_INDEX, ITEM, BUFFER, BUFFER_NT, START_ITERATOR, END_ITERATOR, ITERATOR, SIZE, EMPTY,
NO_YIELD
};
struct Function {
Action action;
Yield yield;
};
std::string startPattern, endPattern, itEndPattern;
std::map<std::string, Function> functions;
int type_templateArgNo;
int size_templateArgNo;
bool arrayLike_indexOp;
bool stdStringLike;
bool opLessAllowed;
Action getAction(const std::string& function) const {
std::map<std::string, Function>::const_iterator i = functions.find(function);
if (i != functions.end())
return i->second.action;
return NO_ACTION;
}
Yield getYield(const std::string& function) const {
std::map<std::string, Function>::const_iterator i = functions.find(function);
if (i != functions.end())
return i->second.yield;
return NO_YIELD;
}
};
std::map<std::string, Container> containers;
const Container* detectContainer(const Token* typeStart, bool iterator = false) const;
class ArgumentChecks {
public:
ArgumentChecks() :
notbool(false),
notnull(false),
notuninit(false),
formatstr(false),
strz(false),
optional(false),
iteratorInfo() {
}
bool notbool;
bool notnull;
bool notuninit;
bool formatstr;
bool strz;
bool optional;
std::string valid;
class IteratorInfo {
public:
IteratorInfo() : it(false), container(0), first(false), last(false) {}
bool it;
int container;
bool first;
bool last;
};
IteratorInfo iteratorInfo;
class MinSize {
public:
2016-12-06 12:31:16 +01:00
enum Type { NONE, STRLEN, ARGVALUE, SIZEOF, MUL };
MinSize(Type t, int a) : type(t), arg(a), arg2(0) {}
Type type;
int arg;
int arg2;
};
std::list<MinSize> minsizes;
};
2016-12-06 12:31:16 +01:00
struct Function {
std::map<int, ArgumentChecks> argumentChecks; // argument nr => argument data
bool use;
bool leakignore;
bool isconst;
bool ispure;
bool useretval;
bool ignore; // ignore functions/macros from a library (gtk, qt etc)
bool formatstr;
bool formatstr_scan;
bool formatstr_secure;
Function() : use(false), leakignore(false), isconst(false), ispure(false), useretval(false), ignore(false), formatstr(false), formatstr_scan(false), formatstr_secure(false) {}
};
std::map<std::string, Function> functions;
bool isUse(const std::string& functionName) const;
bool isLeakIgnore(const std::string& functionName) const;
bool isFunctionConst(const std::string& functionName, bool pure) const;
2013-07-02 07:18:19 +02:00
bool isboolargbad(const Token *ftok, int argnr) const {
const ArgumentChecks *arg = getarg(ftok, argnr);
return arg && arg->notbool;
}
bool isnullargbad(const Token *ftok, int argnr) const;
bool isuninitargbad(const Token *ftok, int argnr) const;
bool isargformatstr(const Token *ftok, int argnr) const {
const ArgumentChecks *arg = getarg(ftok, argnr);
return arg && arg->formatstr;
}
bool isargstrz(const Token *ftok, int argnr) const {
const ArgumentChecks *arg = getarg(ftok, argnr);
return arg && arg->strz;
}
bool isargvalid(const Token *ftok, int argnr, const MathLib::bigint argvalue) const;
const std::string& validarg(const Token *ftok, int argnr) const {
const ArgumentChecks *arg = getarg(ftok, argnr);
return arg ? arg->valid : emptyString;
}
const ArgumentChecks::IteratorInfo *getArgIteratorInfo(const Token *ftok, int argnr) const {
const ArgumentChecks *arg = getarg(ftok, argnr);
return arg && arg->iteratorInfo.it ? &arg->iteratorInfo : nullptr;
}
2016-12-06 12:31:16 +01:00
bool hasminsize(const std::string &functionName) const;
const std::list<ArgumentChecks::MinSize> *argminsizes(const Token *ftok, int argnr) const {
const ArgumentChecks *arg = getarg(ftok, argnr);
return arg ? &arg->minsizes : nullptr;
}
bool markupFile(const std::string &path) const;
bool processMarkupAfterCode(const std::string &path) const;
2014-01-02 18:18:24 +01:00
2014-11-20 14:20:09 +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;
2013-10-20 14:09:10 +02:00
2016-12-06 12:31:16 +01:00
bool ignorefunction(const std::string &function) const;
2013-10-20 14:09:10 +02:00
bool isexecutableblock(const std::string &file, const std::string &token) const;
2013-10-20 14:09:10 +02:00
int blockstartoffset(const std::string &file) const;
2013-10-20 14:09:10 +02:00
const std::string& blockstart(const std::string &file) const;
const std::string& blockend(const std::string &file) const;
2013-10-20 14:09:10 +02:00
bool iskeyword(const std::string &file, const std::string &keyword) const;
2013-10-20 14:09:10 +02:00
2014-11-20 14:20:09 +01:00
bool isexporter(const std::string &prefix) const {
2013-12-22 17:03:46 +01:00
return _exporters.find(prefix) != _exporters.end();
2013-10-20 14:09:10 +02:00
}
2014-11-20 14:20:09 +01:00
bool isexportedprefix(const std::string &prefix, const std::string &token) const {
2013-10-20 14:09:10 +02:00
const std::map<std::string, ExportedFunctions>::const_iterator it = _exporters.find(prefix);
2013-12-22 17:03:46 +01:00
return (it != _exporters.end() && it->second.isPrefix(token));
2013-10-20 14:09:10 +02:00
}
2014-11-20 14:20:09 +01:00
bool isexportedsuffix(const std::string &prefix, const std::string &token) const {
2013-10-20 14:09:10 +02:00
const std::map<std::string, ExportedFunctions>::const_iterator it = _exporters.find(prefix);
2013-12-22 17:03:46 +01:00
return (it != _exporters.end() && it->second.isSuffix(token));
2013-10-20 14:09:10 +02:00
}
bool isimporter(const std::string& file, const std::string &importer) const;
2013-10-20 14:09:10 +02:00
2014-11-20 14:20:09 +01:00
bool isreflection(const std::string &token) const {
const std::map<std::string,int>::const_iterator it
= _reflection.find(token);
return it != _reflection.end();
2013-10-20 14:09:10 +02:00
}
2014-11-20 14:20:09 +01:00
int reflectionArgument(const std::string &token) const {
2013-10-20 14:09:10 +02:00
int argIndex = -1;
const std::map<std::string,int>::const_iterator it
= _reflection.find(token);
2013-10-20 14:09:10 +02:00
if (it != _reflection.end()) {
argIndex = it->second;
2013-10-20 14:09:10 +02:00
}
return argIndex;
}
std::set<std::string> returnuninitdata;
std::vector<std::string> defines; // to provide some library defines
struct PodType {
unsigned int size;
char sign;
};
const struct PodType *podtype(const std::string &name) const {
const std::map<std::string, struct PodType>::const_iterator it = podtypes.find(name);
return (it != podtypes.end()) ? &(it->second) : nullptr;
}
struct PlatformType {
PlatformType()
: _signed(false)
, _unsigned(false)
, _long(false)
, _pointer(false)
, _ptr_ptr(false)
2014-11-20 14:20:09 +01:00
, _const_ptr(false) {
}
2014-11-20 14:20:09 +01:00
bool operator == (const PlatformType & type) const {
2016-12-06 12:31:16 +01:00
return (_signed == type._signed &&
_unsigned == type._unsigned &&
_long == type._long &&
_pointer == type._pointer &&
_ptr_ptr == type._ptr_ptr &&
2016-12-06 12:31:16 +01:00
_const_ptr == type._const_ptr &&
_type == type._type);
}
2014-11-20 14:20:09 +01:00
bool operator != (const PlatformType & type) const {
return !(*this == type);
}
std::string _type;
bool _signed;
bool _unsigned;
bool _long;
bool _pointer;
bool _ptr_ptr;
bool _const_ptr;
};
struct Platform {
2014-11-20 14:20:09 +01:00
const PlatformType *platform_type(const std::string &name) const {
const std::map<std::string, struct PlatformType>::const_iterator it = _platform_types.find(name);
return (it != _platform_types.end()) ? &(it->second) : nullptr;
}
std::map<std::string, PlatformType> _platform_types;
};
2014-11-20 14:20:09 +01:00
const PlatformType *platform_type(const std::string &name, const std::string & platform) const {
const std::map<std::string, Platform>::const_iterator it = platforms.find(platform);
if (it != platforms.end()) {
const PlatformType * const type = it->second.platform_type(name);
if (type)
return type;
}
const std::map<std::string, PlatformType>::const_iterator it2 = platform_types.find(name);
return (it2 != platform_types.end()) ? &(it2->second) : nullptr;
}
2013-07-02 07:18:19 +02:00
private:
// load a <function> xml node
Error loadFunction(const tinyxml2::XMLElement * const node, const std::string &name, std::set<std::string> &unknown_elements);
2013-10-20 14:09:10 +02:00
class ExportedFunctions {
public:
2014-11-20 14:20:09 +01:00
void addPrefix(const std::string& prefix) {
2013-12-22 17:03:46 +01:00
_prefixes.insert(prefix);
2013-10-20 14:09:10 +02:00
}
2014-11-20 14:20:09 +01:00
void addSuffix(const std::string& suffix) {
2013-12-22 17:03:46 +01:00
_suffixes.insert(suffix);
2013-10-20 14:09:10 +02:00
}
2014-11-20 14:20:09 +01:00
bool isPrefix(const std::string& prefix) const {
2013-12-22 17:03:46 +01:00
return (_prefixes.find(prefix) != _prefixes.end());
2013-10-20 14:09:10 +02:00
}
2014-11-20 14:20:09 +01:00
bool isSuffix(const std::string& suffix) const {
2013-12-22 17:03:46 +01:00
return (_suffixes.find(suffix) != _suffixes.end());
2013-10-20 14:09:10 +02:00
}
private:
2013-12-22 17:03:46 +01:00
std::set<std::string> _prefixes;
std::set<std::string> _suffixes;
2013-10-20 14:09:10 +02:00
};
class CodeBlock {
public:
2013-10-23 15:03:31 +02:00
CodeBlock() : _offset(0) {}
2014-11-20 14:20:09 +01:00
void setStart(const std::string& s) {
2013-10-20 14:09:10 +02:00
_start = s;
}
2014-11-20 14:20:09 +01:00
void setEnd(const std::string& e) {
2013-10-20 14:09:10 +02:00
_end = e;
}
2014-11-20 14:20:09 +01:00
void setOffset(const int o) {
2013-10-20 14:09:10 +02:00
_offset = o;
}
2014-11-20 14:20:09 +01:00
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
}
2014-11-20 14:20:09 +01:00
const std::string& start() const {
2013-10-20 14:09:10 +02:00
return _start;
}
2014-11-20 14:20:09 +01:00
const std::string& end() const {
2013-10-20 14:09:10 +02:00
return _end;
}
2014-11-20 14:20:09 +01:00
int offset() const {
2013-10-20 14:09:10 +02:00
return _offset;
}
2014-11-20 14:20:09 +01:00
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::set<std::string> _files;
std::map<std::string, AllocFunc> _alloc; // allocation functions
std::map<std::string, AllocFunc> _dealloc; // deallocation functions
std::set<std::string> _functions;
std::map<std::string, bool> _noreturn; // is function noreturn?
std::map<std::string, std::string> _returnValue;
std::map<std::string, std::string> _returnValueType;
std::map<std::string, int> _returnValueContainer;
2013-10-20 14:09:10 +02:00
std::map<std::string, bool> _reporterrors;
2014-01-02 18:18:24 +01:00
std::map<std::string, bool> _processAfterCode;
std::set<std::string> _markupExtensions; // file extensions of markup files
2013-12-22 17:03:46 +01:00
std::map<std::string, std::set<std::string> > _keywords; // keywords for code in the library
2013-10-20 14:09:10 +02:00
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)
2013-12-22 17:03:46 +01:00
std::map<std::string, std::set<std::string> > _importers; // keywords that import variables/functions
2016-12-06 12:31:16 +01:00
std::map<std::string, int> _reflection; // invocation of reflection
std::map<std::string, struct PodType> podtypes; // pod types
std::map<std::string, PlatformType> platform_types; // platform independent typedefs
std::map<std::string, Platform> platforms; // platform dependent typedefs
2013-07-02 07:18:19 +02:00
const ArgumentChecks * getarg(const Token *ftok, int argnr) const;
std::string getFunctionName(const Token *ftok, bool *error) const;
std::string getFunctionName(const Token *ftok) const;
static const AllocFunc* getAllocDealloc(const std::map<std::string, AllocFunc> &data, const std::string &name) {
const std::map<std::string, AllocFunc>::const_iterator it = data.find(name);
return (it == data.end()) ? nullptr : &it->second;
2013-07-02 07:18:19 +02:00
}
};
/// @}
//---------------------------------------------------------------------------
#endif // libraryH