2010-11-13 08:08:45 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
|
2010-11-13 08:08:45 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef SymbolDatabaseH
|
|
|
|
#define SymbolDatabaseH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
2011-01-16 19:57:29 +01:00
|
|
|
#include <set>
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2011-01-18 07:32:06 +01:00
|
|
|
#include "token.h"
|
2011-06-23 04:41:11 +02:00
|
|
|
#include "mathlib.h"
|
2011-01-18 07:32:06 +01:00
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
class Tokenizer;
|
|
|
|
class Settings;
|
|
|
|
class ErrorLogger;
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
class Scope;
|
2011-01-17 07:21:59 +01:00
|
|
|
class SymbolDatabase;
|
|
|
|
|
|
|
|
/**
|
2011-01-18 07:32:06 +01:00
|
|
|
* @brief Access control enumerations.
|
2011-01-17 07:21:59 +01:00
|
|
|
*/
|
2012-01-26 04:05:29 +01:00
|
|
|
enum AccessControl { Public, Protected, Private, Global, Namespace, Argument, Local, Throw };
|
2011-01-17 07:21:59 +01:00
|
|
|
|
2011-06-23 04:41:11 +02:00
|
|
|
/**
|
|
|
|
* @brief Array dimension information.
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
struct Dimension {
|
2012-09-10 16:14:24 +02:00
|
|
|
Dimension() : start(NULL), end(NULL), num(0), known(true) { }
|
2011-08-28 19:32:42 +02:00
|
|
|
|
2011-06-23 04:41:11 +02:00
|
|
|
const Token *start; // size start token
|
|
|
|
const Token *end; // size end token
|
2013-02-10 07:43:09 +01:00
|
|
|
MathLib::bigint num; // (assumed) dimension length when size is a number, 0 if not known
|
2012-09-10 16:14:24 +02:00
|
|
|
bool known; // Known size
|
2011-06-23 04:41:11 +02:00
|
|
|
};
|
|
|
|
|
2013-03-05 13:33:38 +01:00
|
|
|
/** @brief Information about a class type. */
|
|
|
|
class CPPCHECKLIB Type {
|
|
|
|
public:
|
|
|
|
const Token* classDef; // Points to "class" token
|
|
|
|
const Scope* classScope;
|
|
|
|
const Scope* enclosingScope;
|
|
|
|
enum NeedInitialization {
|
|
|
|
Unknown, True, False
|
|
|
|
} needInitialization;
|
|
|
|
|
2013-03-05 15:28:40 +01:00
|
|
|
struct BaseInfo {
|
|
|
|
std::string name;
|
|
|
|
const Type* type;
|
|
|
|
const Token* nameTok;
|
|
|
|
AccessControl access; // public/protected/private
|
|
|
|
bool isVirtual;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FriendInfo {
|
|
|
|
const Token* nameStart;
|
|
|
|
const Token* nameEnd;
|
|
|
|
std::string name;
|
|
|
|
const Type* type;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<BaseInfo> derivedFrom;
|
|
|
|
std::list<FriendInfo> friendList;
|
|
|
|
|
2013-03-05 13:33:38 +01:00
|
|
|
Type(const Token* classDef_ = 0, const Scope* classScope_ = 0, const Scope* enclosingScope_ = 0) :
|
|
|
|
classDef(classDef_),
|
|
|
|
classScope(classScope_),
|
|
|
|
enclosingScope(enclosingScope_),
|
|
|
|
needInitialization(Unknown) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& name() const {
|
|
|
|
static const std::string empty;
|
|
|
|
return classDef->next()->isName() ? classDef->strAt(1) : empty;
|
|
|
|
}
|
2013-03-05 15:28:40 +01:00
|
|
|
|
|
|
|
const Token *initBaseInfo(const Token *tok, const Token *tok1);
|
2013-03-14 17:00:22 +01:00
|
|
|
|
|
|
|
const Function* getFunction(const std::string& funcName) const;
|
2013-03-05 13:33:38 +01:00
|
|
|
};
|
|
|
|
|
2011-01-17 07:21:59 +01:00
|
|
|
/** @brief Information about a member variable. */
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB Variable {
|
2011-01-18 07:32:06 +01:00
|
|
|
/** @brief flags mask used to access specific bit. */
|
2011-10-13 20:53:06 +02:00
|
|
|
enum {
|
2012-01-26 17:04:25 +01:00
|
|
|
fIsMutable = (1 << 0), /** @brief mutable variable */
|
|
|
|
fIsStatic = (1 << 1), /** @brief static variable */
|
|
|
|
fIsConst = (1 << 2), /** @brief const variable */
|
2012-06-22 11:23:50 +02:00
|
|
|
fIsExtern = (1 << 3), /** @brief extern variable */
|
|
|
|
fIsClass = (1 << 4), /** @brief user defined type */
|
|
|
|
fIsArray = (1 << 5), /** @brief array variable */
|
|
|
|
fIsPointer = (1 << 6), /** @brief pointer variable */
|
|
|
|
fIsReference = (1 << 7), /** @brief reference variable */
|
2013-04-04 18:47:44 +02:00
|
|
|
fIsRValueRef = (1 << 8), /** @brief rvalue reference variable */
|
|
|
|
fHasDefault = (1 << 9) /** @brief function argument with default value */
|
2011-01-18 07:32:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get specified flag state.
|
|
|
|
* @param flag_ flag to get state of
|
|
|
|
* @return true if flag set or false in flag not set
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool getFlag(int flag_) const {
|
2011-01-19 07:33:38 +01:00
|
|
|
return bool((_flags & flag_) != 0);
|
2011-01-18 07:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set specified flag state.
|
|
|
|
* @param flag_ flag to set state
|
|
|
|
* @param state_ new state of flag
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
void setFlag(int flag_, bool state_) {
|
2011-01-18 07:32:06 +01:00
|
|
|
_flags = state_ ? _flags | flag_ : _flags & ~flag_;
|
|
|
|
}
|
|
|
|
|
2012-05-11 17:56:47 +02:00
|
|
|
/**
|
|
|
|
* @brief parse and save array dimension information
|
|
|
|
* @param dimensions array dimensions vector
|
|
|
|
* @param tok the first '[' token of array declaration
|
|
|
|
* @return true if array, false if not
|
|
|
|
*/
|
|
|
|
static bool arrayDimensions(std::vector<Dimension> &dimensions, const Token *tok);
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
public:
|
2011-02-26 15:08:59 +01:00
|
|
|
Variable(const Token *name_, const Token *start_, const Token *end_,
|
2013-03-05 13:33:38 +01:00
|
|
|
std::size_t index_, AccessControl access_, const Type *type_,
|
2012-05-11 17:56:47 +02:00
|
|
|
const Scope *scope_)
|
2011-01-18 07:32:06 +01:00
|
|
|
: _name(name_),
|
2011-02-20 14:25:42 +01:00
|
|
|
_start(start_),
|
2011-02-26 15:08:59 +01:00
|
|
|
_end(end_),
|
2011-01-18 07:32:06 +01:00
|
|
|
_index(index_),
|
|
|
|
_access(access_),
|
|
|
|
_flags(0),
|
2011-02-26 15:08:59 +01:00
|
|
|
_type(type_),
|
2011-10-13 20:53:06 +02:00
|
|
|
_scope(scope_) {
|
2012-05-11 17:56:47 +02:00
|
|
|
evaluate();
|
2011-01-17 07:21:59 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
|
|
|
* Get name token.
|
|
|
|
* @return name token
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
const Token *nameToken() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return _name;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-02-20 14:25:42 +01:00
|
|
|
/**
|
|
|
|
* Get type start token.
|
|
|
|
* @return type start token
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
const Token *typeStartToken() const {
|
2011-02-20 14:25:42 +01:00
|
|
|
return _start;
|
|
|
|
}
|
|
|
|
|
2011-02-26 15:08:59 +01:00
|
|
|
/**
|
|
|
|
* Get type end token.
|
|
|
|
* @return type end token
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
const Token *typeEndToken() const {
|
2011-02-26 15:08:59 +01:00
|
|
|
return _end;
|
|
|
|
}
|
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
|
|
|
* Get name string.
|
|
|
|
* @return name string
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
const std::string &name() const {
|
2011-02-27 00:34:17 +01:00
|
|
|
static const std::string noname;
|
|
|
|
|
|
|
|
// name may not exist for function arguments
|
|
|
|
if (_name)
|
|
|
|
return _name->str();
|
|
|
|
|
|
|
|
return noname;
|
2011-01-18 07:32:06 +01:00
|
|
|
}
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
2013-07-20 12:31:04 +02:00
|
|
|
* Get declaration ID (varId used for variable in its declaration).
|
|
|
|
* @return declaration ID
|
2011-01-18 07:32:06 +01:00
|
|
|
*/
|
2013-07-20 12:31:04 +02:00
|
|
|
unsigned int declarationId() const {
|
2011-02-27 00:34:17 +01:00
|
|
|
// name may not exist for function arguments
|
|
|
|
if (_name)
|
|
|
|
return _name->varId();
|
|
|
|
|
|
|
|
return 0;
|
2011-01-18 07:32:06 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
|
|
|
* Get index of variable in declared order.
|
2013-02-10 07:43:09 +01:00
|
|
|
* @return variable index
|
2011-01-18 07:32:06 +01:00
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
std::size_t index() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return _index;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
|
|
|
* Is variable public.
|
|
|
|
* @return true if public, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isPublic() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return _access == Public;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is variable protected.
|
|
|
|
* @return true if protected, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isProtected() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return _access == Protected;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
|
|
|
* Is variable private.
|
|
|
|
* @return true if private, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isPrivate() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return _access == Private;
|
|
|
|
}
|
|
|
|
|
2011-02-26 15:08:59 +01:00
|
|
|
/**
|
|
|
|
* Is variable global.
|
|
|
|
* @return true if global, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isGlobal() const {
|
2011-02-26 15:08:59 +01:00
|
|
|
return _access == Global;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is variable in a namespace.
|
|
|
|
* @return true if in a namespace, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isNamespace() const {
|
2011-02-26 15:08:59 +01:00
|
|
|
return _access == Namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is variable a function argument.
|
|
|
|
* @return true if a function argument, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isArgument() const {
|
2011-02-26 15:08:59 +01:00
|
|
|
return _access == Argument;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is variable local.
|
|
|
|
* @return true if local, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isLocal() const {
|
2012-06-22 11:23:50 +02:00
|
|
|
return (_access == Local) && !isExtern();
|
2011-02-26 15:08:59 +01:00
|
|
|
}
|
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
|
|
|
* Is variable mutable.
|
|
|
|
* @return true if mutable, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isMutable() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return getFlag(fIsMutable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is variable static.
|
|
|
|
* @return true if static, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isStatic() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return getFlag(fIsStatic);
|
|
|
|
}
|
|
|
|
|
2012-06-22 11:23:50 +02:00
|
|
|
/**
|
|
|
|
* Is variable extern.
|
|
|
|
* @return true if extern, false if not
|
|
|
|
*/
|
|
|
|
bool isExtern() const {
|
|
|
|
return getFlag(fIsExtern);
|
|
|
|
}
|
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
|
|
|
* Is variable const.
|
|
|
|
* @return true if const, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isConst() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return getFlag(fIsConst);
|
|
|
|
}
|
|
|
|
|
2012-01-26 04:05:29 +01:00
|
|
|
/**
|
|
|
|
* Is variable a throw type.
|
|
|
|
* @return true if throw type, false if not
|
|
|
|
*/
|
|
|
|
bool isThrow() const {
|
|
|
|
return _access == Throw;
|
|
|
|
}
|
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
|
|
|
* Is variable a user defined (or unknown) type.
|
|
|
|
* @return true if user defined type, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isClass() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return getFlag(fIsClass);
|
|
|
|
}
|
|
|
|
|
2011-02-27 16:21:14 +01:00
|
|
|
/**
|
|
|
|
* Is variable an array.
|
|
|
|
* @return true if array, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isArray() const {
|
2011-02-27 16:21:14 +01:00
|
|
|
return getFlag(fIsArray);
|
|
|
|
}
|
|
|
|
|
2011-12-17 19:04:03 +01:00
|
|
|
/**
|
2012-01-26 17:04:25 +01:00
|
|
|
* Is pointer variable.
|
2011-12-17 19:04:03 +01:00
|
|
|
* @return true if pointer, false otherwise
|
|
|
|
*/
|
|
|
|
bool isPointer() const {
|
|
|
|
return getFlag(fIsPointer);
|
|
|
|
}
|
|
|
|
|
2012-01-26 17:04:25 +01:00
|
|
|
/**
|
|
|
|
* Is reference variable.
|
|
|
|
* @return true if reference, false otherwise
|
|
|
|
*/
|
|
|
|
bool isReference() const {
|
|
|
|
return getFlag(fIsReference);
|
|
|
|
}
|
|
|
|
|
2013-04-04 18:47:44 +02:00
|
|
|
/**
|
|
|
|
* Is reference variable.
|
|
|
|
* @return true if reference, false otherwise
|
|
|
|
*/
|
|
|
|
bool isRValueReference() const {
|
|
|
|
return getFlag(fIsRValueRef);
|
|
|
|
}
|
|
|
|
|
2011-03-31 03:59:43 +02:00
|
|
|
/**
|
|
|
|
* Does variable have a default value.
|
|
|
|
* @return true if has a default falue, false if not
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
bool hasDefault() const {
|
2011-03-31 03:59:43 +02:00
|
|
|
return getFlag(fHasDefault);
|
|
|
|
}
|
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/**
|
2013-03-05 13:33:38 +01:00
|
|
|
* Get Type pointer of known type.
|
2011-01-18 07:32:06 +01:00
|
|
|
* @return pointer to type if known, NULL if not known
|
|
|
|
*/
|
2013-03-05 13:33:38 +01:00
|
|
|
const Type *type() const {
|
2011-01-18 07:32:06 +01:00
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:33:38 +01:00
|
|
|
/**
|
|
|
|
* Get Scope pointer of known type.
|
|
|
|
* @return pointer to type scope if known, NULL if not known
|
|
|
|
*/
|
|
|
|
const Scope *typeScope() const {
|
|
|
|
return _type ? _type->classScope : 0;
|
|
|
|
}
|
|
|
|
|
2011-02-26 15:08:59 +01:00
|
|
|
/**
|
|
|
|
* Get Scope pointer of enclosing scope.
|
|
|
|
* @return pointer to enclosing scope
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
const Scope *scope() const {
|
2011-02-26 15:08:59 +01:00
|
|
|
return _scope;
|
|
|
|
}
|
|
|
|
|
2011-06-23 04:41:11 +02:00
|
|
|
/**
|
|
|
|
* Get array dimensions.
|
|
|
|
* @return array dimensions vector
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
const std::vector<Dimension> &dimensions() const {
|
2011-06-23 04:41:11 +02:00
|
|
|
return _dimensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get array dimension length.
|
|
|
|
* @return length of dimension
|
|
|
|
*/
|
2011-11-22 21:14:14 +01:00
|
|
|
MathLib::bigint dimension(std::size_t index_) const {
|
2011-06-23 04:41:11 +02:00
|
|
|
return _dimensions[index_].num;
|
|
|
|
}
|
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
private:
|
2013-08-12 06:21:03 +02:00
|
|
|
// only symbol database can change the type
|
|
|
|
friend class SymbolDatabase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Type pointer to known type.
|
|
|
|
* @param t type
|
|
|
|
*/
|
|
|
|
void type(const Type * t) {
|
|
|
|
_type = t;
|
|
|
|
}
|
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/** @brief variable name token */
|
|
|
|
const Token *_name;
|
|
|
|
|
2011-02-20 14:25:42 +01:00
|
|
|
/** @brief variable type start token */
|
|
|
|
const Token *_start;
|
|
|
|
|
2011-02-26 15:08:59 +01:00
|
|
|
/** @brief variable type end token */
|
|
|
|
const Token *_end;
|
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/** @brief order declared */
|
|
|
|
std::size_t _index;
|
|
|
|
|
|
|
|
/** @brief what section is this variable declared in? */
|
|
|
|
AccessControl _access; // public/protected/private
|
2010-11-16 07:30:55 +01:00
|
|
|
|
2011-01-18 07:32:06 +01:00
|
|
|
/** @brief flags */
|
|
|
|
int _flags;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-01-17 07:21:59 +01:00
|
|
|
/** @brief pointer to user defined type info (for known types) */
|
2013-03-05 13:33:38 +01:00
|
|
|
const Type *_type;
|
2011-02-26 15:08:59 +01:00
|
|
|
|
|
|
|
/** @brief pointer to scope this variable is in */
|
|
|
|
const Scope *_scope;
|
2011-06-23 04:41:11 +02:00
|
|
|
|
|
|
|
/** @brief array dimensions */
|
|
|
|
std::vector<Dimension> _dimensions;
|
2012-05-11 17:56:47 +02:00
|
|
|
|
2012-07-31 23:35:56 +02:00
|
|
|
/** @brief fill in information, depending on Tokens given at instantiation */
|
2012-05-11 17:56:47 +02:00
|
|
|
void evaluate();
|
2011-01-17 07:21:59 +01:00
|
|
|
};
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB Function {
|
2011-01-17 07:21:59 +01:00
|
|
|
public:
|
2013-04-04 19:53:55 +02:00
|
|
|
enum Type { eConstructor, eCopyConstructor, eMoveConstructor, eOperatorEqual, eDestructor, eFunction };
|
2011-01-17 07:21:59 +01:00
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
Function()
|
2011-01-17 07:21:59 +01:00
|
|
|
: tokenDef(NULL),
|
|
|
|
argDef(NULL),
|
|
|
|
token(NULL),
|
|
|
|
arg(NULL),
|
2012-05-14 20:46:23 +02:00
|
|
|
functionScope(NULL),
|
2012-05-24 17:40:43 +02:00
|
|
|
nestedIn(NULL),
|
2012-10-14 17:34:09 +02:00
|
|
|
initArgCount(0),
|
2012-05-14 20:46:23 +02:00
|
|
|
type(eFunction),
|
2011-01-17 07:21:59 +01:00
|
|
|
access(Public),
|
|
|
|
hasBody(false),
|
|
|
|
isInline(false),
|
|
|
|
isConst(false),
|
|
|
|
isVirtual(false),
|
|
|
|
isPure(false),
|
|
|
|
isStatic(false),
|
|
|
|
isFriend(false),
|
|
|
|
isExplicit(false),
|
2013-02-15 06:44:07 +01:00
|
|
|
isDefault(false),
|
|
|
|
isDelete(false),
|
2011-01-17 07:21:59 +01:00
|
|
|
isOperator(false),
|
2012-05-14 20:46:23 +02:00
|
|
|
retFuncPtr(false) {
|
2011-01-17 07:21:59 +01:00
|
|
|
}
|
|
|
|
|
2013-01-01 09:53:40 +01:00
|
|
|
const std::string &name() const {
|
|
|
|
return tokenDef->str();
|
|
|
|
}
|
|
|
|
|
2011-11-22 21:14:14 +01:00
|
|
|
std::size_t argCount() const {
|
2011-03-31 03:14:24 +02:00
|
|
|
return argumentList.size();
|
|
|
|
}
|
2013-02-27 06:59:04 +01:00
|
|
|
std::size_t minArgCount() const {
|
|
|
|
return argumentList.size() - initArgCount;
|
|
|
|
}
|
2012-05-03 13:29:41 +02:00
|
|
|
const Variable* getArgumentVar(unsigned int num) const;
|
2012-10-14 17:34:09 +02:00
|
|
|
unsigned int initializedArgCount() const {
|
|
|
|
return initArgCount;
|
|
|
|
}
|
2012-04-27 21:51:13 +02:00
|
|
|
void addArguments(const SymbolDatabase *symbolDatabase, const Scope *scope);
|
2012-04-17 19:50:44 +02:00
|
|
|
/** @brief check if this function is virtual in the base classes */
|
|
|
|
bool isImplicitlyVirtual(bool defaultVal = false) const;
|
2011-01-17 07:21:59 +01:00
|
|
|
|
2013-04-10 21:57:22 +02:00
|
|
|
bool isConstructor() const {
|
|
|
|
return type==eConstructor ||
|
|
|
|
type==eCopyConstructor ||
|
|
|
|
type==eMoveConstructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDestructor() const {
|
|
|
|
return type==eDestructor;
|
|
|
|
}
|
|
|
|
|
2011-01-17 07:21:59 +01:00
|
|
|
const Token *tokenDef; // function name token in class definition
|
|
|
|
const Token *argDef; // function argument start '(' in class definition
|
|
|
|
const Token *token; // function name token in implementation
|
|
|
|
const Token *arg; // function argument start '('
|
2013-03-05 18:42:42 +01:00
|
|
|
const Scope *functionScope; // scope of function body
|
|
|
|
const Scope* nestedIn; // Scope the function is declared in
|
2012-05-14 20:46:23 +02:00
|
|
|
std::list<Variable> argumentList; // argument list
|
2012-10-14 17:34:09 +02:00
|
|
|
unsigned int initArgCount; // number of args with default values
|
2012-05-14 20:46:23 +02:00
|
|
|
Type type; // constructor, destructor, ...
|
2011-01-17 07:21:59 +01:00
|
|
|
AccessControl access; // public/protected/private
|
|
|
|
bool hasBody; // has implementation
|
|
|
|
bool isInline; // implementation in class definition
|
|
|
|
bool isConst; // is const
|
|
|
|
bool isVirtual; // is virtual
|
|
|
|
bool isPure; // is pure virtual
|
|
|
|
bool isStatic; // is static
|
|
|
|
bool isFriend; // is friend
|
|
|
|
bool isExplicit; // is explicit
|
2013-02-15 06:44:07 +01:00
|
|
|
bool isDefault; // is default
|
|
|
|
bool isDelete; // is delete
|
2011-01-17 07:21:59 +01:00
|
|
|
bool isOperator; // is operator
|
|
|
|
bool retFuncPtr; // returns function pointer
|
2012-04-17 19:50:44 +02:00
|
|
|
|
2012-04-18 13:00:34 +02:00
|
|
|
static bool argsMatch(const Scope *info, const Token *first, const Token *second, const std::string &path, unsigned int depth);
|
|
|
|
|
2012-04-17 19:50:44 +02:00
|
|
|
private:
|
2013-03-05 15:28:40 +01:00
|
|
|
bool isImplicitlyVirtual_rec(const ::Type* type, bool& safe) const;
|
2011-01-17 07:21:59 +01:00
|
|
|
};
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB Scope {
|
2011-01-17 18:29:19 +01:00
|
|
|
// let tests access private function for testing
|
|
|
|
friend class TestSymbolDatabase;
|
|
|
|
|
2011-01-17 07:21:59 +01:00
|
|
|
public:
|
2012-12-20 06:53:04 +01:00
|
|
|
struct UsingInfo {
|
|
|
|
const Token *start;
|
2013-03-05 18:42:42 +01:00
|
|
|
const Scope *scope;
|
2012-12-20 06:53:04 +01:00
|
|
|
};
|
|
|
|
|
2012-01-26 04:05:29 +01:00
|
|
|
enum ScopeType { eGlobal, eClass, eStruct, eUnion, eNamespace, eFunction, eIf, eElse, eElseIf, eFor, eWhile, eDo, eSwitch, eUnconditional, eTry, eCatch };
|
2011-01-17 07:21:59 +01:00
|
|
|
|
2013-03-05 18:42:42 +01:00
|
|
|
Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope *nestedIn_);
|
|
|
|
Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_);
|
2011-01-17 07:21:59 +01:00
|
|
|
|
2013-03-05 18:42:42 +01:00
|
|
|
const SymbolDatabase *check;
|
2011-01-17 07:21:59 +01:00
|
|
|
std::string className;
|
|
|
|
const Token *classDef; // class/struct/union/namespace token
|
|
|
|
const Token *classStart; // '{' token
|
|
|
|
const Token *classEnd; // '}' token
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Function> functionList;
|
|
|
|
std::list<Variable> varlist;
|
2013-03-05 18:42:42 +01:00
|
|
|
const Scope *nestedIn;
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Scope *> nestedList;
|
2011-01-17 07:21:59 +01:00
|
|
|
unsigned int numConstructors;
|
2013-04-10 21:57:22 +02:00
|
|
|
unsigned int numCopyOrMoveConstructors;
|
2012-12-20 06:53:04 +01:00
|
|
|
std::list<UsingInfo> usingList;
|
2012-05-14 20:46:23 +02:00
|
|
|
ScopeType type;
|
2013-03-05 13:33:38 +01:00
|
|
|
Type* definedType;
|
|
|
|
std::list<Type*> definedTypes;
|
2011-03-14 02:01:33 +01:00
|
|
|
|
|
|
|
// function specific fields
|
2013-03-05 18:42:42 +01:00
|
|
|
const Scope *functionOf; // scope this function belongs to
|
2011-03-14 02:18:49 +01:00
|
|
|
Function *function; // function info for this function
|
2011-01-17 07:21:59 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isClassOrStruct() const {
|
2011-01-17 18:29:19 +01:00
|
|
|
return (type == eClass || type == eStruct);
|
2011-01-17 07:21:59 +01:00
|
|
|
}
|
|
|
|
|
2012-09-02 14:30:00 +02:00
|
|
|
bool isExecutable() const {
|
|
|
|
return type != eClass && type != eStruct && type != eUnion && type != eGlobal && type != eNamespace;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isLocal() const {
|
2011-02-26 21:53:57 +01:00
|
|
|
return (type == eIf || type == eElse || type == eElseIf ||
|
|
|
|
type == eFor || type == eWhile || type == eDo ||
|
2012-02-02 16:17:42 +01:00
|
|
|
type == eSwitch || type == eUnconditional ||
|
|
|
|
type == eTry || type == eCatch);
|
2011-02-26 21:53:57 +01:00
|
|
|
}
|
2011-03-12 17:42:58 +01:00
|
|
|
|
2013-01-28 06:47:48 +01:00
|
|
|
/**
|
|
|
|
* @brief find a function
|
|
|
|
* @param tok token of function call
|
|
|
|
* @return pointer to function if found or NULL if not found
|
|
|
|
*/
|
|
|
|
const Function *findFunction(const Token *tok) const;
|
|
|
|
|
2011-01-17 07:21:59 +01:00
|
|
|
/**
|
|
|
|
* @brief find if name is in nested list
|
2011-01-21 07:42:41 +01:00
|
|
|
* @param name name of nested scope
|
2011-01-17 07:21:59 +01:00
|
|
|
*/
|
2013-01-03 22:37:19 +01:00
|
|
|
Scope *findInNestedList(const std::string & name);
|
2011-01-17 07:21:59 +01:00
|
|
|
|
2013-01-03 22:37:19 +01:00
|
|
|
const Scope *findRecordInNestedList(const std::string & name) const;
|
|
|
|
Scope *findRecordInNestedList(const std::string & name) {
|
|
|
|
return const_cast<Scope *>(static_cast<const Scope *>(this)->findRecordInNestedList(name));
|
|
|
|
}
|
2012-11-30 06:03:58 +01:00
|
|
|
|
2013-03-05 15:28:40 +01:00
|
|
|
const Type* findType(const std::string& name) const;
|
|
|
|
Type* findType(const std::string& name) {
|
|
|
|
return const_cast<Type*>(static_cast<const Scope *>(this)->findType(name));
|
|
|
|
}
|
2013-03-05 13:33:38 +01:00
|
|
|
|
2011-02-03 07:57:10 +01:00
|
|
|
/**
|
|
|
|
* @brief find if name is in nested list
|
|
|
|
* @param name name of nested scope
|
|
|
|
*/
|
2013-01-03 22:37:19 +01:00
|
|
|
Scope *findInNestedListRecursive(const std::string & name);
|
2011-02-03 07:57:10 +01:00
|
|
|
|
2011-02-26 15:08:59 +01:00
|
|
|
void addVariable(const Token *token_, const Token *start_,
|
2013-03-05 13:33:38 +01:00
|
|
|
const Token *end_, AccessControl access_, const Type *type_,
|
2012-05-11 17:56:47 +02:00
|
|
|
const Scope *scope_) {
|
2011-02-26 15:08:59 +01:00
|
|
|
varlist.push_back(Variable(token_, start_, end_, varlist.size(),
|
2012-05-11 17:56:47 +02:00
|
|
|
access_,
|
|
|
|
type_, scope_));
|
2011-01-17 07:21:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief initialize varlist */
|
2011-01-17 18:29:19 +01:00
|
|
|
void getVariableList();
|
2011-01-17 07:21:59 +01:00
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
const Function *getDestructor() const;
|
2011-01-17 07:21:59 +01:00
|
|
|
|
|
|
|
/**
|
2011-01-21 07:42:41 +01:00
|
|
|
* @brief get the number of nested scopes that are not functions
|
2011-01-17 07:21:59 +01:00
|
|
|
*
|
|
|
|
* This returns the number of user defined types (class, struct, union)
|
|
|
|
* that are defined in this user defined type or namespace.
|
|
|
|
*/
|
|
|
|
unsigned int getNestedNonFunctions() const;
|
|
|
|
|
|
|
|
bool hasDefaultConstructor() const;
|
|
|
|
|
2011-02-26 15:08:59 +01:00
|
|
|
AccessControl defaultAccess() const;
|
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
/**
|
|
|
|
* @brief check if statement is variable declaration and add it if it is
|
|
|
|
* @param tok pointer to start of statement
|
|
|
|
* @param varaccess access control of statement
|
|
|
|
* @return pointer to last token
|
|
|
|
*/
|
|
|
|
const Token *checkVariable(const Token *tok, AccessControl varaccess);
|
|
|
|
|
2011-09-03 03:07:29 +02:00
|
|
|
/**
|
|
|
|
* @brief get variable from name
|
|
|
|
* @param varname name of variable
|
|
|
|
* @return pointer to variable
|
|
|
|
*/
|
|
|
|
const Variable *getVariable(const std::string &varname) const;
|
|
|
|
|
2011-01-17 07:21:59 +01:00
|
|
|
private:
|
|
|
|
/**
|
2011-01-17 18:29:19 +01:00
|
|
|
* @brief helper function for getVariableList()
|
2011-01-17 07:21:59 +01:00
|
|
|
* @param tok pointer to token to check
|
|
|
|
* @param vartok populated with pointer to the variable token, if found
|
|
|
|
* @param typetok populated with pointer to the type token, if found
|
|
|
|
* @return true if tok points to a variable declaration, false otherwise
|
|
|
|
*/
|
2012-05-11 17:56:47 +02:00
|
|
|
bool isVariableDeclaration(const Token* tok, const Token*& vartok, const Token*& typetok) const;
|
2011-01-17 07:21:59 +01:00
|
|
|
};
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB SymbolDatabase {
|
2011-01-17 07:21:59 +01:00
|
|
|
public:
|
|
|
|
SymbolDatabase(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
/** @brief Information about all namespaces/classes/structrues */
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope> scopeList;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2012-10-08 16:15:07 +02:00
|
|
|
/** @brief Fast access to function scopes */
|
2012-10-11 06:12:24 +02:00
|
|
|
std::vector<const Scope *> functionScopes;
|
2012-10-10 20:42:07 +02:00
|
|
|
|
|
|
|
/** @brief Fast access to class and struct scopes */
|
2012-10-11 06:12:24 +02:00
|
|
|
std::vector<const Scope *> classAndStructScopes;
|
2012-10-08 16:15:07 +02:00
|
|
|
|
2013-03-05 13:33:38 +01:00
|
|
|
/** @brief Fast access to types */
|
|
|
|
std::list<Type> typeList;
|
|
|
|
|
2010-12-21 08:13:40 +01:00
|
|
|
/**
|
|
|
|
* @brief find a variable type if it's a user defined type
|
|
|
|
* @param start scope to start looking in
|
|
|
|
* @param type token containing variable type
|
|
|
|
* @return pointer to type if found or NULL if not found
|
|
|
|
*/
|
2013-03-05 13:33:38 +01:00
|
|
|
const Type *findVariableType(const Scope *start, const Token *type) const;
|
2010-12-21 08:13:40 +01:00
|
|
|
|
2012-10-14 17:30:37 +02:00
|
|
|
/**
|
2013-01-28 06:47:48 +01:00
|
|
|
* @brief find a function
|
2012-10-14 17:30:37 +02:00
|
|
|
* @param tok token of function call
|
|
|
|
* @return pointer to function if found or NULL if not found
|
|
|
|
*/
|
2013-01-28 06:47:48 +01:00
|
|
|
const Function *findFunction(const Token *tok) const;
|
2013-01-01 09:53:40 +01:00
|
|
|
|
2013-01-03 22:37:19 +01:00
|
|
|
const Scope *findScopeByName(const std::string& name) const;
|
2012-02-24 20:45:56 +01:00
|
|
|
|
2013-03-05 15:28:40 +01:00
|
|
|
const Type* findType(const Token *tok, const Scope *startScope) const;
|
|
|
|
Type* findType(const Token *tok, Scope *startScope) const {
|
|
|
|
return const_cast<Type*>(this->findType(tok, static_cast<const Scope *>(startScope)));
|
2013-03-05 13:33:38 +01:00
|
|
|
}
|
|
|
|
|
2012-11-30 06:03:58 +01:00
|
|
|
const Scope *findScope(const Token *tok, const Scope *startScope) const;
|
2013-01-03 22:37:19 +01:00
|
|
|
Scope *findScope(const Token *tok, Scope *startScope) const {
|
|
|
|
return const_cast<Scope *>(this->findScope(tok, static_cast<const Scope *>(startScope)));
|
|
|
|
}
|
2012-11-30 06:03:58 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
bool isClassOrStruct(const std::string &type) const {
|
2013-03-05 13:33:38 +01:00
|
|
|
for (std::list<Type>::const_iterator i = typeList.begin(); i != typeList.end(); ++i)
|
|
|
|
if (i->name() == type)
|
|
|
|
return true;
|
|
|
|
return false;
|
2011-01-16 19:57:29 +01:00
|
|
|
}
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
const Variable *getVariableFromVarId(std::size_t varId) const {
|
2011-02-26 21:57:16 +01:00
|
|
|
return _variableList[varId];
|
|
|
|
}
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t getVariableListSize() const {
|
2012-01-26 04:48:18 +01:00
|
|
|
return _variableList.size();
|
|
|
|
}
|
|
|
|
|
2011-03-03 03:08:27 +01:00
|
|
|
/**
|
|
|
|
* @brief output a debug message
|
|
|
|
*/
|
|
|
|
void debugMessage(const Token *tok, const std::string &msg) const;
|
|
|
|
|
2012-01-05 18:22:54 +01:00
|
|
|
void printOut(const char * title = NULL) const;
|
|
|
|
void printVariable(const Variable *var, const char *indent) const;
|
|
|
|
|
2012-11-26 16:34:44 +01:00
|
|
|
bool isCPP() const;
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
private:
|
2010-12-31 10:24:51 +01:00
|
|
|
|
|
|
|
// Needed by Borland C++:
|
2011-01-17 18:29:19 +01:00
|
|
|
friend class Scope;
|
2010-12-31 10:24:51 +01:00
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
void addClassFunction(Scope **info, const Token **tok, const Token *argStart);
|
2013-01-03 22:37:19 +01:00
|
|
|
Function *addGlobalFunctionDecl(Scope*& scope, const Token *argStart, const Token* funcStart);
|
|
|
|
Function *addGlobalFunction(Scope*& scope, const Token*& tok, const Token *argStart, const Token* funcStart);
|
2011-01-17 18:29:19 +01:00
|
|
|
void addNewFunction(Scope **info, const Token **tok);
|
2012-09-01 10:46:09 +02:00
|
|
|
static bool isFunction(const Token *tok, const Scope* outerScope, const Token **funcStart, const Token **argStart);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
const Tokenizer *_tokenizer;
|
|
|
|
const Settings *_settings;
|
|
|
|
ErrorLogger *_errorLogger;
|
2011-02-26 21:57:16 +01:00
|
|
|
|
|
|
|
/** variable symbol table */
|
|
|
|
std::vector<const Variable *> _variableList;
|
2013-07-08 11:45:26 +02:00
|
|
|
|
|
|
|
/** list for missing types */
|
|
|
|
std::list<Type> _blankTypes;
|
2010-11-13 08:08:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|