Fixed #2537 (segmentation fault of cppcheck)

This commit is contained in:
Robert Reif 2011-02-03 07:57:10 +01:00 committed by Daniel Marjamäki
parent 43b0e655bb
commit 8288c28b3f
3 changed files with 53 additions and 2 deletions

View File

@ -254,9 +254,19 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
}
}
// nested class function?
// nested class or friend function?
else if (tok->previous()->str() == "::" && isFunction(tok, &funcStart, &argStart))
addFunction(&scope, &tok, argStart);
{
/** @todo check entire qualification for match */
Scope * nested = scope->findInNestedListRecursive(tok->strAt(-2));
if (nested)
addFunction(&scope, &tok, argStart);
else
{
/** @todo handle friend functions */
}
}
// friend class declaration?
else if (Token::Match(tok, "friend class| %any% ;"))
@ -1398,6 +1408,26 @@ Scope * Scope::findInNestedList(const std::string & name)
//---------------------------------------------------------------------------
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;
}
//---------------------------------------------------------------------------
const Function *Scope::getDestructor() const
{
std::list<Function>::const_iterator it;

View File

@ -311,6 +311,12 @@ public:
*/
Scope * findInNestedList(const std::string & name);
/**
* @brief find if name is in nested list
* @param name name of nested scope
*/
Scope * findInNestedListRecursive(const std::string & name);
void addVariable(const Token *token_, AccessControl access_, bool mutable_, bool static_, bool const_, bool class_, const Scope *type_)
{
varlist.push_back(Variable(token_, varlist.size(), access_, mutable_, static_, const_, class_, type_));

View File

@ -188,6 +188,7 @@ private:
TEST_CASE(symboldatabase7); // ticket #2230
TEST_CASE(symboldatabase8); // ticket #2252
TEST_CASE(symboldatabase9); // ticket #2525
TEST_CASE(symboldatabase10); // ticket #2537
}
// Check the operator Equal
@ -5490,6 +5491,20 @@ private:
ASSERT_EQUALS("", errout.str());
}
void symboldatabase10()
{
// ticket #2537 - segmentation fault
checkConst("class A {\n"
"private:\n"
" void f();\n"
"};\n"
"class B {\n"
" friend void A::f();\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestClass)