Symbol database: reuse in CheckOther. Ticket: #2318

This commit is contained in:
Robert Reif 2010-12-16 19:04:47 +01:00 committed by Daniel Marjamäki
parent bf136f0123
commit f2d69acbfd
2 changed files with 128 additions and 70 deletions

View File

@ -20,6 +20,7 @@
//---------------------------------------------------------------------------
#include "checkother.h"
#include "mathlib.h"
#include "symboldatabase.h"
#include <cctype> // std::isupper
#include <cmath> // fabs()
@ -1192,17 +1193,20 @@ void CheckOther::functionVariableUsage()
return;
// Parse all executing scopes..
for (const Token *token = Token::findmatch(_tokenizer->tokens(), ") const| {"); token;)
SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
std::list<SymbolDatabase::SpaceInfo *>::const_iterator i;
for (i = symbolDatabase->spaceInfoList.begin(); i != symbolDatabase->spaceInfoList.end(); ++i)
{
// goto "{"
while (token->str() != "{")
token = token->next();
SymbolDatabase::SpaceInfo *info = *i;
// only check functions
if (info->type != SymbolDatabase::SpaceInfo::Function)
continue;
// First token for the current scope..
const Token *const tok1 = token;
// Find next scope that will be checked next time..
token = Token::findmatch(token->link(), ") const| {");
const Token *const tok1 = info->classStart;
// varId, usage {read, write, modified}
Variables variables;
@ -1820,13 +1824,24 @@ void CheckOther::checkVariableScope()
if (!_settings->_checkCodingStyle)
return;
// Walk through all tokens..
bool func = false;
int indentlevel = 0;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
std::list<SymbolDatabase::SpaceInfo *>::const_iterator i;
for (i = symbolDatabase->spaceInfoList.begin(); i != symbolDatabase->spaceInfoList.end(); ++i)
{
// Skip class and struct declarations..
if ((tok->str() == "class") || (tok->str() == "struct"))
SymbolDatabase::SpaceInfo *info = *i;
// only check functions
if (info->type != SymbolDatabase::SpaceInfo::Function)
continue;
// Walk through all tokens..
int indentlevel = 0;
for (const Token *tok = info->classStart; tok; tok = tok->next())
{
// Skip function local class and struct declarations..
if ((tok->str() == "class") || (tok->str() == "struct") || (tok->str() == "union"))
{
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
@ -1852,13 +1867,10 @@ void CheckOther::checkVariableScope()
{
--indentlevel;
if (indentlevel == 0)
func = false;
break;;
}
if (indentlevel == 0 && Token::simpleMatch(tok, ") {"))
{
func = true;
}
if (indentlevel > 0 && func && Token::Match(tok, "[{};]"))
if (indentlevel > 0 && Token::Match(tok, "[{};]"))
{
// First token of statement..
const Token *tok1 = tok->next();
@ -1891,6 +1903,7 @@ void CheckOther::checkVariableScope()
}
}
}
}
}
//---------------------------------------------------------------------------

View File

@ -59,6 +59,13 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
new_info->classStart = tok2;
new_info->classEnd = tok2->link();
// make sure we have valid code
if (!new_info->classEnd)
{
delete new_info;
break;
}
info = new_info;
// add namespace
@ -221,6 +228,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
SpaceInfo *functionOf = info;
addNewFunction(&info, &tok2);
if (info)
info->functionOf = functionOf;
tok = tok2;
@ -283,9 +291,19 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
function.arg = function.argDef;
function.type = Func::Function;
info->functionList.push_back(function);
SpaceInfo *old_info = info;
addNewFunction(&info, &tok);
if (info)
old_info->functionList.push_back(function);
// syntax error
else
{
info = old_info;
break;
}
}
}
@ -294,6 +312,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
Token::Match(argStart->link()->tokAt(2)->link(), ") const| {"))
{
const Token *tok1 = funcStart;
SpaceInfo *old_info = info;
// class function
if (tok1->previous()->str() == "::")
@ -303,6 +322,10 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
else
addNewFunction(&info, &tok1);
// syntax error?
if (!info)
info = old_info;
tok = tok1;
}
}
@ -747,8 +770,11 @@ void SymbolDatabase::addFunction(SpaceInfo **info, const Token **tok, const Toke
if (func->hasBody)
{
addNewFunction(info, tok);
if (info)
{
(*info)->functionOf = info1;
added = true;
}
break;
}
}
@ -775,6 +801,17 @@ void SymbolDatabase::addNewFunction(SymbolDatabase::SpaceInfo **info, const Toke
new_info->classStart = tok1;
new_info->classEnd = tok1->link();
// syntax error?
if (!new_info->classEnd)
{
delete new_info;
while (tok1->next())
tok1 = tok1->next();
*info = NULL;
*tok = tok1;
return;
}
*info = new_info;
// add space
@ -933,13 +970,21 @@ void SymbolDatabase::SpaceInfo::getVarList()
for (const Token *tok = start; tok; tok = tok->next())
{
// end of space?
if (tok->str() == "}")
break;
// syntax error?
else if (tok->next() == NULL)
break;
// Is it a function?
else if (tok->str() == "{")
{
tok = tok->link();
// syntax error?
if (!tok)
return;
continue;
}