2010-11-13 08:08:45 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 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 <locale>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
: _tokenizer(tokenizer), _settings(settings), _errorLogger(errorLogger)
|
|
|
|
{
|
|
|
|
// find all namespaces (class,struct and namespace)
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *scope = new Scope(this, NULL, NULL);
|
|
|
|
scopeList.push_back(scope);
|
2010-11-13 08:08:45 +01:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
// Locate next class
|
|
|
|
if (Token::Match(tok, "class|struct|namespace %var% [{:]"))
|
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *new_scope = new Scope(this, tok, scope);
|
2010-11-13 08:08:45 +01:00
|
|
|
const Token *tok2 = tok->tokAt(2);
|
|
|
|
|
|
|
|
// only create base list for classes and structures
|
2011-01-21 07:42:41 +01:00
|
|
|
if (new_scope->isClassOrStruct())
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-20 18:02:52 +01:00
|
|
|
// fill the classAndStructTypes set..
|
2011-01-21 07:42:41 +01:00
|
|
|
classAndStructTypes.insert(new_scope->className);
|
2011-01-20 18:02:52 +01:00
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
// goto initial '{'
|
2011-01-21 07:42:41 +01:00
|
|
|
tok2 = initBaseInfo(new_scope, tok);
|
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-01-21 07:42:41 +01:00
|
|
|
if (!new_scope->classEnd)
|
2010-12-16 19:04:47 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
delete new_scope;
|
2010-12-16 19:04:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
scope = new_scope;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// add namespace
|
2011-01-21 07:42:41 +01:00
|
|
|
scopeList.push_back(scope);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
|
2011-01-20 18:02:52 +01:00
|
|
|
// forward declaration
|
|
|
|
else if (Token::Match(tok, "class|struct %var% ;"))
|
|
|
|
{
|
|
|
|
// fill the classAndStructTypes set..
|
|
|
|
classAndStructTypes.insert(tok->next()->str());
|
|
|
|
|
|
|
|
/** @todo save forward declarations in database someday */
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
else
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
// check for end of space
|
2011-01-21 07:42:41 +01:00
|
|
|
if (tok == scope->classEnd)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
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-01-21 07:42:41 +01: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;
|
2010-11-13 08:08:45 +01:00
|
|
|
else if (tok->str() == "public:")
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->access = Public;
|
2010-11-13 08:08:45 +01:00
|
|
|
else if (Token::Match(tok, "public|protected|private %var% :"))
|
|
|
|
{
|
|
|
|
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;
|
2010-11-13 08:08:45 +01:00
|
|
|
else if (tok->str() == "public")
|
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?
|
|
|
|
else if (tok->previous()->str() != "::" && isFunction(tok, &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-01-27 18:44:20 +01: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-01-21 07:42:41 +01:00
|
|
|
else if (function.tokenDef->str() == scope->className)
|
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;
|
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-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;
|
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
|
|
|
|
else if (tok->str() == "(")
|
|
|
|
{
|
|
|
|
function.retFuncPtr = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token *tok1 = tok;
|
|
|
|
|
|
|
|
// look for end of previous statement
|
|
|
|
while (tok1->previous() && !Token::Match(tok1->previous(), ";|}|{|public:|protected:|private:"))
|
|
|
|
{
|
|
|
|
// virtual function
|
|
|
|
if (tok1->previous()->str() == "virtual")
|
|
|
|
{
|
|
|
|
function.isVirtual = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static function
|
|
|
|
else if (tok1->previous()->str() == "static")
|
|
|
|
{
|
|
|
|
function.isStatic = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// friend function
|
|
|
|
else if (tok1->previous()->str() == "friend")
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if (Token::Match(end, ") const| = 0 ;"))
|
|
|
|
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| ;") ||
|
|
|
|
Token::Match(end, ") const| = 0 ;"))
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
else
|
|
|
|
{
|
|
|
|
function.isInline = true;
|
|
|
|
function.hasBody = true;
|
|
|
|
|
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);
|
|
|
|
if (scope)
|
|
|
|
scope->functionOf = functionOf;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 07:57:10 +01:00
|
|
|
// nested class or friend function?
|
2010-11-13 08:08:45 +01:00
|
|
|
else if (tok->previous()->str() == "::" && isFunction(tok, &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)
|
|
|
|
addFunction(&scope, &tok, argStart);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/** @todo handle friend functions */
|
|
|
|
}
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// friend class declaration?
|
|
|
|
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);
|
|
|
|
/** @todo fill this in later 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-01-21 07:42:41 +01: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?
|
|
|
|
if (isFunction(tok, &funcStart, &argStart))
|
|
|
|
{
|
|
|
|
// has body?
|
|
|
|
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() == "::")
|
2011-01-21 07:42:41 +01:00
|
|
|
addFunction(&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() == "~" &&
|
|
|
|
tok->previous()->previous() && tok->previous()->previous()->str() == "::")
|
2011-01-21 07:42:41 +01:00
|
|
|
addFunction(&scope, &tok, argStart);
|
2010-11-23 18:41:07 +01:00
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// regular function
|
|
|
|
else
|
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
Function function;
|
2010-11-20 07:26:50 +01:00
|
|
|
|
|
|
|
// 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.token = funcStart;
|
|
|
|
|
|
|
|
function.isInline = false;
|
|
|
|
function.hasBody = true;
|
|
|
|
function.arg = function.argDef;
|
2011-01-17 18:29:19 +01:00
|
|
|
function.type = Function::eFunction;
|
2010-11-20 07:26:50 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
addNewFunction(&scope, &tok);
|
2010-12-16 19:04:47 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope)
|
|
|
|
old_scope->functionList.push_back(function);
|
2011-01-23 22:31:35 +01:00
|
|
|
}
|
2010-12-16 19:04:47 +01:00
|
|
|
|
2011-01-23 22:31:35 +01:00
|
|
|
// syntax error
|
|
|
|
if (!scope)
|
|
|
|
{
|
|
|
|
scope = old_scope;
|
|
|
|
break;
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// function returning function pointer with body
|
|
|
|
else if (Token::simpleMatch(argStart->link(), ") ) (") &&
|
|
|
|
Token::Match(argStart->link()->tokAt(2)->link(), ") const| {"))
|
|
|
|
{
|
|
|
|
const Token *tok1 = funcStart;
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *old_scope = scope;
|
2010-11-20 07:26:50 +01:00
|
|
|
|
|
|
|
// class function
|
|
|
|
if (tok1->previous()->str() == "::")
|
2011-01-21 07:42:41 +01:00
|
|
|
addFunction(&scope, &tok1, argStart);
|
2010-11-20 07:26:50 +01:00
|
|
|
|
|
|
|
// regular function
|
|
|
|
else
|
2011-01-23 22:31:35 +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.token = funcStart;
|
|
|
|
|
|
|
|
function.isInline = false;
|
|
|
|
function.hasBody = true;
|
|
|
|
function.arg = function.argDef;
|
|
|
|
function.type = Function::eFunction;
|
|
|
|
function.retFuncPtr = true;
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
addNewFunction(&scope, &tok1);
|
2010-11-20 07:26:50 +01:00
|
|
|
|
2011-01-23 22:31:35 +01:00
|
|
|
if (scope)
|
|
|
|
old_scope->functionList.push_back(function);
|
|
|
|
}
|
|
|
|
|
2010-12-16 19:04:47 +01:00
|
|
|
// syntax error?
|
2011-01-21 07:42:41 +01:00
|
|
|
if (!scope)
|
2011-01-23 22:31:35 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
scope = old_scope;
|
2011-01-23 22:31:35 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-12-16 19:04:47 +01:00
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
tok = tok1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2010-12-02 07:35:01 +01:00
|
|
|
// fill in base class info
|
2011-01-21 07:42:41 +01:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +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-01-21 07:42:41 +01:00
|
|
|
for (unsigned int i = 0; i < scope->derivedFrom.size(); ++i)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Scope *>::iterator it1;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
for (it1 = scopeList.begin(); it1 != scopeList.end(); ++it1)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *scope1 = *it1;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
/** @todo handle derived base classes and namespaces */
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope1->type == Scope::eClass || scope1->type == Scope::eStruct)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
// do class names match?
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope1->className == scope->derivedFrom[i].name)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
// are they in the same namespace or different namespaces with same name?
|
2011-01-21 07:42:41 +01:00
|
|
|
if ((scope1->nestedIn == scope->nestedIn) ||
|
|
|
|
((scope1->nestedIn && scope1->nestedIn->type == Scope::eNamespace) &&
|
|
|
|
(scope->nestedIn && scope->nestedIn->type == Scope::eNamespace) &&
|
|
|
|
(scope1->nestedIn->className == scope->nestedIn->className)))
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->derivedFrom[i].scope = scope1;
|
2010-11-13 08:08:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2010-12-02 07:35:01 +01:00
|
|
|
// fill in variable info
|
2011-01-21 07:42:41 +01:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
scope = *it;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
|
|
|
// skip functions
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope->type != Scope::eFunction)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
|
|
|
// find variables
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->getVariableList();
|
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
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
unknowns = 0;
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +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)
|
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-01-21 07:42:41 +01:00
|
|
|
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
if (func->type == Function::eConstructor)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
|
|
|
// check for no arguments: func ( )
|
|
|
|
if (func->argDef->next() == func->argDef->link())
|
|
|
|
{
|
|
|
|
hasDefaultConstructor = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-01-15 07:59:37 +01:00
|
|
|
|
|
|
|
/** check for arguments with default values */
|
|
|
|
else if (func->argCount() == func->initializedArgCount())
|
|
|
|
{
|
|
|
|
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
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool needInitialization = false;
|
|
|
|
bool unknown = false;
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Variable>::const_iterator var;
|
2011-01-21 07:42:41 +01:00
|
|
|
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
2011-01-18 07:32:06 +01:00
|
|
|
if (var->isClass())
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
2011-01-18 07:32:06 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
needInitialization = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!unknown)
|
|
|
|
{
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retry++;
|
|
|
|
}
|
|
|
|
while (unknowns && retry < 100);
|
|
|
|
|
|
|
|
// this shouldn't happen so output a debug warning
|
|
|
|
if (retry == 100 && _settings->debugwarnings)
|
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +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)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
2011-01-21 07:42:41 +01:00
|
|
|
loc.line = scope->classDef->linenr();
|
|
|
|
loc.setfile(_tokenizer->file(scope->classDef));
|
2010-12-02 07:35:01 +01:00
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
Severity::debug,
|
|
|
|
"SymbolDatabase::SymbolDatabase couldn't resolve all user defined types.",
|
|
|
|
"debug");
|
|
|
|
if (_errorLogger)
|
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
|
|
|
}
|
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolDatabase::~SymbolDatabase()
|
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Scope *>::iterator it;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it)
|
2010-11-13 08:08:45 +01:00
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolDatabase::isFunction(const Token *tok, const Token **funcStart, const Token **argStart) const
|
|
|
|
{
|
|
|
|
// function returning function pointer? '... ( ... %var% ( ... ))( ... ) {'
|
|
|
|
if (tok->str() == "(" &&
|
|
|
|
tok->link()->previous()->str() == ")" &&
|
|
|
|
tok->link()->next() &&
|
|
|
|
tok->link()->next()->str() == "(" &&
|
|
|
|
tok->link()->next()->link()->next() &&
|
|
|
|
Token::Match(tok->link()->next()->link()->next(), "{|;|const|="))
|
|
|
|
{
|
|
|
|
*funcStart = tok->link()->previous()->link()->previous();
|
|
|
|
*argStart = tok->link()->previous()->link();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// regular function?
|
|
|
|
else if (Token::Match(tok, "%var% (") && Token::Match(tok->next()->link(), ") const| ;|{|=|:"))
|
|
|
|
{
|
|
|
|
*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;
|
|
|
|
while (first->str() == second->str())
|
|
|
|
{
|
|
|
|
// at end of argument list
|
|
|
|
if (first->str() == ")")
|
|
|
|
{
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip default value assignment
|
|
|
|
else if (first->next()->str() == "=")
|
|
|
|
first = first->tokAt(2);
|
|
|
|
|
|
|
|
// 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% ,|)")) &&
|
|
|
|
(first->next()->str() != second->next()->str()))
|
|
|
|
{
|
|
|
|
// skip variable names
|
|
|
|
first = first->next();
|
|
|
|
second = second->next();
|
|
|
|
|
|
|
|
// skip default value assignment
|
|
|
|
if (first->next()->str() == "=")
|
|
|
|
first = first->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// variable with class path
|
|
|
|
else if (depth && Token::Match(first->next(), "%var%"))
|
|
|
|
{
|
|
|
|
std::string param = path + first->next()->str();
|
|
|
|
|
|
|
|
if (Token::Match(second->next(), param.c_str()))
|
|
|
|
{
|
|
|
|
second = second->tokAt(int(depth) * 2);
|
|
|
|
}
|
|
|
|
else if (depth > 1)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
if (Token::Match(second->next(), param.c_str()))
|
|
|
|
{
|
|
|
|
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) == "::" &&
|
2010-12-07 19:42:30 +01:00
|
|
|
first->next()->str() == second->strAt(3))
|
|
|
|
{
|
|
|
|
second = second->tokAt(2);
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
first = first->next();
|
|
|
|
second = second->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
void SymbolDatabase::addFunction(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;
|
|
|
|
|
|
|
|
// skip class/struct name
|
|
|
|
if ((*tok)->previous()->str() == "~")
|
|
|
|
tok1 = (*tok)->tokAt(-3);
|
|
|
|
else
|
|
|
|
tok1 = (*tok)->tokAt(-2);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// back up to head of path
|
2010-11-20 07:26:50 +01: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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
path = tok1->str() + " :: " + path;
|
|
|
|
path_length++;
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Scope *>::iterator it1;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// search for match
|
2011-01-21 07:42:41 +01:00
|
|
|
for (it1 = scopeList.begin(); it1 != scopeList.end(); ++it1)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *scope1 = *it1;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
bool match = false;
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope1->className == tok1->str() && (scope1->type != Scope::eFunction))
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2010-11-20 07:26:50 +01:00
|
|
|
// do the spaces match (same space) 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() &&
|
|
|
|
(*scope)->type == scope1->nestedIn->type))
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
Scope *scope2 = scope1;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-21 07:42:41 +01: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-01-21 07:42:41 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match)
|
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
std::list<Function>::iterator func;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
for (func = scope1->functionList.begin(); func != scope1->functionList.end(); ++func)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
if (!func->hasBody)
|
|
|
|
{
|
2011-01-27 18:44:20 +01:00
|
|
|
if (func->type == Function::eDestructor &&
|
|
|
|
(*tok)->previous()->str() == "~" &&
|
|
|
|
func->tokenDef->str() == (*tok)->str())
|
2010-11-23 18:41:07 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01: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;
|
|
|
|
}
|
|
|
|
}
|
2010-12-03 07:35:06 +01:00
|
|
|
else if (func->tokenDef->str() == (*tok)->str() && (*tok)->previous()->str() != "~")
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
if (argsMatch(scope1, func->tokenDef->next(), (*tok)->next(), path, path_length))
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
// normal function?
|
|
|
|
if (!func->retFuncPtr && (*tok)->next()->link())
|
|
|
|
{
|
|
|
|
if ((func->isConst && (*tok)->next()->link()->next()->str() == "const") ||
|
|
|
|
(!func->isConst && (*tok)->next()->link()->next()->str() != "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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// function returning function pointer?
|
|
|
|
else if (func->retFuncPtr)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (func->hasBody)
|
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
addNewFunction(scope, tok);
|
|
|
|
if (scope)
|
2010-12-16 19:04:47 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
(*scope)->functionOf = scope1;
|
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-01-21 07:42:41 +01:00
|
|
|
Scope *new_scope = new Scope(this, tok1, *scope);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// skip to start of function
|
|
|
|
while (tok1 && tok1->str() != "{")
|
|
|
|
tok1 = tok1->next();
|
|
|
|
|
|
|
|
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-01-21 07:42:41 +01:00
|
|
|
if (!new_scope->classEnd)
|
2010-12-16 19:04:47 +01:00
|
|
|
{
|
2011-01-23 08:41:31 +01:00
|
|
|
(*scope)->nestedList.pop_back();
|
2011-01-21 07:42:41 +01:00
|
|
|
delete new_scope;
|
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
|
|
|
|
|
|
|
// add space
|
2011-01-21 07:42:41 +01:00
|
|
|
scopeList.push_back(new_scope);
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
*tok = tok1;
|
|
|
|
}
|
2011-01-22 08:36:47 +01:00
|
|
|
else
|
|
|
|
{
|
2011-01-23 08:41:31 +01:00
|
|
|
(*scope)->nestedList.pop_back();
|
2011-01-22 08:36:47 +01:00
|
|
|
delete new_scope;
|
|
|
|
*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;
|
|
|
|
while (tok2 && tok2->str() != "{")
|
|
|
|
{
|
|
|
|
// skip unsupported templates
|
|
|
|
if (tok2->str() == "<")
|
|
|
|
level++;
|
|
|
|
else if (tok2->str() == ">")
|
|
|
|
level--;
|
|
|
|
|
|
|
|
// check for base classes
|
|
|
|
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
|
|
|
|
|
|
|
tok2 = tok2->next();
|
|
|
|
|
|
|
|
if (tok2->str() == "public")
|
|
|
|
{
|
|
|
|
base.access = Public;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "protected")
|
|
|
|
{
|
|
|
|
base.access = Protected;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
else if (tok2->str() == "private")
|
|
|
|
{
|
|
|
|
base.access = Private;
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tok->str() == "class")
|
|
|
|
base.access = Private;
|
|
|
|
else if (tok->str() == "struct")
|
|
|
|
base.access = Public;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle derived base classes
|
|
|
|
while (Token::Match(tok2, "%var% ::"))
|
|
|
|
{
|
|
|
|
base.name += tok2->str();
|
|
|
|
base.name += " :: ";
|
|
|
|
tok2 = tok2->tokAt(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
base.name += tok2->str();
|
2011-01-21 07:42:41 +01:00
|
|
|
base.scope = 0;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2010-11-25 07:43:39 +01:00
|
|
|
// don't add unhandled templates
|
|
|
|
if (tok2->next()->str() == "<")
|
|
|
|
{
|
2010-11-30 19:52:42 +01:00
|
|
|
int level1 = 1;
|
|
|
|
while (tok2->next())
|
|
|
|
{
|
|
|
|
if (tok2->next()->str() == ">")
|
|
|
|
{
|
|
|
|
level1--;
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (tok2->next()->str() == "<")
|
|
|
|
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
|
2010-11-25 07:43:39 +01:00
|
|
|
else
|
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
scope->derivedFrom.push_back(base);
|
2010-11-25 07:43:39 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
tok2 = tok2->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok2;
|
|
|
|
}
|
2011-01-15 07:59:37 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
unsigned int Function::argCount() const
|
2011-01-15 07:59:37 +01:00
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
|
|
|
|
|
|
|
if (argDef->link() != argDef->next())
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
|
|
|
|
for (const Token *tok = argDef->next(); tok && tok->next() && tok->next() != argDef->link(); tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == ",")
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (argDef->link() != argDef->next())
|
|
|
|
{
|
|
|
|
for (const Token *tok = argDef->next(); tok && tok->next() && tok->next() != argDef->link(); tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "=")
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:08:45 +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),
|
2010-12-08 07:49:01 +01:00
|
|
|
functionOf(NULL)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2010-11-20 07:26:50 +01:00
|
|
|
if (!classDef)
|
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eGlobal;
|
2010-11-20 07:26:50 +01:00
|
|
|
access = Public;
|
|
|
|
}
|
|
|
|
else if (classDef->str() == "class")
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
else if (classDef->str() == "struct")
|
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eStruct;
|
2010-11-13 08:08:45 +01:00
|
|
|
className = classDef->next()->str();
|
|
|
|
access = Public;
|
|
|
|
}
|
|
|
|
else if (classDef->str() == "union")
|
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
type = Scope::eUnion;
|
2010-11-13 08:08:45 +01:00
|
|
|
className = classDef->next()->str();
|
|
|
|
access = Public;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nestedIn)
|
|
|
|
nestedIn->nestedList.push_back(this);
|
|
|
|
}
|
|
|
|
|
2010-12-21 08:13:40 +01:00
|
|
|
bool
|
2011-01-17 18:29:19 +01:00
|
|
|
Scope::hasDefaultConstructor() const
|
2010-12-21 08:13:40 +01: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
|
|
|
|
|
|
|
for (func = functionList.begin(); func != functionList.end(); ++func)
|
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
if (func->type == Function::eConstructor &&
|
2010-12-21 08:13:40 +01:00
|
|
|
func->argDef->link() == func->argDef->next())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-01-17 18:29:19 +01:00
|
|
|
AccessControl varaccess = type == eClass ? Private : Public;
|
2010-11-20 07:26:50 +01:00
|
|
|
const Token *start;
|
|
|
|
|
|
|
|
if (classStart)
|
|
|
|
start = classStart->next();
|
|
|
|
else
|
|
|
|
start = check->_tokenizer->tokens();
|
|
|
|
|
|
|
|
for (const Token *tok = start; tok; tok = tok->next())
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2010-12-16 19:04:47 +01:00
|
|
|
// end of space?
|
2010-11-20 07:26:50 +01:00
|
|
|
if (tok->str() == "}")
|
2010-11-13 08:08:45 +01:00
|
|
|
break;
|
|
|
|
|
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?
|
|
|
|
else if (tok->str() == "{")
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
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?
|
|
|
|
else if (Token::Match(tok, "class|struct|union|namespace %type% :|{"))
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
while (tok && tok->str() != "{")
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok)
|
|
|
|
{
|
|
|
|
// skip implementation
|
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
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.
|
2010-11-20 07:26:50 +01:00
|
|
|
else if (tok->str() == "__published:")
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
for (; tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == "{")
|
|
|
|
tok = tok->link();
|
|
|
|
if (Token::Match(tok->next(), "private:|protected:|public:"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "private:" "public:" "protected:" etc
|
2010-11-20 07:26:50 +01: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;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
else if (tok->str() == "protected:")
|
|
|
|
{
|
|
|
|
varaccess = Protected;
|
2010-11-20 07:26:50 +01:00
|
|
|
continue;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
else if (tok->str() == "private:")
|
|
|
|
{
|
|
|
|
varaccess = Private;
|
|
|
|
continue;
|
2010-11-20 07:26:50 +01:00
|
|
|
}
|
2010-11-13 08:08:45 +01:00
|
|
|
|
|
|
|
// Is it a forward declaration?
|
2010-11-20 07:26:50 +01: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;
|
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// Search for start of statement..
|
|
|
|
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;
|
2010-11-13 08:08:45 +01:00
|
|
|
|
2010-11-20 07:26:50 +01:00
|
|
|
// This is the start of a statement
|
2010-12-02 07:35:01 +01:00
|
|
|
const Token *vartok = NULL;
|
|
|
|
const Token *typetok = NULL;
|
2010-11-20 07:26:50 +01:00
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
// Is it const..?
|
2010-11-16 07:30:55 +01:00
|
|
|
bool isConst = false;
|
2010-11-20 07:26:50 +01:00
|
|
|
if (tok->str() == "const")
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2010-11-20 07:26:50 +01:00
|
|
|
tok = tok->next();
|
2010-11-16 07:30:55 +01:00
|
|
|
isConst = true;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is it a static variable?
|
2010-11-20 07:26:50 +01:00
|
|
|
const bool isStatic(Token::simpleMatch(tok, "static"));
|
2010-11-13 08:08:45 +01:00
|
|
|
if (isStatic)
|
|
|
|
{
|
2010-11-20 07:26:50 +01:00
|
|
|
tok = tok->next();
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is it a mutable variable?
|
2010-11-20 07:26:50 +01:00
|
|
|
const bool isMutable(Token::simpleMatch(tok, "mutable"));
|
2010-11-13 08:08:45 +01:00
|
|
|
if (isMutable)
|
|
|
|
{
|
2010-11-20 07:26:50 +01:00
|
|
|
tok = tok->next();
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is it const..?
|
2010-11-20 07:26:50 +01:00
|
|
|
if (tok->str() == "const")
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2010-11-20 07:26:50 +01:00
|
|
|
tok = tok->next();
|
2010-11-16 07:30:55 +01:00
|
|
|
isConst = true;
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isClass = false;
|
2010-11-20 07:26:50 +01:00
|
|
|
|
2010-12-30 22:57:43 +01:00
|
|
|
if (Token::Match(tok, "struct|union"))
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
tok = tok->next();
|
2010-11-13 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2010-12-30 22:57:43 +01:00
|
|
|
if (isVariableDeclaration(tok, vartok, typetok))
|
2010-11-20 07:26:50 +01:00
|
|
|
{
|
2011-01-02 00:36:22 +01:00
|
|
|
isClass = (!typetok->isStandardType() && vartok->previous()->str() != "*");
|
2010-11-20 07:26:50 +01:00
|
|
|
tok = vartok->next();
|
|
|
|
}
|
2011-01-01 01:19:32 +01:00
|
|
|
|
2010-11-13 08:08:45 +01:00
|
|
|
// If the vartok was set in the if-blocks above, create a entry for this variable..
|
|
|
|
if (vartok && vartok->str() != "operator")
|
|
|
|
{
|
2010-12-07 07:07:07 +01:00
|
|
|
if (vartok->varId() == 0 && check->_settings->debugwarnings)
|
2010-11-13 08:08:45 +01:00
|
|
|
{
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = vartok->linenr();
|
|
|
|
loc.setfile(check->_tokenizer->file(vartok));
|
|
|
|
locationList.push_back(loc);
|
|
|
|
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locationList,
|
|
|
|
Severity::debug,
|
2011-01-17 18:29:19 +01:00
|
|
|
"Scope::getVariableList found variable \'" + vartok->str() + "\' with varid 0.",
|
2010-11-13 08:08:45 +01:00
|
|
|
"debug");
|
|
|
|
if (check->_errorLogger)
|
|
|
|
check->_errorLogger->reportErr(errmsg);
|
|
|
|
else
|
|
|
|
Check::reportError(errmsg);
|
|
|
|
}
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
const Scope *scope = NULL;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2010-12-04 07:29:12 +01:00
|
|
|
if (typetok)
|
2011-01-21 07:42:41 +01:00
|
|
|
scope = check->findVariableType(this, typetok);
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
addVariable(vartok, varaccess, isMutable, isStatic, isConst, isClass, scope);
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-30 22:57:43 +01:00
|
|
|
const Token* skipScopeIdentifiers(const Token* tok)
|
2010-12-30 09:46:44 +01:00
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
const Token* ret = tok;
|
|
|
|
|
|
|
|
if (Token::simpleMatch(ret, "::"))
|
2010-12-30 09:46:44 +01:00
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
ret = ret->next();
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
2011-02-02 16:41:43 +01:00
|
|
|
while (Token::Match(ret, "%type% ::"))
|
2010-12-30 09:46:44 +01:00
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
ret = ret->tokAt(2);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
2010-12-30 22:57:43 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token* skipPointers(const Token* tok)
|
|
|
|
{
|
|
|
|
const Token* ret = tok;
|
|
|
|
|
|
|
|
while (Token::simpleMatch(ret, "*"))
|
2010-12-30 09:46:44 +01:00
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
ret = ret->next();
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
2010-12-30 22:57:43 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
bool Scope::isVariableDeclaration(const Token* tok, const Token*& vartok, const Token*& typetok) const
|
2010-12-30 22:57:43 +01:00
|
|
|
{
|
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-02-02 17:12:46 +01:00
|
|
|
if (Token::Match(localTypeTok, "%type% <"))
|
2011-01-02 00:36:22 +01:00
|
|
|
{
|
2011-01-18 10:14:12 +01:00
|
|
|
const Token* closeTok = NULL;
|
|
|
|
bool found = findClosingBracket(localTypeTok->next(), closeTok);
|
2011-01-02 00:36:22 +01:00
|
|
|
if (found)
|
|
|
|
{
|
2011-01-18 10:51:57 +01:00
|
|
|
localVarTok = skipPointers(closeTok->next());
|
2011-01-18 10:14:12 +01:00
|
|
|
|
2011-01-18 10:51:57 +01:00
|
|
|
if (Token::Match(localVarTok, ":: %type% %var% ;"))
|
2011-01-02 00:36:22 +01:00
|
|
|
{
|
2011-01-18 10:51:57 +01:00
|
|
|
localTypeTok = localVarTok->next();
|
|
|
|
localVarTok = localVarTok->tokAt(2);
|
2011-01-02 00:36:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Token::Match(localTypeTok, "%type%"))
|
|
|
|
{
|
2011-01-18 10:51:57 +01:00
|
|
|
localVarTok = skipPointers(localTypeTok->next());
|
|
|
|
}
|
2010-12-30 22:57:43 +01:00
|
|
|
|
2011-01-18 10:51:57 +01:00
|
|
|
if (isSimpleVariable(localVarTok) || isArrayVariable(localVarTok))
|
|
|
|
{
|
|
|
|
vartok = localVarTok;
|
|
|
|
typetok = localTypeTok;
|
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::isSimpleVariable(const Token* tok) const
|
2011-01-01 01:19:32 +01:00
|
|
|
{
|
|
|
|
return Token::Match(tok, "%var% ;");
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:29:19 +01:00
|
|
|
bool Scope::isArrayVariable(const Token* tok) const
|
2011-01-01 01:19:32 +01:00
|
|
|
{
|
|
|
|
return Token::Match(tok, "%var% [") && tok->next()->str() != "operator";
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (NULL != tok && tok->str() == "<")
|
|
|
|
{
|
|
|
|
unsigned int depth = 0;
|
|
|
|
for (close = tok; (close != NULL) && (close->str() != ";"); close = close->next())
|
|
|
|
{
|
|
|
|
if (close->str() == "<")
|
|
|
|
{
|
|
|
|
++depth;
|
|
|
|
}
|
|
|
|
else if (close->str() == ">")
|
|
|
|
{
|
|
|
|
if (--depth == 0)
|
|
|
|
{
|
|
|
|
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-01-17 18:29:19 +01:00
|
|
|
std::list<Scope *>::const_iterator it;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
for (it = scopeList.begin(); it != scopeList.end(); ++it)
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
2011-01-21 07:42:41 +01:00
|
|
|
const Scope *scope = *it;
|
2010-12-02 07:35:01 +01:00
|
|
|
|
|
|
|
// skip namespaces and functions
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope->type == Scope::eNamespace || scope->type == Scope::eFunction || scope->type == Scope::eGlobal)
|
2010-12-02 07:35:01 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// do the names match?
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope->className == type->str())
|
2010-12-02 07:35:01 +01:00
|
|
|
{
|
|
|
|
// check if type does not have a namespace
|
|
|
|
if (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-01-21 07:42:41 +01:00
|
|
|
while (parent && parent != scope->nestedIn)
|
2010-12-02 07:35:01 +01:00
|
|
|
parent = parent->nestedIn;
|
|
|
|
|
2011-01-21 07:42:41 +01:00
|
|
|
if (scope->nestedIn == parent)
|
|
|
|
return scope;
|
2010-12-02 07:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// type has a namespace
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// FIXME check if namespace path matches supplied path
|
2011-01-21 07:42:41 +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
|
|
|
|
{
|
|
|
|
std::list<Scope *>::const_iterator scope;
|
|
|
|
|
|
|
|
for (scope = scopeList.begin(); scope != scopeList.end(); ++scope)
|
|
|
|
{
|
|
|
|
if ((*scope)->type == Scope::eFunction)
|
|
|
|
{
|
|
|
|
if ((*scope)->classDef == tok)
|
|
|
|
return (*scope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const Function *SymbolDatabase::findFunctionByToken(const Token *tok) const
|
|
|
|
{
|
|
|
|
std::list<Scope *>::const_iterator scope;
|
|
|
|
|
|
|
|
for (scope = scopeList.begin(); scope != scopeList.end(); ++scope)
|
|
|
|
{
|
|
|
|
std::list<Function>::const_iterator func;
|
|
|
|
|
|
|
|
for (func = (*scope)->functionList.begin(); func != (*scope)->functionList.end(); ++func)
|
|
|
|
{
|
|
|
|
if (func->token == tok)
|
|
|
|
return &*func;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2010-11-13 08:08:45 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
for (it = nestedList.begin(); it != nestedList.end(); ++it)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (it = nestedList.begin(); it != nestedList.end(); ++it)
|
|
|
|
{
|
|
|
|
if ((*it)->className == name)
|
|
|
|
return (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (it = nestedList.begin(); it != nestedList.end(); ++it)
|
|
|
|
{
|
|
|
|
Scope *child = (*it)->findInNestedListRecursive(name);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
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;
|
2010-11-13 08:08:45 +01:00
|
|
|
for (it = functionList.begin(); it != functionList.end(); ++it)
|
|
|
|
{
|
2011-01-17 18:29:19 +01:00
|
|
|
if (it->type == Function::eDestructor)
|
2010-11-13 08:08:45 +01:00
|
|
|
return &*it;
|
|
|
|
}
|
|
|
|
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;
|
2010-11-13 08:08:45 +01: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;
|
|
|
|
}
|