2010-11-13 08:08:45 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "symboldatabase.h"
|
|
|
|
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "errorlogger.h"
|
|
|
|
#include "check.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2011-06-24 04:35:15 +02:00
|
|
|
#include <climits>
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-10-16 07:52:54 +02:00
|
|
|
// Define ULLONG_MAX and LLONG_MAX for Borland
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#define ULLONG_MAX ULONG_MAX
|
|
|
|
#define LLONG_MAX LONG_MAX
|
|
|
|
#endif
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
: _tokenizer(tokenizer), _settings(settings), _errorLogger(errorLogger)
|
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
// create global scope
|
|
|
|
scopeList.push_back(Scope(this, NULL, NULL));
|
|
|
|
|
|
|
|
// pointer to current scope
|
|
|
|
Scope *scope = &scopeList.back();
|
|
|
|
|
|
|
|
// find all scopes
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
2010-11-13 08:08:45 +01:00
|
|
|
// Locate next class
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok, "class|struct|union|namespace %var% [{:]")) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scopeList.push_back(Scope(this, tok, scope));
|
|
|
|
|
|
|
|
Scope *new_scope = &scopeList.back();
|
2010-11-13 08:08:45 +01:00
|
|
|
const Token *tok2 = tok->tokAt(2);
|
|
|
|
|
|
|
|
// only create base list for classes and structures
|
2011-10-13 20:53:06 +02:00
|
|
|
if (new_scope->isClassOrStruct()) {
|
2010-11-13 08:08:45 +01:00
|
|
|
// goto initial '{'
|
2011-01-21 07:42:41 +01:00
|
|
|
tok2 = initBaseInfo(new_scope, tok);
|
2011-02-19 20:38:00 +01:00
|
|
|
|
|
|
|
// make sure we have valid code
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!tok2) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scopeList.pop_back();
|
2011-02-19 20:38:00 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
new_scope->classStart = tok2;
|
|
|
|
new_scope->classEnd = tok2->link();
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2010-12-16 19:04:47 +01:00
|
|
|
// make sure we have valid code
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!new_scope->classEnd) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scopeList.pop_back();
|
2010-12-16 19:04:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-11 01:43:29 +01:00
|
|
|
// fill the classAndStructTypes set..
|
|
|
|
if (new_scope->isClassOrStruct())
|
|
|
|
classAndStructTypes.insert(new_scope->className);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-11 01:43:29 +01:00
|
|
|
// make the new scope the current scope
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
|
2011-01-20 18:02:52 +01:00
|
|
|
// forward declaration
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok, "class|struct %var% ;")) {
|
2011-01-20 18:02:52 +01:00
|
|
|
// fill the classAndStructTypes set..
|
|
|
|
classAndStructTypes.insert(tok->next()->str());
|
|
|
|
|
|
|
|
/** @todo save forward declarations in database someday */
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-03-14 02:01:33 +01:00
|
|
|
// using namespace
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok, "using namespace %type% ;|::")) {
|
2011-03-14 02:01:33 +01:00
|
|
|
// save location
|
|
|
|
scope->usingList.push_back(tok);
|
|
|
|
|
|
|
|
tok = tok->tokAt(3);
|
|
|
|
}
|
|
|
|
|
2011-03-30 01:48:12 +02:00
|
|
|
// unnamed struct and union
|
2011-03-29 13:08:59 +02:00
|
|
|
else if (Token::Match(tok, "struct|union {") &&
|
2012-01-26 17:04:25 +01:00
|
|
|
Token::Match(tok->next()->link(), "} *|&| %var% ;|[")) {
|
2011-03-27 19:48:41 +02:00
|
|
|
scopeList.push_back(Scope(this, tok, scope));
|
|
|
|
|
|
|
|
Scope *new_scope = &scopeList.back();
|
|
|
|
|
2011-06-23 04:41:11 +02:00
|
|
|
std::vector<Dimension> dimensions;
|
|
|
|
|
2011-12-17 19:04:03 +01:00
|
|
|
bool isPointer = false;
|
2012-01-26 17:04:25 +01:00
|
|
|
bool isReference = false;
|
2011-06-23 04:41:11 +02:00
|
|
|
bool isArray = false;
|
|
|
|
|
2011-12-17 19:04:03 +01:00
|
|
|
const Token* varNameTok = tok->next()->link()->next();
|
|
|
|
if (varNameTok->str() == "*") {
|
|
|
|
isPointer = true;
|
|
|
|
varNameTok = varNameTok->next();
|
2012-01-26 17:04:25 +01:00
|
|
|
} else if (varNameTok->str() == "&") {
|
|
|
|
isReference = true;
|
|
|
|
varNameTok = varNameTok->next();
|
2011-12-17 19:04:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (varNameTok->next()->str() == "[")
|
|
|
|
isArray = arrayDimensions(dimensions, varNameTok->next());
|
2011-06-23 04:41:11 +02:00
|
|
|
|
2012-01-26 17:04:25 +01:00
|
|
|
scope->addVariable(varNameTok, tok, tok, scope->access, false, false, false, true, new_scope, scope, isArray, isPointer, isReference, dimensions);
|
2011-03-27 19:48:41 +02:00
|
|
|
|
|
|
|
const Token *tok2 = tok->next();
|
|
|
|
|
|
|
|
new_scope->classStart = tok2;
|
|
|
|
new_scope->classEnd = tok2->link();
|
|
|
|
|
|
|
|
// make sure we have valid code
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!new_scope->classEnd) {
|
2011-03-27 19:48:41 +02:00
|
|
|
scopeList.pop_back();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make the new scope the current scope
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
|
|
|
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
|
2011-04-01 02:54:23 +02:00
|
|
|
// anonymous struct and union
|
|
|
|
else if (Token::Match(tok, "struct|union {") &&
|
2011-10-13 20:53:06 +02:00
|
|
|
Token::simpleMatch(tok->next()->link(), "} ;")) {
|
2011-04-01 02:54:23 +02:00
|
|
|
scopeList.push_back(Scope(this, tok, scope));
|
|
|
|
|
|
|
|
Scope *new_scope = &scopeList.back();
|
|
|
|
|
|
|
|
const Token *tok2 = tok->next();
|
|
|
|
|
|
|
|
new_scope->classStart = tok2;
|
|
|
|
new_scope->classEnd = tok2->link();
|
|
|
|
|
|
|
|
// make sure we have valid code
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!new_scope->classEnd) {
|
2011-04-01 02:54:23 +02:00
|
|
|
scopeList.pop_back();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make the new scope the current scope
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
|
|
|
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2011-04-01 02:54:23 +02:00
|
|
|
// check for end of scope
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok == scope->classEnd) {
|
2011-01-21 07:42:41 +01:00
|
|
|
scope = scope->nestedIn;
|
2010-11-13 08:08:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if in class or structure
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (scope->type == Scope::eClass || scope->type == Scope::eStruct) {
|
2010-11-13 08:08:45 +01:00
|
|
|
const Token *funcStart = 0;
|
|
|
|
const Token *argStart = 0;
|
|
|
|
|
|
|
|
// What section are we in..
|
|
|
|
if (tok->str() == "private:")
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->access = Private;
|
2010-11-13 08:08:45 +01:00
|
|
|
else if (tok->str() == "protected:")
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->access = Protected;
|
2012-04-09 11:19:19 +02:00
|
|
|
else if (tok->str() == "public:" || tok->str() == "__published:")
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->access = Public;
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok, "public|protected|private %var% :")) {
|
2010-11-13 08:08:45 +01:00
|
|
|
if (tok->str() == "private")
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->access = Private;
|
2010-11-13 08:08:45 +01:00
|
|
|
else if (tok->str() == "protected")
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->access = Protected;
|
2012-02-11 12:26:48 +01:00
|
|
|
else
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->access = Public;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// class function?
|
2012-02-11 12:26:48 +01:00
|
|
|
else if (tok->previous()->str() != "::" && isFunction(tok, scope, &funcStart, &argStart)) {
|
2011-01-17 18:29:19 +01:00
|
|
|
Function function;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// save the function definition argument start '('
|
|
|
|
function.argDef = argStart;
|
|
|
|
|
|
|
|
// save the access type
|
2011-01-21 07:42:41 +01:00
|
|
|
function.access = scope->access;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// save the function name location
|
|
|
|
function.tokenDef = funcStart;
|
|
|
|
|
|
|
|
// operator function
|
2011-10-13 20:53:06 +02:00
|
|
|
if (function.tokenDef->str().find("operator") == 0) {
|
2010-11-13 08:08:45 +01:00
|
|
|
function.isOperator = true;
|
|
|
|
|
|
|
|
// 'operator =' is special
|
2011-01-27 18:44:20 +01:00
|
|
|
if (function.tokenDef->str() == "operator=")
|
2011-01-17 18:29:19 +01:00
|
|
|
function.type = Function::eOperatorEqual;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// class constructor/destructor
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (function.tokenDef->str() == scope->className) {
|
2011-03-11 01:43:29 +01:00
|
|
|
// destructor
|
2010-11-13 08:08:45 +01:00
|
|
|
if (function.tokenDef->previous()->str() == "~")
|
2011-01-17 18:29:19 +01:00
|
|
|
function.type = Function::eDestructor;
|
2011-03-11 01:43:29 +01:00
|
|
|
|
|
|
|
// copy constructor
|
2010-11-13 08:08:45 +01:00
|
|
|
else if ((Token::Match(function.tokenDef, "%var% ( const %var% & )") ||
|
|
|
|
Token::Match(function.tokenDef, "%var% ( const %var% & %var% )")) &&
|
2011-01-21 07:42:41 +01:00
|
|
|
function.tokenDef->strAt(3) == scope->className)
|
2011-01-17 18:29:19 +01:00
|
|
|
function.type = Function::eCopyConstructor;
|
2011-03-11 01:43:29 +01:00
|
|
|
|
|
|
|
// copy constructor with non-const argument
|
2011-01-22 17:43:36 +01:00
|
|
|
else if ((Token::Match(function.tokenDef, "%var% ( %var% & )") ||
|
|
|
|
Token::Match(function.tokenDef, "%var% ( %var% & %var% )")) &&
|
|
|
|
function.tokenDef->strAt(2) == scope->className)
|
|
|
|
function.type = Function::eCopyConstructor;
|
2011-03-11 01:43:29 +01:00
|
|
|
|
|
|
|
// regular constructor
|
2010-11-13 08:08:45 +01:00
|
|
|
else
|
2011-01-17 18:29:19 +01:00
|
|
|
function.type = Function::eConstructor;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
if (function.tokenDef->previous()->str() == "explicit")
|
|
|
|
function.isExplicit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// function returning function pointer
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok->str() == "(") {
|
2010-11-13 08:08:45 +01:00
|
|
|
function.retFuncPtr = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token *tok1 = tok;
|
|
|
|
|
|
|
|
// look for end of previous statement
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok1->previous() && !Token::Match(tok1->previous(), ";|}|{|public:|protected:|private:")) {
|
2010-11-13 08:08:45 +01:00
|
|
|
// virtual function
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok1->previous()->str() == "virtual") {
|
2010-11-13 08:08:45 +01:00
|
|
|
function.isVirtual = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static function
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok1->previous()->str() == "static") {
|
2010-11-13 08:08:45 +01:00
|
|
|
function.isStatic = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// friend function
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok1->previous()->str() == "friend") {
|
2010-11-13 08:08:45 +01:00
|
|
|
function.isFriend = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok1 = tok1->previous();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token *end;
|
|
|
|
|
|
|
|
if (!function.retFuncPtr)
|
|
|
|
end = function.argDef->link();
|
|
|
|
else
|
|
|
|
end = tok->link()->next()->link();
|
|
|
|
|
|
|
|
// const function
|
|
|
|
if (end->next()->str() == "const")
|
|
|
|
function.isConst = true;
|
|
|
|
|
|
|
|
// pure virtual function
|
2011-02-20 14:25:42 +01:00
|
|
|
if (Token::Match(end, ") const| = %any%"))
|
2010-11-13 08:08:45 +01:00
|
|
|
function.isPure = true;
|
|
|
|
|
|
|
|
// count the number of constructors
|
2011-01-23 08:41:31 +01:00
|
|
|
if (function.type == Function::eConstructor ||
|
|
|
|
function.type == Function::eCopyConstructor)
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->numConstructors++;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// assume implementation is inline (definition and implementation same)
|
|
|
|
function.token = function.tokenDef;
|
2011-01-23 08:41:31 +01:00
|
|
|
function.arg = function.argDef;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// out of line function
|
|
|
|
if (Token::Match(end, ") const| ;") ||
|
2011-10-13 20:53:06 +02:00
|
|
|
Token::Match(end, ") const| = %any%")) {
|
2010-11-13 08:08:45 +01:00
|
|
|
// find the function implementation later
|
|
|
|
tok = end->next();
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->functionList.push_back(function);
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// inline function
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2010-11-13 08:08:45 +01:00
|
|
|
function.isInline = true;
|
|
|
|
function.hasBody = true;
|
|
|
|
|
2011-02-20 14:25:42 +01:00
|
|
|
// find start of function '{'
|
|
|
|
while (end && end->str() != "{")
|
|
|
|
end = end->next();
|
2012-01-31 19:34:55 +01:00
|
|
|
if (!end)
|
|
|
|
continue;
|
2011-02-20 14:25:42 +01:00
|
|
|
|
|
|
|
// save start of function
|
|
|
|
function.start = end;
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->functionList.push_back(function);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
const Token *tok2 = funcStart;
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *functionOf = scope;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
addNewFunction(&scope, &tok2);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (scope) {
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->functionOf = functionOf;
|
2011-03-14 02:18:49 +01:00
|
|
|
scope->function = &functionOf->functionList.back();
|
2011-03-25 03:08:18 +01:00
|
|
|
scope->function->functionScope = scope;
|
2011-03-14 02:18:49 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 07:57:10 +01:00
|
|
|
// nested class or friend function?
|
2012-02-11 12:26:48 +01:00
|
|
|
else if (tok->previous()->str() == "::" && isFunction(tok, scope, &funcStart, &argStart)) {
|
2011-02-03 07:57:10 +01:00
|
|
|
/** @todo check entire qualification for match */
|
|
|
|
Scope * nested = scope->findInNestedListRecursive(tok->strAt(-2));
|
|
|
|
|
|
|
|
if (nested)
|
2012-02-11 12:26:48 +01:00
|
|
|
addClassFunction(&scope, &tok, argStart);
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2011-02-03 07:57:10 +01:00
|
|
|
/** @todo handle friend functions */
|
|
|
|
}
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// friend class declaration?
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok, "friend class| %any% ;")) {
|
2011-01-17 18:29:19 +01:00
|
|
|
Scope::FriendInfo friendInfo;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
friendInfo.name = tok->strAt(1) == "class" ? tok->strAt(2) : tok->strAt(1);
|
2012-02-24 20:45:56 +01:00
|
|
|
// fill this in after parsing is complete
|
2011-01-21 07:42:41 +01:00
|
|
|
friendInfo.scope = 0;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->friendList.push_back(friendInfo);
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (scope->type == Scope::eNamespace || scope->type == Scope::eGlobal) {
|
2010-11-20 07:26:50 +01:00
|
|
|
const Token *funcStart = 0;
|
|
|
|
const Token *argStart = 0;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// function?
|
2012-02-11 12:26:48 +01:00
|
|
|
if (isFunction(tok, scope, &funcStart, &argStart)) {
|
2010-11-20 07:26:50 +01:00
|
|
|
// has body?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(argStart->link(), ") const| {|:")) {
|
2011-01-23 22:31:35 +01:00
|
|
|
Scope *old_scope = scope;
|
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// class function
|
|
|
|
if (tok->previous() && tok->previous()->str() == "::")
|
2012-02-11 12:26:48 +01:00
|
|
|
addClassFunction(&scope, &tok, argStart);
|
2010-11-20 07:26:50 +01:00
|
|
|
|
2010-11-23 18:41:07 +01:00
|
|
|
// class destructor
|
|
|
|
else if (tok->previous() && tok->previous()->str() == "~" &&
|
2011-11-13 13:10:59 +01:00
|
|
|
tok->tokAt(-2) && tok->strAt(-2) == "::")
|
2012-02-11 12:26:48 +01:00
|
|
|
addClassFunction(&scope, &tok, argStart);
|
2010-11-23 18:41:07 +01:00
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// regular function
|
2012-02-11 12:26:48 +01:00
|
|
|
else
|
|
|
|
addGlobalFunction(scope, tok, argStart, funcStart);
|
2010-12-16 19:04:47 +01:00
|
|
|
|
2011-01-23 22:31:35 +01:00
|
|
|
// syntax error
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!scope) {
|
2011-01-23 22:31:35 +01:00
|
|
|
scope = old_scope;
|
|
|
|
break;
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// function returning function pointer with body
|
|
|
|
else if (Token::simpleMatch(argStart->link(), ") ) (") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::Match(argStart->link()->linkAt(2), ") const| {")) {
|
2012-02-11 12:26:48 +01:00
|
|
|
tok = funcStart;
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *old_scope = scope;
|
2010-11-20 07:26:50 +01:00
|
|
|
|
|
|
|
// class function
|
2012-02-11 12:26:48 +01:00
|
|
|
if (tok->previous()->str() == "::")
|
|
|
|
addClassFunction(&scope, &tok, argStart);
|
2010-11-20 07:26:50 +01:00
|
|
|
|
|
|
|
// regular function
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2012-02-11 12:26:48 +01:00
|
|
|
Function* function = addGlobalFunction(scope, tok, argStart, funcStart);
|
|
|
|
function->retFuncPtr = true;
|
2011-01-23 22:31:35 +01:00
|
|
|
}
|
|
|
|
|
2010-12-16 19:04:47 +01:00
|
|
|
// syntax error?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!scope) {
|
2011-01-21 07:42:41 +01:00
|
|
|
scope = old_scope;
|
2011-01-23 22:31:35 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
2011-03-14 03:59:25 +01:00
|
|
|
|
|
|
|
// function prototype
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::simpleMatch(argStart->link(), ") ;")) {
|
2012-03-23 17:59:51 +01:00
|
|
|
bool newFunc = true; // Is this function already in the database?
|
|
|
|
for (std::list<Function>::const_iterator i = scope->functionList.begin(); i != scope->functionList.end(); ++i) {
|
|
|
|
if (i->tokenDef->str() == tok->str() && argsMatch(scope, i->argDef, argStart, "", 0))
|
|
|
|
newFunc = false;
|
|
|
|
}
|
|
|
|
// save function prototype in database
|
|
|
|
if (newFunc)
|
|
|
|
addGlobalFunctionDecl(scope, tok, argStart, funcStart);
|
|
|
|
|
2011-03-14 03:59:25 +01:00
|
|
|
tok = argStart->link()->next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// function returning function pointer prototype
|
|
|
|
else if (Token::simpleMatch(argStart->link(), ") ) (") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::simpleMatch(argStart->link()->linkAt(2), ") ;")) {
|
2012-03-23 17:59:51 +01:00
|
|
|
bool newFunc = true; // Is this function already in the database?
|
|
|
|
for (std::list<Function>::const_iterator i = scope->functionList.begin(); i != scope->functionList.end(); ++i) {
|
|
|
|
if (i->tokenDef->str() == tok->str() && argsMatch(scope, i->argDef, argStart, "", 0))
|
|
|
|
newFunc = false;
|
|
|
|
}
|
|
|
|
// save function prototype in database
|
|
|
|
if (newFunc) {
|
|
|
|
Function* func = addGlobalFunctionDecl(scope, tok, argStart, funcStart);
|
|
|
|
func->retFuncPtr = true;
|
|
|
|
}
|
|
|
|
|
2011-11-20 14:22:39 +01:00
|
|
|
tok = argStart->link()->linkAt(2)->next();
|
2011-03-14 03:59:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (scope->type == Scope::eFunction || scope->isLocal()) {
|
2011-02-26 21:53:57 +01:00
|
|
|
if (Token::simpleMatch(tok, "if (") &&
|
2011-10-13 20:53:06 +02:00
|
|
|
Token::simpleMatch(tok->next()->link(), ") {")) {
|
2011-03-11 01:43:29 +01:00
|
|
|
const Token *tok1 = tok->next()->link()->next();
|
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eIf, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::simpleMatch(tok, "else {")) {
|
2011-03-11 01:43:29 +01:00
|
|
|
const Token *tok1 = tok->next();
|
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eElse, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::simpleMatch(tok, "else if (") &&
|
2011-11-20 14:22:39 +01:00
|
|
|
Token::simpleMatch(tok->linkAt(2), ") {")) {
|
|
|
|
const Token *tok1 = tok->linkAt(2)->next();
|
2011-03-11 01:43:29 +01:00
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eElseIf, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::simpleMatch(tok, "for (") &&
|
|
|
|
Token::simpleMatch(tok->next()->link(), ") {")) {
|
2011-03-06 21:21:42 +01:00
|
|
|
// save location of initialization
|
2011-03-11 01:43:29 +01:00
|
|
|
const Token *tok1 = tok->next()->link()->next();
|
|
|
|
const Token *tok2 = tok->tokAt(2);
|
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eFor, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2011-03-06 21:21:42 +01:00
|
|
|
// check for variable declaration and add it to new scope if found
|
2011-03-11 01:43:29 +01:00
|
|
|
scope->checkVariable(tok2, Local);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::simpleMatch(tok, "while (") &&
|
|
|
|
Token::simpleMatch(tok->next()->link(), ") {")) {
|
2011-03-11 01:43:29 +01:00
|
|
|
const Token *tok1 = tok->next()->link()->next();
|
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eWhile, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::simpleMatch(tok, "do {")) {
|
2011-03-11 01:43:29 +01:00
|
|
|
const Token *tok1 = tok->next();
|
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eDo, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::simpleMatch(tok, "switch (") &&
|
|
|
|
Token::simpleMatch(tok->next()->link(), ") {")) {
|
2011-03-11 01:43:29 +01:00
|
|
|
const Token *tok1 = tok->next()->link()->next();
|
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eSwitch, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2012-01-26 04:05:29 +01:00
|
|
|
} else if (Token::simpleMatch(tok, "try {")) {
|
|
|
|
const Token *tok1 = tok->next();
|
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eTry, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
|
|
|
} else if (Token::simpleMatch(tok, "catch (") &&
|
|
|
|
Token::simpleMatch(tok->next()->link(), ") {")) {
|
|
|
|
const Token *tok1 = tok->next()->link()->next();
|
|
|
|
const Token *tok2 = tok->tokAt(2);
|
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eCatch, tok1));
|
|
|
|
tok = tok1;
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
|
|
|
// check for variable declaration and add it to new scope if found
|
|
|
|
scope->checkVariable(tok2, Throw);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->str() == "{") {
|
|
|
|
if (!Token::Match(tok->previous(), "=|,")) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scopeList.push_back(Scope(this, tok, scope, Scope::eUnconditional, tok));
|
|
|
|
scope = &scopeList.back();
|
|
|
|
scope->nestedIn->nestedList.push_back(scope);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-02-26 21:53:57 +01:00
|
|
|
tok = tok->link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope>::iterator it;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2010-12-02 07:35:01 +01:00
|
|
|
// fill in base class info
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scope = &(*it);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// skip namespaces and functions
|
2011-01-21 07:42:41 +01:00
|
|
|
if (!scope->isClassOrStruct())
|
2010-11-13 08:08:45 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// finish filling in base class info
|
2011-10-13 20:53:06 +02:00
|
|
|
for (unsigned int i = 0; i < scope->derivedFrom.size(); ++i) {
|
2011-03-20 17:53:37 +01:00
|
|
|
std::list<Scope>::const_iterator it1;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-20 17:53:37 +01:00
|
|
|
// check all scopes for match
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it1 = scopeList.begin(); it1 != scopeList.end(); ++it1) {
|
2011-03-20 17:53:37 +01:00
|
|
|
// check scope for match
|
|
|
|
const Scope *scope1 = it1->findQualifiedScope(scope->derivedFrom[i].name);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-20 17:53:37 +01:00
|
|
|
// found match?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (scope1) {
|
2011-03-20 17:53:37 +01:00
|
|
|
// set found scope
|
|
|
|
scope->derivedFrom[i].scope = const_cast<Scope *>(scope1);
|
|
|
|
break;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2012-02-24 20:45:56 +01:00
|
|
|
// fill in friend info
|
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it) {
|
|
|
|
for (std::list<Scope::FriendInfo>::iterator i = it->friendList.begin(); i != it->friendList.end(); ++i) {
|
|
|
|
for (std::list<Scope>::iterator j = scopeList.begin(); j != scopeList.end(); ++j) {
|
|
|
|
// check scope for match
|
2012-02-26 11:56:32 +01:00
|
|
|
scope = const_cast<Scope*>(j->findQualifiedScope(i->name));
|
2012-02-24 20:45:56 +01:00
|
|
|
|
|
|
|
// found match?
|
|
|
|
if (scope && scope->isClassOrStruct()) {
|
|
|
|
// set found scope
|
2012-02-26 11:56:32 +01:00
|
|
|
i->scope = scope;
|
2012-02-24 20:45:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 07:35:01 +01:00
|
|
|
// fill in variable info
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it) {
|
2011-02-26 21:57:16 +01:00
|
|
|
// find variables
|
2012-03-23 17:59:51 +01:00
|
|
|
it->getVariableList();
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 21:51:12 +01:00
|
|
|
// fill in function arguments
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it) {
|
2011-02-26 21:51:12 +01:00
|
|
|
std::list<Function>::iterator func;
|
|
|
|
|
2012-03-23 17:59:51 +01:00
|
|
|
for (func = it->functionList.begin(); func != it->functionList.end(); ++func) {
|
2011-02-26 21:51:12 +01:00
|
|
|
// add arguments
|
2011-04-23 03:41:55 +02:00
|
|
|
func->addArguments(this, &*func, scope);
|
2011-02-26 21:51:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 07:35:01 +01:00
|
|
|
// determine if user defined type needs initialization
|
|
|
|
unsigned int unknowns = 0; // stop checking when there are no unknowns
|
|
|
|
unsigned int retry = 0; // bail if we don't resolve all the variable types for some reason
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
do {
|
2010-12-02 07:35:01 +01:00
|
|
|
unknowns = 0;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scope = &(*it);
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (scope->isClassOrStruct() && scope->needInitialization == Scope::Unknown) {
|
2010-12-02 07:35:01 +01:00
|
|
|
// check for default constructor
|
|
|
|
bool hasDefaultConstructor = false;
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Function>::const_iterator func;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
|
|
|
|
if (func->type == Function::eConstructor) {
|
2010-12-02 07:35:01 +01:00
|
|
|
// check for no arguments: func ( )
|
2011-10-13 20:53:06 +02:00
|
|
|
if (func->argCount() == 0) {
|
2010-12-02 07:35:01 +01:00
|
|
|
hasDefaultConstructor = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-01-15 07:59:37 +01:00
|
|
|
|
|
|
|
/** check for arguments with default values */
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (func->argCount() == func->initializedArgCount()) {
|
2011-01-15 07:59:37 +01:00
|
|
|
hasDefaultConstructor = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 18:45:53 +01:00
|
|
|
// User defined types with user defined default constructor doesn't need initialization.
|
2010-12-02 07:35:01 +01:00
|
|
|
// We assume the default constructor initializes everything.
|
|
|
|
// Another check will figure out if the constructor actually initializes everything.
|
|
|
|
if (hasDefaultConstructor)
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->needInitialization = Scope::False;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
|
|
|
// check each member variable to see if it needs initialization
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2010-12-02 07:35:01 +01:00
|
|
|
bool needInitialization = false;
|
|
|
|
bool unknown = false;
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Variable>::const_iterator var;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
|
|
|
|
if (var->isClass()) {
|
|
|
|
if (var->type()) {
|
2010-12-02 07:35:01 +01:00
|
|
|
// does this type need initialization?
|
2011-01-18 07:32:06 +01:00
|
|
|
if (var->type()->needInitialization == Scope::True)
|
2010-12-02 07:35:01 +01:00
|
|
|
needInitialization = true;
|
2011-01-18 07:32:06 +01:00
|
|
|
else if (var->type()->needInitialization == Scope::Unknown)
|
2010-12-02 07:35:01 +01:00
|
|
|
unknown = true;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2010-12-02 07:35:01 +01:00
|
|
|
needInitialization = true;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!unknown) {
|
2010-12-02 07:35:01 +01:00
|
|
|
if (needInitialization)
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->needInitialization = Scope::True;
|
2010-12-02 07:35:01 +01:00
|
|
|
else
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->needInitialization = Scope::False;
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope->needInitialization == Scope::Unknown)
|
2010-12-02 07:35:01 +01:00
|
|
|
unknowns++;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (scope->type == Scope::eUnion && scope->needInitialization == Scope::Unknown)
|
2011-03-27 19:48:41 +02:00
|
|
|
scope->needInitialization = Scope::True;
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
retry++;
|
2011-10-13 20:53:06 +02:00
|
|
|
} while (unknowns && retry < 100);
|
2010-12-02 07:35:01 +01:00
|
|
|
|
|
|
|
// this shouldn't happen so output a debug warning
|
2011-10-13 20:53:06 +02:00
|
|
|
if (retry == 100 && _settings->debugwarnings) {
|
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scope = &(*it);
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope->isClassOrStruct() && scope->needInitialization == Scope::Unknown)
|
2011-03-03 03:08:27 +01:00
|
|
|
debugMessage(scope->classDef, "SymbolDatabase::SymbolDatabase couldn't resolve all user defined types.");
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
2011-02-26 21:57:16 +01:00
|
|
|
|
|
|
|
// create variable symbol table
|
|
|
|
_variableList.resize(_tokenizer->varIdCount() + 1);
|
2011-10-20 09:02:20 +02:00
|
|
|
std::fill_n(_variableList.begin(), _variableList.size(), (const Variable*)NULL);
|
2011-02-26 21:57:16 +01:00
|
|
|
|
|
|
|
// check all scopes for variables
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scope = &(*it);
|
2011-02-26 21:57:16 +01:00
|
|
|
|
|
|
|
// add all variables
|
|
|
|
std::list<Variable>::const_iterator var;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
|
2011-02-26 21:57:16 +01:00
|
|
|
unsigned int varId = var->varId();
|
|
|
|
if (varId)
|
2011-03-11 01:43:29 +01:00
|
|
|
_variableList[varId] = &(*var);
|
2011-02-26 21:57:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// add all function paramaters
|
|
|
|
std::list<Function>::const_iterator func;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
|
2011-02-26 21:57:16 +01:00
|
|
|
// ignore function without implementations
|
|
|
|
if (!func->hasBody)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::list<Variable>::const_iterator arg;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (arg = func->argumentList.begin(); arg != func->argumentList.end(); ++arg) {
|
2011-02-26 21:57:16 +01:00
|
|
|
// check for named parameters
|
2011-10-13 20:53:06 +02:00
|
|
|
if (arg->nameToken() && arg->varId()) {
|
2011-02-26 21:57:16 +01:00
|
|
|
unsigned int varId = arg->varId();
|
|
|
|
if (varId)
|
2011-03-11 01:43:29 +01:00
|
|
|
_variableList[varId] = &(*arg);
|
2011-02-26 21:57:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-24 04:35:15 +02:00
|
|
|
|
|
|
|
/* set all unknown array dimensions that are set by a variable to the maximum size of that variable type */
|
2011-10-13 20:53:06 +02:00
|
|
|
for (size_t i = 1; i <= _tokenizer->varIdCount(); i++) {
|
2011-06-24 04:35:15 +02:00
|
|
|
// check each array variable
|
2011-10-13 20:53:06 +02:00
|
|
|
if (_variableList[i] && _variableList[i]->isArray()) {
|
2011-06-24 04:35:15 +02:00
|
|
|
// check each array dimension
|
2011-10-13 20:53:06 +02:00
|
|
|
for (size_t j = 0; j < _variableList[i]->dimensions().size(); j++) {
|
2011-06-24 04:35:15 +02:00
|
|
|
// check for a single token dimension that is a variable
|
2011-08-28 19:32:42 +02:00
|
|
|
if (_variableList[i]->dimensions()[j].start &&
|
|
|
|
(_variableList[i]->dimensions()[j].start == _variableList[i]->dimensions()[j].end) &&
|
2011-10-13 20:53:06 +02:00
|
|
|
_variableList[i]->dimensions()[j].start->varId()) {
|
2011-06-24 04:35:15 +02:00
|
|
|
Dimension &dimension = const_cast<Dimension &>(_variableList[i]->dimensions()[j]);
|
|
|
|
|
|
|
|
// get maximum size from type
|
|
|
|
// find where this type is defined
|
|
|
|
const Variable *var = getVariableFromVarId(dimension.start->varId());
|
2011-06-24 14:02:41 +02:00
|
|
|
|
2011-06-24 04:35:15 +02:00
|
|
|
// make sure it is in the database
|
|
|
|
if (!var)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// get type token
|
|
|
|
const Token *index_type = var->typeEndToken();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (index_type->str() == "char") {
|
2011-06-24 04:35:15 +02:00
|
|
|
if (index_type->isUnsigned())
|
|
|
|
dimension.num = UCHAR_MAX + 1;
|
|
|
|
else if (index_type->isSigned())
|
|
|
|
dimension.num = SCHAR_MAX + 1;
|
|
|
|
else
|
|
|
|
dimension.num = CHAR_MAX + 1;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (index_type->str() == "short") {
|
2011-06-24 04:35:15 +02:00
|
|
|
if (index_type->isUnsigned())
|
|
|
|
dimension.num = USHRT_MAX + 1;
|
|
|
|
else
|
|
|
|
dimension.num = SHRT_MAX + 1;
|
|
|
|
}
|
2011-06-24 14:02:41 +02:00
|
|
|
|
2011-06-24 04:35:15 +02:00
|
|
|
// checkScope assumes size is signed int so we limit the following sizes to INT_MAX
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (index_type->str() == "int") {
|
2011-06-24 04:35:15 +02:00
|
|
|
if (index_type->isUnsigned())
|
|
|
|
dimension.num = UINT_MAX + 1ULL;
|
|
|
|
else
|
|
|
|
dimension.num = INT_MAX + 1ULL;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (index_type->str() == "long") {
|
|
|
|
if (index_type->isUnsigned()) {
|
2011-06-24 04:35:15 +02:00
|
|
|
if (index_type->isLong())
|
2011-07-01 22:59:17 +02:00
|
|
|
dimension.num = ULLONG_MAX; // should be ULLONG_MAX + 1ULL
|
2011-06-24 04:35:15 +02:00
|
|
|
else
|
2011-07-01 22:59:17 +02:00
|
|
|
dimension.num = ULONG_MAX; // should be ULONG_MAX + 1ULL
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-06-24 04:35:15 +02:00
|
|
|
if (index_type->isLong())
|
2011-07-01 22:59:17 +02:00
|
|
|
dimension.num = LLONG_MAX; // should be LLONG_MAX + 1LL
|
2011-06-24 04:35:15 +02:00
|
|
|
else
|
2011-07-01 22:59:17 +02:00
|
|
|
dimension.num = LONG_MAX; // should be LONG_MAX + 1LL
|
2011-06-24 04:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const Token **funcStart, const Token **argStart) const
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
// function returning function pointer? '... ( ... %var% ( ... ))( ... ) {'
|
|
|
|
if (tok->str() == "(" &&
|
|
|
|
tok->link()->previous()->str() == ")" &&
|
|
|
|
tok->link()->next() &&
|
|
|
|
tok->link()->next()->str() == "(" &&
|
|
|
|
tok->link()->next()->link()->next() &&
|
2011-10-13 20:53:06 +02:00
|
|
|
Token::Match(tok->link()->next()->link()->next(), "{|;|const|=")) {
|
2010-11-13 08:08:45 +01:00
|
|
|
*funcStart = tok->link()->previous()->link()->previous();
|
|
|
|
*argStart = tok->link()->previous()->link();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// regular function?
|
2012-02-11 12:26:48 +01:00
|
|
|
else if (Token::Match(tok, "%var% (") && tok->previous() &&
|
|
|
|
(tok->previous()->isName() || tok->previous()->str() == "&" || tok->previous()->str() == "*" || // Either a return type in front of tok
|
|
|
|
tok->previous()->str() == "::" || tok->previous()->str() == "~" || // or a scope qualifier in front of tok
|
|
|
|
outerScope->isClassOrStruct()) && // or a ctor/dtor
|
2011-02-04 20:19:49 +01:00
|
|
|
(Token::Match(tok->next()->link(), ") const| ;|{|=") ||
|
2011-10-13 20:53:06 +02:00
|
|
|
Token::Match(tok->next()->link(), ") : %var% (|::"))) {
|
2010-11-13 08:08:45 +01:00
|
|
|
*funcStart = tok;
|
|
|
|
*argStart = tok->next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
bool SymbolDatabase::argsMatch(const Scope *scope, const Token *first, const Token *second, const std::string &path, unsigned int depth) const
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
bool match = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (first->str() == second->str()) {
|
2010-11-13 08:08:45 +01:00
|
|
|
// at end of argument list
|
2011-10-13 20:53:06 +02:00
|
|
|
if (first->str() == ")") {
|
2010-11-13 08:08:45 +01:00
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip default value assignment
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (first->next()->str() == "=") {
|
2012-02-11 12:26:48 +01:00
|
|
|
first = first->nextArgument();
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
if (second->next()->str() == "=") {
|
|
|
|
second = second->nextArgument();
|
|
|
|
if (!first || !second) { // End of argument list (first or second)
|
|
|
|
match = !first && !second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (!first) { // End of argument list (first)
|
|
|
|
match = second->next() && second->next()->str() == ")";
|
|
|
|
break;
|
|
|
|
}
|
2011-08-26 01:13:53 +02:00
|
|
|
}
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
// definition missing variable name
|
|
|
|
else if (first->next()->str() == "," && second->next()->str() != ",")
|
|
|
|
second = second->next();
|
|
|
|
else if (first->next()->str() == ")" && second->next()->str() != ")")
|
|
|
|
second = second->next();
|
|
|
|
|
|
|
|
// function missing variable name
|
|
|
|
else if (second->next()->str() == "," && first->next()->str() != ",")
|
|
|
|
first = first->next();
|
|
|
|
else if (second->next()->str() == ")" && first->next()->str() != ")")
|
|
|
|
first = first->next();
|
|
|
|
|
|
|
|
// argument list has different number of arguments
|
|
|
|
else if (second->str() == ")")
|
|
|
|
break;
|
|
|
|
|
|
|
|
// variable names are different
|
|
|
|
else if ((Token::Match(first->next(), "%var% ,|)|=") &&
|
|
|
|
Token::Match(second->next(), "%var% ,|)")) &&
|
2011-10-13 20:53:06 +02:00
|
|
|
(first->next()->str() != second->next()->str())) {
|
2010-11-13 08:08:45 +01:00
|
|
|
// skip variable names
|
|
|
|
first = first->next();
|
|
|
|
second = second->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// variable with class path
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (depth && Token::Match(first->next(), "%var%")) {
|
2010-11-13 08:08:45 +01:00
|
|
|
std::string param = path + first->next()->str();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(second->next(), param.c_str())) {
|
2010-11-13 08:08:45 +01:00
|
|
|
second = second->tokAt(int(depth) * 2);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (depth > 1) {
|
2010-11-13 08:08:45 +01:00
|
|
|
std::string short_path = path;
|
|
|
|
|
|
|
|
// remove last " :: "
|
|
|
|
short_path.resize(short_path.size() - 4);
|
|
|
|
|
|
|
|
// remove last name
|
|
|
|
while (!short_path.empty() && short_path[short_path.size() - 1] != ' ')
|
|
|
|
short_path.resize(short_path.size() - 1);
|
|
|
|
|
|
|
|
param = short_path + first->next()->str();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(second->next(), param.c_str())) {
|
2010-11-13 08:08:45 +01:00
|
|
|
second = second->tokAt((int(depth) - 1) * 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-07 19:42:30 +01:00
|
|
|
// nested class variable
|
|
|
|
else if (depth == 0 && Token::Match(first->next(), "%var%") &&
|
2011-01-21 07:42:41 +01:00
|
|
|
second->next()->str() == scope->className && second->strAt(2) == "::" &&
|
2011-10-13 20:53:06 +02:00
|
|
|
first->next()->str() == second->strAt(3)) {
|
2010-12-07 19:42:30 +01:00
|
|
|
second = second->tokAt(2);
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
first = first->next();
|
|
|
|
second = second->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
Function* SymbolDatabase::addGlobalFunction(Scope*& scope, const Token*& tok, const Token *argStart, const Token* funcStart)
|
2012-03-23 17:59:51 +01:00
|
|
|
{
|
|
|
|
Function* function = 0;
|
|
|
|
for (std::list<Function>::iterator i = scope->functionList.begin(); i != scope->functionList.end(); ++i) {
|
|
|
|
if (i->tokenDef->str() == tok->str() && argsMatch(scope, i->argDef, argStart, "", 0))
|
|
|
|
function = &*i;
|
|
|
|
}
|
|
|
|
if (!function)
|
|
|
|
function = addGlobalFunctionDecl(scope, tok, argStart, funcStart);
|
|
|
|
|
|
|
|
function->arg = argStart;
|
|
|
|
function->token = funcStart;
|
|
|
|
function->hasBody = true;
|
|
|
|
|
|
|
|
Scope* old_scope = scope;
|
|
|
|
addNewFunction(&scope, &tok);
|
|
|
|
|
|
|
|
if (scope) {
|
|
|
|
scope->function = &old_scope->functionList.back();
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Function* SymbolDatabase::addGlobalFunctionDecl(Scope*& scope, const Token*& tok, const Token *argStart, const Token* funcStart)
|
2012-02-11 12:26:48 +01:00
|
|
|
{
|
|
|
|
Function function;
|
|
|
|
|
|
|
|
// save the function definition argument start '('
|
|
|
|
function.argDef = argStart;
|
|
|
|
|
|
|
|
// save the access type
|
|
|
|
function.access = Public;
|
|
|
|
|
|
|
|
// save the function name location
|
|
|
|
function.tokenDef = funcStart;
|
|
|
|
|
|
|
|
function.isInline = false;
|
2012-03-23 17:59:51 +01:00
|
|
|
function.hasBody = false;
|
2012-02-11 12:26:48 +01:00
|
|
|
function.type = Function::eFunction;
|
|
|
|
|
|
|
|
// find start of function '{'
|
|
|
|
const Token *start = tok;
|
|
|
|
while (start && start->str() != "{")
|
|
|
|
start = start->next();
|
|
|
|
|
|
|
|
// save start of function
|
|
|
|
function.start = start;
|
|
|
|
|
2012-03-23 17:59:51 +01:00
|
|
|
scope->functionList.push_back(function);
|
|
|
|
return &scope->functionList.back();
|
2012-02-11 12:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolDatabase::addClassFunction(Scope **scope, const Token **tok, const Token *argStart)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
bool added = false;
|
|
|
|
std::string path;
|
|
|
|
unsigned int path_length = 0;
|
2010-11-23 18:41:07 +01:00
|
|
|
const Token *tok1;
|
|
|
|
|
2012-01-22 19:34:53 +01:00
|
|
|
const bool destructor((*tok)->previous()->str() == "~");
|
|
|
|
|
2010-11-23 18:41:07 +01:00
|
|
|
// skip class/struct name
|
2012-01-22 19:34:53 +01:00
|
|
|
if (destructor)
|
2010-11-23 18:41:07 +01:00
|
|
|
tok1 = (*tok)->tokAt(-3);
|
|
|
|
else
|
|
|
|
tok1 = (*tok)->tokAt(-2);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-08-11 23:57:54 +02:00
|
|
|
// syntax error?
|
|
|
|
if (!tok1)
|
|
|
|
return;
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
// back up to head of path
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok1 && tok1->previous() && tok1->previous()->str() == "::") {
|
2010-11-13 08:08:45 +01:00
|
|
|
path = tok1->str() + " :: " + path;
|
|
|
|
tok1 = tok1->tokAt(-2);
|
|
|
|
count++;
|
|
|
|
path_length++;
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:24:34 +01:00
|
|
|
if (tok1 && count) {
|
2010-11-13 08:08:45 +01:00
|
|
|
path = tok1->str() + " :: " + path;
|
|
|
|
path_length++;
|
|
|
|
}
|
|
|
|
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope>::iterator it1;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// search for match
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it1 = scopeList.begin(); it1 != scopeList.end(); ++it1) {
|
2011-03-11 01:43:29 +01:00
|
|
|
Scope *scope1 = &(*it1);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
bool match = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (scope1->className == tok1->str() && (scope1->type != Scope::eFunction)) {
|
2011-04-01 02:54:23 +02:00
|
|
|
// do the scopes match (same scope) or do their names match (multiple namespaces)
|
2011-01-21 07:42:41 +01:00
|
|
|
if ((*scope == scope1->nestedIn) || (*scope && scope1 &&
|
|
|
|
(*scope)->className == scope1->nestedIn->className &&
|
|
|
|
!(*scope)->className.empty() &&
|
2011-10-13 20:53:06 +02:00
|
|
|
(*scope)->type == scope1->nestedIn->type)) {
|
2012-01-07 09:28:26 +01:00
|
|
|
|
|
|
|
// nested scopes => check that they match
|
|
|
|
{
|
|
|
|
const Scope *s1 = *scope;
|
|
|
|
const Scope *s2 = scope1->nestedIn;
|
|
|
|
while (s1 && s2) {
|
|
|
|
if (s1->className != s2->className)
|
|
|
|
break;
|
|
|
|
s1 = s1->nestedIn;
|
|
|
|
s2 = s2->nestedIn;
|
|
|
|
}
|
|
|
|
// Not matching scopes
|
|
|
|
if (s1 || s2)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *scope2 = scope1;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (scope2 && count > 0) {
|
2010-11-20 07:26:50 +01:00
|
|
|
count--;
|
|
|
|
tok1 = tok1->tokAt(2);
|
2011-01-21 07:42:41 +01:00
|
|
|
scope2 = scope2->findInNestedList(tok1->str());
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (count == 0 && scope2) {
|
2010-11-20 07:26:50 +01:00
|
|
|
match = true;
|
2011-01-21 07:42:41 +01:00
|
|
|
scope1 = scope2;
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (match) {
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Function>::iterator func;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (func = scope1->functionList.begin(); func != scope1->functionList.end(); ++func) {
|
2012-01-22 19:34:53 +01:00
|
|
|
if (!func->hasBody && func->tokenDef->str() == (*tok)->str()) {
|
|
|
|
if (func->type == Function::eDestructor && destructor) {
|
2011-10-13 20:53:06 +02:00
|
|
|
if (argsMatch(scope1, func->tokenDef->next(), (*tok)->next(), path, path_length)) {
|
2010-11-23 18:41:07 +01:00
|
|
|
func->hasBody = true;
|
|
|
|
func->token = *tok;
|
|
|
|
func->arg = argStart;
|
2011-02-20 14:25:42 +01:00
|
|
|
const Token *start = argStart->link()->next();
|
|
|
|
while (start && start->str() != "{")
|
|
|
|
start = start->next();
|
|
|
|
func->start = start;
|
2010-11-23 18:41:07 +01:00
|
|
|
}
|
2012-01-22 19:34:53 +01:00
|
|
|
} else if (func->type != Function::eDestructor && !destructor) {
|
2011-10-13 20:53:06 +02:00
|
|
|
if (argsMatch(scope1, func->tokenDef->next(), (*tok)->next(), path, path_length)) {
|
2010-11-13 08:08:45 +01:00
|
|
|
// normal function?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!func->retFuncPtr && (*tok)->next()->link()) {
|
2010-11-13 08:08:45 +01:00
|
|
|
if ((func->isConst && (*tok)->next()->link()->next()->str() == "const") ||
|
2011-10-13 20:53:06 +02:00
|
|
|
(!func->isConst && (*tok)->next()->link()->next()->str() != "const")) {
|
2010-11-13 08:08:45 +01:00
|
|
|
func->hasBody = true;
|
2010-11-23 18:41:07 +01:00
|
|
|
func->token = *tok;
|
2010-11-13 08:08:45 +01:00
|
|
|
func->arg = argStart;
|
2011-02-20 14:25:42 +01:00
|
|
|
const Token *start = argStart->link()->next();
|
|
|
|
while (start && start->str() != "{")
|
|
|
|
start = start->next();
|
|
|
|
func->start = start;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// function returning function pointer?
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (func->retFuncPtr) {
|
2010-11-13 08:08:45 +01:00
|
|
|
// todo check for const
|
|
|
|
func->hasBody = true;
|
2010-11-23 18:41:07 +01:00
|
|
|
func->token = *tok;
|
2010-11-13 08:08:45 +01:00
|
|
|
func->arg = argStart;
|
2011-11-20 14:22:39 +01:00
|
|
|
const Token *start = argStart->link()->linkAt(2)->next();
|
2011-02-20 14:25:42 +01:00
|
|
|
while (start && start->str() != "{")
|
|
|
|
start = start->next();
|
|
|
|
func->start = start;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (func->hasBody) {
|
2011-01-21 07:42:41 +01:00
|
|
|
addNewFunction(scope, tok);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (*scope) {
|
2011-01-21 07:42:41 +01:00
|
|
|
(*scope)->functionOf = scope1;
|
2011-03-25 03:08:18 +01:00
|
|
|
(*scope)->function = &*func;
|
|
|
|
(*scope)->function->functionScope = *scope;
|
2011-03-14 02:18:49 +01:00
|
|
|
|
2010-12-16 19:04:47 +01:00
|
|
|
added = true;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for class function for unknown class
|
|
|
|
if (!added)
|
2011-01-21 07:42:41 +01:00
|
|
|
addNewFunction(scope, tok);
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
void SymbolDatabase::addNewFunction(Scope **scope, const Token **tok)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
const Token *tok1 = *tok;
|
2011-03-11 01:43:29 +01:00
|
|
|
scopeList.push_back(Scope(this, tok1, *scope));
|
|
|
|
Scope *new_scope = &scopeList.back();
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// skip to start of function
|
|
|
|
while (tok1 && tok1->str() != "{")
|
|
|
|
tok1 = tok1->next();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok1) {
|
2011-01-21 07:42:41 +01:00
|
|
|
new_scope->classStart = tok1;
|
|
|
|
new_scope->classEnd = tok1->link();
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2010-12-16 19:04:47 +01:00
|
|
|
// syntax error?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!new_scope->classEnd) {
|
2011-03-11 01:43:29 +01:00
|
|
|
scopeList.pop_back();
|
2010-12-16 19:04:47 +01:00
|
|
|
while (tok1->next())
|
|
|
|
tok1 = tok1->next();
|
2011-01-21 07:42:41 +01:00
|
|
|
*scope = NULL;
|
2010-12-16 19:04:47 +01:00
|
|
|
*tok = tok1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
*scope = new_scope;
|
2010-11-13 08:08:45 +01:00
|
|
|
*tok = tok1;
|
2011-03-11 01:43:29 +01:00
|
|
|
(*scope)->nestedIn->nestedList.push_back(*scope);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-03-11 01:43:29 +01:00
|
|
|
scopeList.pop_back();
|
2011-01-22 08:36:47 +01:00
|
|
|
*scope = NULL;
|
|
|
|
*tok = NULL;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
const Token *SymbolDatabase::initBaseInfo(Scope *scope, const Token *tok)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
// goto initial '{'
|
|
|
|
const Token *tok2 = tok->tokAt(2);
|
|
|
|
int level = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok2 && tok2->str() != "{") {
|
2010-11-13 08:08:45 +01:00
|
|
|
// skip unsupported templates
|
|
|
|
if (tok2->str() == "<")
|
|
|
|
level++;
|
|
|
|
else if (tok2->str() == ">")
|
|
|
|
level--;
|
|
|
|
|
|
|
|
// check for base classes
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (level == 0 && Token::Match(tok2, ":|,")) {
|
2011-01-17 18:29:19 +01:00
|
|
|
Scope::BaseInfo base;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-22 01:03:41 +01:00
|
|
|
base.isVirtual = false;
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
tok2 = tok2->next();
|
|
|
|
|
2011-02-19 20:38:00 +01:00
|
|
|
// check for invalid code
|
2011-02-21 20:25:35 +01:00
|
|
|
if (!tok2 || !tok2->next())
|
2011-02-19 20:38:00 +01:00
|
|
|
return NULL;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->str() == "virtual") {
|
2011-03-22 01:03:41 +01:00
|
|
|
base.isVirtual = true;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->str() == "public") {
|
2010-11-13 08:08:45 +01:00
|
|
|
base.access = Public;
|
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok2->str() == "protected") {
|
2010-11-13 08:08:45 +01:00
|
|
|
base.access = Protected;
|
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok2->str() == "private") {
|
2010-11-13 08:08:45 +01:00
|
|
|
base.access = Private;
|
|
|
|
tok2 = tok2->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-11-13 08:08:45 +01:00
|
|
|
if (tok->str() == "class")
|
|
|
|
base.access = Private;
|
|
|
|
else if (tok->str() == "struct")
|
|
|
|
base.access = Public;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->str() == "virtual") {
|
2011-03-22 01:03:41 +01:00
|
|
|
base.isVirtual = true;
|
|
|
|
tok2 = tok2->next();
|
2011-06-30 01:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle global namespace
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->str() == "::") {
|
2011-06-30 01:43:39 +02:00
|
|
|
base.name = ":: ";
|
|
|
|
tok2 = tok2->next();
|
2011-03-22 01:03:41 +01:00
|
|
|
}
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
// handle derived base classes
|
2011-10-13 20:53:06 +02:00
|
|
|
while (Token::Match(tok2, "%var% ::")) {
|
2010-11-13 08:08:45 +01:00
|
|
|
base.name += tok2->str();
|
|
|
|
base.name += " :: ";
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
base.name += tok2->str();
|
2011-08-28 19:32:42 +02:00
|
|
|
base.scope = NULL;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-02-15 01:50:13 +01:00
|
|
|
// add unhandled templates
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->next() && tok2->next()->str() == "<") {
|
2011-07-27 17:23:22 +02:00
|
|
|
tok2 = tok2->next();
|
|
|
|
base.name += tok2->str();
|
|
|
|
|
2010-11-30 19:52:42 +01:00
|
|
|
int level1 = 1;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok2->next()) {
|
2011-02-15 01:50:13 +01:00
|
|
|
base.name += tok2->next()->str();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok2->next()->str() == ">") {
|
2010-11-30 19:52:42 +01:00
|
|
|
level1--;
|
2011-06-30 01:52:07 +02:00
|
|
|
if (level1 == 0)
|
2010-11-30 19:52:42 +01:00
|
|
|
break;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok2->next()->str() == "<")
|
2010-11-30 19:52:42 +01:00
|
|
|
level1++;
|
|
|
|
|
2010-11-25 07:43:39 +01:00
|
|
|
tok2 = tok2->next();
|
2010-11-30 19:52:42 +01:00
|
|
|
}
|
2010-11-25 07:43:39 +01:00
|
|
|
}
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
// save pattern for base class name
|
2011-02-15 01:50:13 +01:00
|
|
|
scope->derivedFrom.push_back(base);
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok2;
|
|
|
|
}
|
2011-01-15 07:59:37 +01:00
|
|
|
|
2011-03-03 03:08:27 +01:00
|
|
|
void SymbolDatabase::debugMessage(const Token *tok, const std::string &msg) const
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok && _settings->debugwarnings) {
|
2011-03-03 03:08:27 +01:00
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = tok->linenr();
|
|
|
|
loc.setfile(_tokenizer->file(tok));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
Severity::debug,
|
|
|
|
msg,
|
2011-04-14 18:02:01 +02:00
|
|
|
"debug",
|
|
|
|
false);
|
2011-03-03 03:08:27 +01:00
|
|
|
if (_errorLogger)
|
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-23 04:41:11 +02:00
|
|
|
bool SymbolDatabase::arrayDimensions(std::vector<Dimension> &dimensions, const Token *tok) const
|
|
|
|
{
|
|
|
|
bool isArray = false;
|
|
|
|
|
|
|
|
const Token *dim = tok;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (dim && dim->next() && dim->str() == "[") {
|
2011-06-23 04:41:11 +02:00
|
|
|
Dimension dimension;
|
2011-08-28 19:32:42 +02:00
|
|
|
// check for empty array dimension []
|
2011-10-13 20:53:06 +02:00
|
|
|
if (dim->next()->str() != "]") {
|
2011-08-28 19:32:42 +02:00
|
|
|
dimension.start = dim->next();
|
|
|
|
dimension.end = dim->link()->previous();
|
|
|
|
if (dimension.start == dimension.end && dimension.start->isNumber())
|
|
|
|
dimension.num = MathLib::toLongNumber(dimension.start->str());
|
|
|
|
}
|
2011-06-23 04:41:11 +02:00
|
|
|
dimensions.push_back(dimension);
|
|
|
|
dim = dim->link()->next();
|
|
|
|
isArray = true;
|
|
|
|
}
|
|
|
|
return isArray;
|
|
|
|
}
|
|
|
|
|
2012-01-05 18:22:54 +01:00
|
|
|
static std::ostream & operator << (std::ostream & s, Scope::ScopeType type)
|
|
|
|
{
|
|
|
|
s << (type == Scope::eGlobal ? "Global" :
|
|
|
|
type == Scope::eClass ? "Class" :
|
|
|
|
type == Scope::eStruct ? "Struct" :
|
|
|
|
type == Scope::eUnion ? "Union" :
|
|
|
|
type == Scope::eNamespace ? "Namespace" :
|
|
|
|
type == Scope::eFunction ? "Function" :
|
|
|
|
type == Scope::eIf ? "If" :
|
|
|
|
type == Scope::eElse ? "Else" :
|
|
|
|
type == Scope::eElseIf ? "ElseIf" :
|
|
|
|
type == Scope::eFor ? "For" :
|
|
|
|
type == Scope::eWhile ? "While" :
|
|
|
|
type == Scope::eDo ? "Do" :
|
|
|
|
type == Scope::eSwitch ? "Switch" :
|
2012-01-26 04:05:29 +01:00
|
|
|
type == Scope::eTry ? "Try" :
|
|
|
|
type == Scope::eCatch ? "Catch" :
|
2012-01-05 18:22:54 +01:00
|
|
|
type == Scope::eUnconditional ? "Unconditional" :
|
|
|
|
"Unknown");
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolDatabase::printVariable(const Variable *var, const char *indent) const
|
|
|
|
{
|
|
|
|
std::cout << indent << "_name: " << var->nameToken();
|
2012-01-05 18:24:27 +01:00
|
|
|
if (var->nameToken()) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " " << var->name() << " " << _tokenizer->fileLine(var->nameToken()) << std::endl;
|
|
|
|
std::cout << indent << " varId: " << var->varId() << std::endl;
|
2012-01-05 18:24:27 +01:00
|
|
|
} else
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << indent << "_start: " << var->typeStartToken() << " " << var->typeStartToken()->str()
|
|
|
|
<< " " << _tokenizer->fileLine(var->typeStartToken()) << std::endl;;
|
|
|
|
std::cout << indent << "_end: " << var->typeEndToken() << " " << var->typeEndToken()->str()
|
|
|
|
<< " " << _tokenizer->fileLine(var->typeEndToken()) << std::endl;;
|
|
|
|
std::cout << indent << "_index: " << var->index() << std::endl;
|
2012-01-05 18:24:27 +01:00
|
|
|
std::cout << indent << "_access: " <<
|
|
|
|
(var->isPublic() ? "Public" :
|
|
|
|
var->isProtected() ? "Protected" :
|
|
|
|
var->isPrivate() ? "Private" :
|
|
|
|
var->isGlobal() ? "Global" :
|
|
|
|
var->isNamespace() ? "Namespace" :
|
|
|
|
var->isArgument() ? "Argument" :
|
|
|
|
var->isLocal() ? "Local" :
|
2012-01-26 04:05:29 +01:00
|
|
|
var->isThrow() ? "Throw" :
|
2012-01-05 18:24:27 +01:00
|
|
|
"???") << std::endl;
|
|
|
|
std::cout << indent << "_flags: " << std::endl;
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << indent << " isMutable: " << (var->isMutable() ? "true" : "false") << std::endl;
|
|
|
|
std::cout << indent << " isStatic: " << (var->isStatic() ? "true" : "false") << std::endl;
|
|
|
|
std::cout << indent << " isConst: " << (var->isConst() ? "true" : "false") << std::endl;
|
|
|
|
std::cout << indent << " isClass: " << (var->isClass() ? "true" : "false") << std::endl;
|
|
|
|
std::cout << indent << " isArray: " << (var->isArray() ? "true" : "false") << std::endl;
|
2012-01-25 02:45:38 +01:00
|
|
|
std::cout << indent << " isPointer: " << (var->isPointer() ? "true" : "false") << std::endl;
|
2012-01-26 17:04:25 +01:00
|
|
|
std::cout << indent << " isReference: " << (var->isReference() ? "true" : "false") << std::endl;
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << indent << " hasDefault: " << (var->hasDefault() ? "true" : "false") << std::endl;
|
|
|
|
std::cout << indent << "_type: ";
|
2012-01-05 18:24:27 +01:00
|
|
|
if (var->type()) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << var->type()->className << " " << var->type()->type << " "
|
|
|
|
<< _tokenizer->fileLine(var->type()->classDef) << std::endl;
|
2012-01-05 18:24:27 +01:00
|
|
|
} else
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << "none" << std::endl;
|
|
|
|
|
|
|
|
std::cout << indent << "_scope: ";
|
2012-01-05 18:24:27 +01:00
|
|
|
if (var->scope()) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << var->scope()->className << " " << var->scope()->type;
|
|
|
|
if (var->scope()->classDef)
|
2012-01-05 18:24:27 +01:00
|
|
|
std::cout << " " << _tokenizer->fileLine(var->scope()->classDef) << std::endl;
|
2012-01-05 18:22:54 +01:00
|
|
|
else
|
2012-01-05 18:24:27 +01:00
|
|
|
std::cout << std::endl;
|
|
|
|
} else
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << "none" << std::endl;
|
|
|
|
|
|
|
|
std::cout << indent << "_dimensions:";
|
2012-01-05 18:24:27 +01:00
|
|
|
for (size_t i = 0; i < var->dimensions().size(); i++) {
|
|
|
|
std::cout << " " << var->dimension(i);
|
2012-01-05 18:22:54 +01:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolDatabase::printOut(const char *title) const
|
|
|
|
{
|
|
|
|
if (title)
|
|
|
|
std::cout << "\n### " << title << " ###\n";
|
|
|
|
|
|
|
|
std::list<Scope>::const_iterator scope;
|
|
|
|
|
2012-01-05 18:24:27 +01:00
|
|
|
for (scope = scopeList.begin(); scope != scopeList.end(); ++scope) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << "Scope: " << &*scope << std::endl;
|
|
|
|
std::cout << " type: " << scope->type << std::endl;
|
|
|
|
std::cout << " className: " << scope->className << std::endl;
|
|
|
|
std::cout << " classDef: " << scope->classDef;
|
|
|
|
if (scope->classDef)
|
|
|
|
std::cout << " " << scope->classDef->str() << " " << _tokenizer->fileLine(scope->classDef) << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::cout << " classStart: " << scope->classStart;
|
|
|
|
if (scope->classStart)
|
|
|
|
std::cout << " " << scope->classStart->str() << " " << _tokenizer->fileLine(scope->classStart) << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::cout << " classEnd: " << scope->classEnd;
|
|
|
|
if (scope->classEnd)
|
|
|
|
std::cout << " " << scope->classEnd->str() << " " << _tokenizer->fileLine(scope->classEnd) << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::list<Function>::const_iterator func;
|
|
|
|
|
|
|
|
// find the function body if not implemented inline
|
2012-01-05 18:24:27 +01:00
|
|
|
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " Function: " << &*func << std::endl;
|
2012-01-05 18:24:27 +01:00
|
|
|
std::cout << " name: " << func->tokenDef->str() << " "
|
2012-01-05 18:22:54 +01:00
|
|
|
<< _tokenizer->fileLine(func->tokenDef) << std::endl;
|
|
|
|
std::cout << " type: " << (func->type == Function::eConstructor? "Constructor" :
|
|
|
|
func->type == Function::eCopyConstructor ? "CopyConstructor" :
|
|
|
|
func->type == Function::eOperatorEqual ? "OperatorEqual" :
|
|
|
|
func->type == Function::eDestructor ? "Destructor" :
|
|
|
|
func->type == Function::eFunction ? "Function" :
|
|
|
|
"???") << std::endl;
|
|
|
|
std::cout << " access: " << (func->access == Public ? "Public" :
|
|
|
|
func->access == Protected ? "Protected" :
|
|
|
|
func->access == Private ? "Private" :
|
|
|
|
"???") << std::endl;
|
|
|
|
std::cout << " hasBody: " << (func->hasBody ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " isInline: " << (func->isInline ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " isConst: " << (func->isConst ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " isVirtual: " << (func->isVirtual ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " isPure: " << (func->isPure ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " isStatic: " << (func->isStatic ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " isFriend: " << (func->isFriend ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " isExplicit: " << (func->isExplicit ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " isOperator: " << (func->isOperator ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " retFuncPtr: " << (func->retFuncPtr ? "true" : "false") << std::endl;
|
|
|
|
std::cout << " tokenDef: " << _tokenizer->fileLine(func->tokenDef) << std::endl;
|
|
|
|
std::cout << " argDef: " << _tokenizer->fileLine(func->argDef) << std::endl;
|
2012-03-23 17:59:51 +01:00
|
|
|
if (func->hasBody) {
|
|
|
|
std::cout << " token: " << _tokenizer->fileLine(func->token) << std::endl;
|
|
|
|
std::cout << " arg: " << _tokenizer->fileLine(func->arg) << std::endl;
|
|
|
|
}
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " functionScope: ";
|
2012-01-05 18:24:27 +01:00
|
|
|
if (func->functionScope) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << func->functionScope->className << " "
|
|
|
|
<< _tokenizer->fileLine(func->functionScope->classDef) << std::endl;
|
2012-01-05 18:24:27 +01:00
|
|
|
} else
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << "Unknown" << std::endl;
|
|
|
|
|
|
|
|
std::list<Variable>::const_iterator var;
|
|
|
|
|
2012-01-05 18:24:27 +01:00
|
|
|
for (var = func->argumentList.begin(); var != func->argumentList.end(); ++var) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " Variable: " << &*var << std::endl;
|
|
|
|
printVariable(&*var, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<Variable>::const_iterator var;
|
|
|
|
|
2012-01-05 18:24:27 +01:00
|
|
|
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " Variable: " << &*var << std::endl;
|
|
|
|
printVariable(&*var, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << " derivedFrom[" << scope->derivedFrom.size() << "] = (";
|
|
|
|
|
|
|
|
size_t count = scope->derivedFrom.size();
|
2012-01-05 18:24:27 +01:00
|
|
|
for (size_t i = 0; i < scope->derivedFrom.size(); ++i) {
|
2012-01-05 18:22:54 +01:00
|
|
|
if (scope->derivedFrom[i].isVirtual)
|
|
|
|
std::cout << "Virtual ";
|
|
|
|
|
|
|
|
std::cout << (scope->derivedFrom[i].access == Public ? " Public " :
|
|
|
|
scope->derivedFrom[i].access == Protected ? " Protected " :
|
|
|
|
scope->derivedFrom[i].access == Private ? " Private " :
|
|
|
|
" Unknown");
|
|
|
|
|
|
|
|
if (scope->derivedFrom[i].scope)
|
|
|
|
std::cout << scope->derivedFrom[i].scope->type;
|
|
|
|
else
|
|
|
|
std::cout << " Unknown";
|
|
|
|
|
|
|
|
std::cout << " " << scope->derivedFrom[i].name;
|
|
|
|
if (count-- > 1)
|
|
|
|
std::cout << ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << " )" << std::endl;
|
|
|
|
|
|
|
|
std::cout << " nestedIn: " << scope->nestedIn;
|
2012-01-05 18:24:27 +01:00
|
|
|
if (scope->nestedIn) {
|
|
|
|
std::cout << " " << scope->nestedIn->type << " "
|
2012-01-23 04:32:53 +01:00
|
|
|
<< scope->nestedIn->className;
|
2012-01-05 18:22:54 +01:00
|
|
|
}
|
2012-01-23 04:32:53 +01:00
|
|
|
std::cout << std::endl;
|
2012-01-05 18:22:54 +01:00
|
|
|
|
|
|
|
std::cout << " nestedList[" << scope->nestedList.size() << "] = (";
|
|
|
|
|
|
|
|
std::list<Scope *>::const_iterator nsi;
|
|
|
|
|
|
|
|
count = scope->nestedList.size();
|
2012-01-05 18:24:27 +01:00
|
|
|
for (nsi = scope->nestedList.begin(); nsi != scope->nestedList.end(); ++nsi) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " " << &(*nsi) << " " << (*nsi)->type << " " << (*nsi)->className;
|
|
|
|
if (count-- > 1)
|
|
|
|
std::cout << ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << " )" << std::endl;
|
|
|
|
|
|
|
|
std::cout << " needInitialization: " << (scope->needInitialization == Scope::Unknown ? "Unknown" :
|
|
|
|
scope->needInitialization == Scope::True ? "True" :
|
|
|
|
scope->needInitialization == Scope::False ? "False" :
|
|
|
|
"Invalid") << std::endl;
|
|
|
|
|
|
|
|
std::list<const Token *>::const_iterator use;
|
|
|
|
|
2012-01-05 18:24:27 +01:00
|
|
|
for (use = scope->usingList.begin(); use != scope->usingList.end(); ++use) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " using: " << (*use)->strAt(2);
|
|
|
|
const Token *tok1 = (*use)->tokAt(3);
|
2012-01-05 18:24:27 +01:00
|
|
|
while (tok1 && tok1->str() == "::") {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << "::" << tok1->strAt(1);
|
|
|
|
tok1 = tok1->tokAt(2);
|
|
|
|
}
|
|
|
|
std::cout << " " << _tokenizer->fileLine(*use) << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << " functionOf: " << scope->functionOf;
|
2012-01-05 18:24:27 +01:00
|
|
|
if (scope->functionOf) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " " << scope->functionOf->type << " " << scope->functionOf->className;
|
|
|
|
if (scope->functionOf->classDef)
|
|
|
|
std::cout << " " << _tokenizer->fileLine(scope->functionOf->classDef);
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::cout << " function: " << scope->function;
|
2012-01-05 18:24:27 +01:00
|
|
|
if (scope->function) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << " " << scope->function->tokenDef->str() << " "
|
|
|
|
<< _tokenizer->fileLine(scope->function->tokenDef);
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
2012-01-05 18:24:27 +01:00
|
|
|
for (size_t i = 0; i < _variableList.size(); i++) {
|
2012-01-05 18:22:54 +01:00
|
|
|
std::cout << "_variableList[" << i << "] = " << _variableList[i] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-15 07:59:37 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
unsigned int Function::initializedArgCount() const
|
2011-01-15 07:59:37 +01:00
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
2011-03-31 03:59:43 +02:00
|
|
|
std::list<Variable>::const_iterator var;
|
2011-01-15 07:59:37 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (var = argumentList.begin(); var != argumentList.end(); ++var) {
|
2011-03-31 03:59:43 +02:00
|
|
|
if (var->hasDefault())
|
|
|
|
++count;
|
2011-01-15 07:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-04-23 03:41:55 +02:00
|
|
|
void Function::addArguments(const SymbolDatabase *symbolDatabase, const Function *func, const Scope *scope)
|
2011-02-26 21:51:12 +01:00
|
|
|
{
|
|
|
|
// check for non-empty argument list "( ... )"
|
2012-03-23 17:59:51 +01:00
|
|
|
if (arg && arg->link() != arg->next() && !Token::simpleMatch(arg, "( void )")) {
|
2011-02-26 21:51:12 +01:00
|
|
|
unsigned int count = 0;
|
2011-12-17 19:04:03 +01:00
|
|
|
|
|
|
|
for (const Token* tok = arg->next(); tok; tok = tok->next()) {
|
|
|
|
const Token* startTok = tok;
|
|
|
|
const Token* endTok = NULL;
|
|
|
|
const Token* nameTok = NULL;
|
|
|
|
bool isConstVar = bool(tok->str() == "const");
|
|
|
|
bool isArrayVar = false;
|
|
|
|
bool hasDefault = false;
|
2011-06-23 04:41:11 +02:00
|
|
|
std::vector<Dimension> dimensions;
|
2011-02-26 21:51:12 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok->str() != "," && tok->str() != ")" && tok->str() != "=") {
|
|
|
|
if (tok->varId() != 0) {
|
2011-02-26 21:51:12 +01:00
|
|
|
nameTok = tok;
|
|
|
|
endTok = tok->previous();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->str() == "[") {
|
2011-06-23 04:41:11 +02:00
|
|
|
isArrayVar = symbolDatabase->arrayDimensions(dimensions, tok);
|
2011-08-28 19:32:42 +02:00
|
|
|
|
|
|
|
// skip array dimension(s)
|
|
|
|
tok = tok->link();
|
|
|
|
while (tok->next()->str() == "[")
|
|
|
|
tok = tok->next()->link();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->str() == "<") {
|
2011-04-29 02:53:31 +02:00
|
|
|
int level = 1;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (tok && tok->next()) {
|
2011-04-29 02:53:31 +02:00
|
|
|
tok = tok->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->str() == ">") {
|
2011-04-29 02:53:31 +02:00
|
|
|
--level;
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->str() == "<")
|
2011-04-29 02:53:31 +02:00
|
|
|
level++;
|
|
|
|
}
|
2011-12-13 00:24:34 +01:00
|
|
|
if (!tok) // something is wrong so just bail
|
|
|
|
return;
|
2011-04-29 02:53:31 +02:00
|
|
|
}
|
2011-03-03 03:08:27 +01:00
|
|
|
|
2011-02-26 21:51:12 +01:00
|
|
|
tok = tok->next();
|
2011-04-29 02:53:31 +02:00
|
|
|
|
|
|
|
if (!tok) // something is wrong so just bail
|
|
|
|
return;
|
2011-02-26 21:51:12 +01:00
|
|
|
}
|
|
|
|
|
2011-03-03 03:08:27 +01:00
|
|
|
// check for argument with no name or missing varid
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!endTok) {
|
|
|
|
if (tok->previous()->isName()) {
|
|
|
|
if (tok->previous() != startTok->tokAt(isConstVar ? 1 : 0)) {
|
2011-03-03 03:08:27 +01:00
|
|
|
nameTok = tok->previous();
|
|
|
|
endTok = nameTok->previous();
|
|
|
|
|
2011-04-23 03:41:55 +02:00
|
|
|
if (func->hasBody)
|
|
|
|
symbolDatabase->debugMessage(nameTok, "Function::addArguments found argument \'" + nameTok->str() + "\' with varid 0.");
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2011-03-30 05:16:13 +02:00
|
|
|
endTok = startTok;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2011-03-03 03:08:27 +01:00
|
|
|
endTok = tok->previous();
|
|
|
|
}
|
2011-02-26 21:51:12 +01:00
|
|
|
|
|
|
|
const Token *typeTok = startTok;
|
|
|
|
if (isConstVar)
|
|
|
|
typeTok = typeTok->next();
|
|
|
|
|
|
|
|
const Scope *argType = NULL;
|
|
|
|
if (!typeTok->isStandardType())
|
|
|
|
argType = symbolDatabase->findVariableType(scope, typeTok);
|
|
|
|
|
|
|
|
bool isClassVar = startTok == endTok && !startTok->isStandardType();
|
2011-12-17 19:04:03 +01:00
|
|
|
bool isPointerVar = nameTok->strAt(-1) == "*" || nameTok->strAt(-2) == "*";
|
2012-01-26 17:04:25 +01:00
|
|
|
bool isReferenceVar = nameTok->strAt(-1) == "&";
|
2011-02-26 21:51:12 +01:00
|
|
|
|
2011-03-30 05:16:13 +02:00
|
|
|
// skip default values
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->str() == "=") {
|
2011-03-31 03:59:43 +02:00
|
|
|
hasDefault = true;
|
|
|
|
|
2011-03-30 05:16:13 +02:00
|
|
|
while (tok->str() != "," && tok->str() != ")")
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
|
2012-01-26 17:04:25 +01:00
|
|
|
argumentList.push_back(Variable(nameTok, startTok, endTok, count++, Argument, false, false, isConstVar, isClassVar, argType, functionScope, isArrayVar, isPointerVar, isReferenceVar, hasDefault, dimensions));
|
2011-03-31 03:59:43 +02:00
|
|
|
|
2011-02-26 21:51:12 +01:00
|
|
|
if (tok->str() == ")")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-02-26 21:53:57 +01:00
|
|
|
Scope::Scope(SymbolDatabase *check_, const Token *classDef_, Scope *nestedIn_, ScopeType type_, const Token *start_) :
|
|
|
|
check(check_),
|
|
|
|
type(type_),
|
|
|
|
classDef(classDef_),
|
|
|
|
classStart(start_),
|
|
|
|
classEnd(start_->link()),
|
|
|
|
nestedIn(nestedIn_),
|
|
|
|
access(Public),
|
|
|
|
numConstructors(0),
|
|
|
|
needInitialization(Scope::Unknown),
|
2011-03-14 02:18:49 +01:00
|
|
|
functionOf(NULL),
|
|
|
|
function(NULL)
|
2011-02-26 21:53:57 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
Scope::Scope(SymbolDatabase *check_, const Token *classDef_, Scope *nestedIn_) :
|
2010-11-13 08:08:45 +01:00
|
|
|
check(check_),
|
|
|
|
classDef(classDef_),
|
|
|
|
classStart(NULL),
|
|
|
|
classEnd(NULL),
|
|
|
|
nestedIn(nestedIn_),
|
2010-12-02 07:35:01 +01:00
|
|
|
numConstructors(0),
|
2011-01-17 18:29:19 +01:00
|
|
|
needInitialization(Scope::Unknown),
|
2011-03-14 02:18:49 +01:00
|
|
|
functionOf(NULL),
|
|
|
|
function(NULL)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!classDef) {
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eGlobal;
|
2010-11-20 07:26:50 +01:00
|
|
|
access = Public;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (classDef->str() == "class") {
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eClass;
|
2010-11-13 08:08:45 +01:00
|
|
|
className = classDef->next()->str();
|
|
|
|
access = Private;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (classDef->str() == "struct") {
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eStruct;
|
2011-04-01 02:54:23 +02:00
|
|
|
// anonymous and unnamed structs don't have a name
|
2011-03-29 13:08:59 +02:00
|
|
|
if (classDef->next()->str() != "{")
|
|
|
|
className = classDef->next()->str();
|
2010-11-13 08:08:45 +01:00
|
|
|
access = Public;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (classDef->str() == "union") {
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eUnion;
|
2011-04-01 02:54:23 +02:00
|
|
|
// anonymous and unnamed unions don't have a name
|
2011-03-27 19:48:41 +02:00
|
|
|
if (classDef->next()->str() != "{")
|
|
|
|
className = classDef->next()->str();
|
2010-11-13 08:08:45 +01:00
|
|
|
access = Public;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (classDef->str() == "namespace") {
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eNamespace;
|
2010-11-13 08:08:45 +01:00
|
|
|
className = classDef->next()->str();
|
|
|
|
access = Public;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eFunction;
|
2010-11-13 08:08:45 +01:00
|
|
|
className = classDef->str();
|
|
|
|
access = Public;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 19:04:03 +01:00
|
|
|
bool Scope::hasDefaultConstructor() const
|
2010-12-21 08:13:40 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (numConstructors) {
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Function>::const_iterator func;
|
2010-12-21 08:13:40 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (func = functionList.begin(); func != functionList.end(); ++func) {
|
2011-03-31 03:59:43 +02:00
|
|
|
if (func->type == Function::eConstructor && func->argCount() == 0)
|
2010-12-21 08:13:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-26 15:08:59 +01:00
|
|
|
AccessControl Scope::defaultAccess() const
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
switch (type) {
|
2011-02-26 15:08:59 +01:00
|
|
|
case eGlobal:
|
|
|
|
return Global;
|
|
|
|
case eClass:
|
|
|
|
return Private;
|
|
|
|
case eStruct:
|
|
|
|
return Public;
|
|
|
|
case eUnion:
|
|
|
|
return Public;
|
|
|
|
case eNamespace:
|
|
|
|
return Namespace;
|
2011-02-26 21:53:57 +01:00
|
|
|
default:
|
2011-02-26 15:08:59 +01:00
|
|
|
return Local;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// Get variable list..
|
2011-01-17 18:29:19 +01:00
|
|
|
void Scope::getVariableList()
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-02-26 15:08:59 +01:00
|
|
|
AccessControl varaccess = defaultAccess();
|
2010-11-20 07:26:50 +01:00
|
|
|
const Token *start;
|
2011-04-01 02:54:23 +02:00
|
|
|
int level = 1;
|
2010-11-20 07:26:50 +01:00
|
|
|
|
|
|
|
if (classStart)
|
|
|
|
start = classStart->next();
|
|
|
|
else
|
|
|
|
start = check->_tokenizer->tokens();
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = start; tok; tok = tok->next()) {
|
2011-04-01 02:54:23 +02:00
|
|
|
// end of scope?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->str() == "}") {
|
2011-04-01 02:54:23 +02:00
|
|
|
level--;
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2010-12-16 19:04:47 +01:00
|
|
|
// syntax error?
|
|
|
|
else if (tok->next() == NULL)
|
|
|
|
break;
|
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// Is it a function?
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok->str() == "{") {
|
2010-11-20 07:26:50 +01:00
|
|
|
tok = tok->link();
|
2010-12-16 19:04:47 +01:00
|
|
|
// syntax error?
|
|
|
|
if (!tok)
|
|
|
|
return;
|
2010-11-20 07:26:50 +01:00
|
|
|
continue;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// Is it a nested class or structure?
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok, "class|struct|union|namespace %type% :|{")) {
|
2010-11-20 07:26:50 +01:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
while (tok && tok->str() != "{")
|
|
|
|
tok = tok->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok) {
|
2010-11-20 07:26:50 +01:00
|
|
|
// skip implementation
|
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2010-11-20 07:26:50 +01:00
|
|
|
break;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(tok, "struct|union {") && Token::Match(tok->next()->link(), "} %var% ;|[")) {
|
2011-11-12 22:33:03 +01:00
|
|
|
tok = tok->next()->link()->tokAt(2);
|
2011-03-28 00:37:31 +02:00
|
|
|
continue;
|
2011-10-27 10:41:34 +02:00
|
|
|
} else if (Token::Match(tok, "struct|union {") && Token::simpleMatch(tok->next()->link(), "} ;")) {
|
2011-04-01 02:54:23 +02:00
|
|
|
level++;
|
|
|
|
tok = tok->next();
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// Borland C++: Skip all variables in the __published section.
|
2010-12-15 18:45:53 +01:00
|
|
|
// These are automatically initialized.
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok->str() == "__published:") {
|
|
|
|
for (; tok; tok = tok->next()) {
|
2010-11-13 08:08:45 +01:00
|
|
|
if (tok->str() == "{")
|
|
|
|
tok = tok->link();
|
|
|
|
if (Token::Match(tok->next(), "private:|protected:|public:"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "private:" "public:" "protected:" etc
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (tok->str() == "public:") {
|
2010-11-13 08:08:45 +01:00
|
|
|
varaccess = Public;
|
2010-11-20 07:26:50 +01:00
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->str() == "protected:") {
|
2010-11-13 08:08:45 +01:00
|
|
|
varaccess = Protected;
|
2010-11-20 07:26:50 +01:00
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (tok->str() == "private:") {
|
2010-11-13 08:08:45 +01:00
|
|
|
varaccess = Private;
|
|
|
|
continue;
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// Is it a forward declaration?
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok, "class|struct|union %var% ;")) {
|
2010-11-13 08:08:45 +01:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// Borland C++: Ignore properties..
|
|
|
|
else if (tok->str() == "__property")
|
2010-11-13 08:08:45 +01:00
|
|
|
continue;
|
|
|
|
|
2011-02-27 16:35:59 +01:00
|
|
|
// skip return and delete
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok, "return|delete")) {
|
2011-03-18 01:10:56 +01:00
|
|
|
while (tok->next() && tok->next()->str() != ";")
|
2011-02-27 16:13:40 +01:00
|
|
|
tok = tok->next();
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-30 20:01:33 +01:00
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// Search for start of statement..
|
2011-02-25 13:17:55 +01:00
|
|
|
else if (tok->previous() && !Token::Match(tok->previous(), ";|{|}|public:|protected:|private:"))
|
2010-11-13 08:08:45 +01:00
|
|
|
continue;
|
2010-12-02 07:35:01 +01:00
|
|
|
else if (Token::Match(tok, ";|{|}"))
|
|
|
|
continue;
|
2011-10-13 20:53:06 +02:00
|
|
|
else if (Token::Match(tok, "goto %var% ;")) {
|
2011-11-12 22:33:03 +01:00
|
|
|
tok = tok->tokAt(2);
|
2011-10-07 14:11:48 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
tok = checkVariable(tok, varaccess);
|
2011-03-16 03:33:14 +01:00
|
|
|
|
|
|
|
if (!tok)
|
|
|
|
break;
|
2011-03-06 21:21:42 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-20 07:26:50 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
const Token *Scope::checkVariable(const Token *tok, AccessControl varaccess)
|
|
|
|
{
|
|
|
|
// This is the start of a statement
|
|
|
|
const Token *vartok = NULL;
|
|
|
|
const Token *typetok = NULL;
|
|
|
|
|
2012-01-26 04:05:29 +01:00
|
|
|
// Is it a throw..?
|
|
|
|
if (Token::Match(tok, "throw %any% (") &&
|
|
|
|
Token::simpleMatch(tok->linkAt(2), ") ;")) {
|
|
|
|
return tok->linkAt(2);
|
|
|
|
} else if ((Token::Match(tok, "throw %any% :: %any% (") &&
|
|
|
|
Token::simpleMatch(tok->linkAt(4), ") ;"))) {
|
|
|
|
return tok->linkAt(4);
|
|
|
|
}
|
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
// Is it const..?
|
2012-03-23 17:59:51 +01:00
|
|
|
bool isConst(tok->str() == "const");
|
|
|
|
if (isConst) {
|
2011-03-06 21:21:42 +01:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
// Is it a static variable?
|
2012-03-23 17:59:51 +01:00
|
|
|
const bool isStatic(tok->str() == "static");
|
2011-10-13 20:53:06 +02:00
|
|
|
if (isStatic) {
|
2011-03-06 21:21:42 +01:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
// Is it a mutable variable?
|
2012-03-23 17:59:51 +01:00
|
|
|
const bool isMutable(tok->str() == "mutable");
|
2011-10-13 20:53:06 +02:00
|
|
|
if (isMutable) {
|
2011-03-06 21:21:42 +01:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
// Is it const..?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (tok->str() == "const") {
|
2011-03-06 21:21:42 +01:00
|
|
|
tok = tok->next();
|
|
|
|
isConst = true;
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-04-22 15:12:08 +02:00
|
|
|
// the start of the type tokens does not include the above modifiers
|
|
|
|
const Token *typestart = tok;
|
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
bool isClass = false;
|
2010-11-20 07:26:50 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(tok, "struct|union")) {
|
2011-03-06 21:21:42 +01:00
|
|
|
tok = tok->next();
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
bool isArray = false;
|
2011-12-17 19:04:03 +01:00
|
|
|
bool isPointer = false;
|
2012-01-26 17:04:25 +01:00
|
|
|
bool isReference = false;
|
2011-06-23 04:41:11 +02:00
|
|
|
std::vector<Dimension> dimensions;
|
2011-02-27 16:21:14 +01:00
|
|
|
|
2012-01-26 17:04:25 +01:00
|
|
|
if (tok && isVariableDeclaration(tok, vartok, typetok, isArray, isPointer, isReference)) {
|
2011-12-17 19:04:03 +01:00
|
|
|
isClass = (!typetok->isStandardType() && !isPointer && vartok->previous()->str() != "&");
|
2011-10-13 20:53:06 +02:00
|
|
|
if (isArray) {
|
2011-06-23 04:41:11 +02:00
|
|
|
isArray = check->arrayDimensions(dimensions, vartok->next());
|
|
|
|
tok = vartok->next();
|
|
|
|
while (tok && tok->str() == "[")
|
|
|
|
tok = tok->link()->next();
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2011-06-23 04:41:11 +02:00
|
|
|
tok = vartok->next();
|
2011-03-06 21:21:42 +01:00
|
|
|
}
|
2011-01-01 01:19:32 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
// If the vartok was set in the if-blocks above, create a entry for this variable..
|
2011-10-13 20:53:06 +02:00
|
|
|
if (vartok && vartok->str() != "operator") {
|
2011-03-06 21:21:42 +01:00
|
|
|
if (vartok->varId() == 0 && !vartok->isBoolean())
|
|
|
|
check->debugMessage(vartok, "Scope::checkVariable found variable \'" + vartok->str() + "\' with varid 0.");
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
const Scope *scope = NULL;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-03-06 21:21:42 +01:00
|
|
|
if (typetok)
|
|
|
|
scope = check->findVariableType(this, typetok);
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2012-01-26 17:04:25 +01:00
|
|
|
addVariable(vartok, typestart, vartok->previous(), varaccess, isMutable, isStatic, isConst, isClass, scope, this, isArray, isPointer, isReference, dimensions);
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
2011-03-06 21:21:42 +01:00
|
|
|
|
|
|
|
return tok;
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
|
|
|
|
2011-09-03 03:07:29 +02:00
|
|
|
const Variable *Scope::getVariable(const std::string &varname) const
|
|
|
|
{
|
|
|
|
std::list<Variable>::const_iterator iter;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (iter = varlist.begin(); iter != varlist.end(); ++iter) {
|
2011-09-03 03:07:29 +02:00
|
|
|
if (iter->name() == varname)
|
|
|
|
return &*iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
static const Token* skipScopeIdentifiers(const Token* tok)
|
2010-12-30 09:46:44 +01:00
|
|
|
{
|
2012-02-11 12:26:48 +01:00
|
|
|
if (Token::simpleMatch(tok, "::")) {
|
|
|
|
tok = tok->next();
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
2012-02-11 12:26:48 +01:00
|
|
|
while (Token::Match(tok, "%type% ::")) {
|
|
|
|
tok = tok->tokAt(2);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
2010-12-30 22:57:43 +01:00
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
return tok;
|
2010-12-30 22:57:43 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
static const Token* skipPointers(const Token* tok)
|
2010-12-30 22:57:43 +01:00
|
|
|
{
|
2012-02-11 12:26:48 +01:00
|
|
|
while (Token::Match(tok, "*|&")) {
|
|
|
|
tok = tok->next();
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
2010-12-30 22:57:43 +01:00
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
return tok;
|
2010-12-30 22:57:43 +01:00
|
|
|
}
|
|
|
|
|
2012-01-26 17:04:25 +01:00
|
|
|
bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const Token*& typetok, bool &isArray, bool &isPointer, bool &isReference) const
|
2010-12-30 22:57:43 +01:00
|
|
|
{
|
2012-01-28 01:24:01 +01:00
|
|
|
if (tok && tok->str() == "throw" && check->_tokenizer->isCPP())
|
|
|
|
return false;
|
|
|
|
|
2011-01-02 00:36:22 +01:00
|
|
|
const Token* localTypeTok = skipScopeIdentifiers(tok);
|
2011-01-18 10:51:57 +01:00
|
|
|
const Token* localVarTok = NULL;
|
2010-12-30 22:57:43 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(localTypeTok, "%type% <")) {
|
2011-01-18 10:14:12 +01:00
|
|
|
const Token* closeTok = NULL;
|
|
|
|
bool found = findClosingBracket(localTypeTok->next(), closeTok);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (found) {
|
2011-01-18 10:51:57 +01:00
|
|
|
localVarTok = skipPointers(closeTok->next());
|
2011-01-18 10:14:12 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(localVarTok, ":: %type% %var% ;|=")) {
|
2011-01-18 10:51:57 +01:00
|
|
|
localTypeTok = localVarTok->next();
|
|
|
|
localVarTok = localVarTok->tokAt(2);
|
2011-01-02 00:36:22 +01:00
|
|
|
}
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (Token::Match(localTypeTok, "%type%")) {
|
2012-03-24 15:10:06 +01:00
|
|
|
localVarTok = skipPointers(localTypeTok->strAt(1)=="const"?localTypeTok->tokAt(2):localTypeTok->next());
|
2011-01-18 10:51:57 +01:00
|
|
|
}
|
2010-12-30 22:57:43 +01:00
|
|
|
|
2012-02-11 12:26:48 +01:00
|
|
|
if (Token::Match(localVarTok, "%var% ;|=")) {
|
2011-02-27 16:21:14 +01:00
|
|
|
vartok = localVarTok;
|
|
|
|
typetok = localTypeTok;
|
|
|
|
isArray = false;
|
2012-02-11 12:26:48 +01:00
|
|
|
} else if (Token::Match(localVarTok, "%var% [") && localVarTok->str() != "operator") {
|
2011-01-18 10:51:57 +01:00
|
|
|
vartok = localVarTok;
|
|
|
|
typetok = localTypeTok;
|
2011-02-27 16:21:14 +01:00
|
|
|
isArray = true;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if ((isLocal() || type == Scope::eFunction) &&
|
|
|
|
Token::Match(localVarTok, "%var% (") &&
|
|
|
|
Token::simpleMatch(localVarTok->next()->link(), ") ;")) {
|
2011-07-23 21:12:30 +02:00
|
|
|
vartok = localVarTok;
|
|
|
|
typetok = localTypeTok;
|
|
|
|
isArray = false;
|
2012-01-26 04:05:29 +01:00
|
|
|
} else if (type == eCatch &&
|
|
|
|
(Token::Match(localTypeTok, "%var% )") ||
|
|
|
|
Token::Match(localTypeTok, "%var% &| %var% )"))) {
|
|
|
|
vartok = localVarTok;
|
|
|
|
typetok = localTypeTok;
|
|
|
|
isArray = false;
|
2011-07-23 21:12:30 +02:00
|
|
|
}
|
2012-01-26 04:05:29 +01:00
|
|
|
|
2011-12-25 11:05:06 +01:00
|
|
|
isPointer = vartok && (vartok->strAt(-1) == "*" || Token::simpleMatch(vartok->tokAt(-2), "* const"));
|
2012-01-26 17:04:25 +01:00
|
|
|
isReference = vartok && vartok->strAt(-1) == "&";
|
2010-12-30 22:57:43 +01:00
|
|
|
|
2010-12-30 09:46:44 +01:00
|
|
|
return NULL != vartok;
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
bool Scope::findClosingBracket(const Token* tok, const Token*& close) const
|
2011-01-02 00:36:22 +01:00
|
|
|
{
|
|
|
|
bool found = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (NULL != tok && tok->str() == "<") {
|
2011-01-02 00:36:22 +01:00
|
|
|
unsigned int depth = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (close = tok; (close != NULL) && (close->str() != ";") && (close->str() != "="); close = close->next()) {
|
|
|
|
if (close->str() == "<") {
|
2011-01-02 00:36:22 +01:00
|
|
|
++depth;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (close->str() == ">") {
|
|
|
|
if (--depth == 0) {
|
2011-01-02 00:36:22 +01:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-02 07:35:01 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
const Scope *SymbolDatabase::findVariableType(const Scope *start, const Token *type) const
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope>::const_iterator scope;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (scope = scopeList.begin(); scope != scopeList.end(); ++scope) {
|
2012-03-23 17:59:51 +01:00
|
|
|
// skip namespaces, functions, ...
|
|
|
|
if (scope->type != Scope::eClass && scope->type != Scope::eStruct && scope->type != Scope::eUnion)
|
2010-12-02 07:35:01 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// do the names match?
|
2011-10-13 20:53:06 +02:00
|
|
|
if (scope->className == type->str()) {
|
2010-12-02 07:35:01 +01:00
|
|
|
// check if type does not have a namespace
|
2011-12-22 07:28:28 +01:00
|
|
|
if (type->previous() != NULL && type->previous()->str() != "::") {
|
2011-01-17 18:29:19 +01:00
|
|
|
const Scope *parent = start;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
|
|
|
// check if in same namespace
|
2011-10-13 20:53:06 +02:00
|
|
|
while (parent) {
|
2011-09-29 04:05:26 +02:00
|
|
|
// out of line class function belongs to class
|
|
|
|
if (parent->type == Scope::eFunction && parent->functionOf)
|
|
|
|
parent = parent->functionOf;
|
|
|
|
else if (parent != scope->nestedIn)
|
|
|
|
parent = parent->nestedIn;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope->nestedIn == parent)
|
2011-03-11 01:43:29 +01:00
|
|
|
return &(*scope);
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// type has a namespace
|
2011-10-13 20:53:06 +02:00
|
|
|
else {
|
2010-12-02 07:35:01 +01:00
|
|
|
// FIXME check if namespace path matches supplied path
|
2011-03-11 01:43:29 +01:00
|
|
|
return &(*scope);
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
}
|
2010-12-02 07:35:01 +01:00
|
|
|
|
|
|
|
return NULL;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2011-01-28 08:33:02 +01:00
|
|
|
|
|
|
|
const Scope *SymbolDatabase::findFunctionScopeByToken(const Token *tok) const
|
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope>::const_iterator scope;
|
2011-01-28 08:33:02 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (scope = scopeList.begin(); scope != scopeList.end(); ++scope) {
|
|
|
|
if (scope->type == Scope::eFunction) {
|
2011-03-11 01:43:29 +01:00
|
|
|
if (scope->classDef == tok)
|
|
|
|
return &(*scope);
|
2011-01-28 08:33:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const Function *SymbolDatabase::findFunctionByToken(const Token *tok) const
|
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope>::const_iterator scope;
|
2011-01-28 08:33:02 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (scope = scopeList.begin(); scope != scopeList.end(); ++scope) {
|
2011-01-28 08:33:02 +01:00
|
|
|
std::list<Function>::const_iterator func;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
|
2011-01-28 08:33:02 +01:00
|
|
|
if (func->token == tok)
|
2011-03-11 01:43:29 +01:00
|
|
|
return &(*func);
|
2011-01-28 08:33:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2012-02-24 20:45:56 +01:00
|
|
|
const Scope* SymbolDatabase::findScopeByName(const std::string& name) const
|
|
|
|
{
|
|
|
|
for (std::list<Scope>::const_iterator it = scopeList.begin(); it != scopeList.end(); ++it) {
|
|
|
|
if (it->className == name)
|
|
|
|
return &*it;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
Scope * Scope::findInNestedList(const std::string & name)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Scope *>::iterator it;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = nestedList.begin(); it != nestedList.end(); ++it) {
|
2010-11-13 08:08:45 +01:00
|
|
|
if ((*it)->className == name)
|
|
|
|
return (*it);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-02-03 07:57:10 +01:00
|
|
|
Scope * Scope::findInNestedListRecursive(const std::string & name)
|
|
|
|
{
|
|
|
|
std::list<Scope *>::iterator it;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = nestedList.begin(); it != nestedList.end(); ++it) {
|
2011-02-03 07:57:10 +01:00
|
|
|
if ((*it)->className == name)
|
|
|
|
return (*it);
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = nestedList.begin(); it != nestedList.end(); ++it) {
|
2011-02-03 07:57:10 +01:00
|
|
|
Scope *child = (*it)->findInNestedListRecursive(name);
|
2011-02-19 19:40:02 +01:00
|
|
|
if (child)
|
|
|
|
return child;
|
2011-02-03 07:57:10 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-03-20 17:53:37 +01:00
|
|
|
const Scope * Scope::findQualifiedScope(const std::string & name) const
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (type == Scope::eClass || type == Scope::eStruct || type == Scope::eNamespace) {
|
|
|
|
if (name.compare(0, className.size(), className) == 0) {
|
2011-03-20 17:53:37 +01:00
|
|
|
std::string path = name;
|
|
|
|
path.erase(0, className.size());
|
|
|
|
if (path.compare(0, 4, " :: ") == 0)
|
|
|
|
path.erase(0, 4);
|
|
|
|
else if (path.empty())
|
|
|
|
return this;
|
|
|
|
|
|
|
|
std::list<Scope *>::const_iterator it;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = nestedList.begin() ; it != nestedList.end(); ++it) {
|
2011-03-20 17:53:37 +01:00
|
|
|
const Scope *scope1 = (*it)->findQualifiedScope(path);
|
|
|
|
if (scope1)
|
|
|
|
return scope1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
const Function *Scope::getDestructor() const
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Function>::const_iterator it;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (it = functionList.begin(); it != functionList.end(); ++it) {
|
2011-01-17 18:29:19 +01:00
|
|
|
if (it->type == Function::eDestructor)
|
2011-03-11 01:43:29 +01:00
|
|
|
return &(*it);
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
unsigned int Scope::getNestedNonFunctions() const
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
unsigned int nested = 0;
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Scope *>::const_iterator ni;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (ni = nestedList.begin(); ni != nestedList.end(); ++ni) {
|
2011-01-17 18:29:19 +01:00
|
|
|
if ((*ni)->type != Scope::eFunction)
|
2010-11-13 08:08:45 +01:00
|
|
|
nested++;
|
|
|
|
}
|
|
|
|
return nested;
|
|
|
|
}
|