Symbol database: fixed linenr problem in virtual destructors check

This commit is contained in:
Robert Reif 2010-08-13 23:57:53 +02:00 committed by Daniel Marjamäki
parent e9a3d03372
commit db78c3acdf
2 changed files with 35 additions and 37 deletions

View File

@ -1583,9 +1583,8 @@ void CheckClass::virtualDestructor()
for (unsigned int j = 0; j < info->derivedFrom.size(); ++j) for (unsigned int j = 0; j < info->derivedFrom.size(); ++j)
{ {
// Check if base class is public and exists in database // Check if base class is public and exists in database
if (info->derivedFrom[j].access != Public || !info->derivedFrom[j].spaceInfo) if (info->derivedFrom[j].access == Public && info->derivedFrom[j].spaceInfo)
continue; {
const SpaceInfo *spaceInfo = info->derivedFrom[j].spaceInfo; const SpaceInfo *spaceInfo = info->derivedFrom[j].spaceInfo;
// Name of base class.. // Name of base class..
@ -1595,28 +1594,24 @@ void CheckClass::virtualDestructor()
const Func *base_destructor = spaceInfo->getDestructor(); const Func *base_destructor = spaceInfo->getDestructor();
const Token *base = 0; const Token *base = 0;
if (base_destructor) if (base_destructor)
base = base_destructor->token->tokAt(-2); base = base_destructor->token;
// Check that there is a destructor.. // Check that there is a destructor..
if (!base_destructor) if (!base_destructor)
{ {
if (spaceInfo->derivedFrom.empty()) if (spaceInfo->derivedFrom.empty())
virtualDestructorError(spaceInfo->classDef, baseName, derivedClass->str()); virtualDestructorError(spaceInfo->classDef, baseName, derivedClass->str());
continue;
} }
else if (base_destructor->isVirtual) else if (!base_destructor->isVirtual)
continue; {
// TODO: This is just a temporary fix, better solution is needed. // TODO: This is just a temporary fix, better solution is needed.
// Skip situations where base class has base classes of its own, because // Skip situations where base class has base classes of its own, because
// some of the base classes might have virtual destructor. // some of the base classes might have virtual destructor.
// Proper solution is to check all of the base classes. If base class is not // Proper solution is to check all of the base classes. If base class is not
// found or if one of the base classes has virtual destructor, error should not // found or if one of the base classes has virtual destructor, error should not
// be printed. See TODO test case "virtualDestructorInherited" // be printed. See TODO test case "virtualDestructorInherited"
if (!Token::findmatch(_tokenizer->tokens(), (std::string("class ") + baseName + " {").c_str())) if (spaceInfo->derivedFrom.empty())
continue; {
// Make sure that the destructor is public (protected or private // Make sure that the destructor is public (protected or private
// would not compile if inheritance is used in a way that would // would not compile if inheritance is used in a way that would
// cause the bug we are trying to find here.) // cause the bug we are trying to find here.)
@ -1624,6 +1619,9 @@ void CheckClass::virtualDestructor()
virtualDestructorError(base, baseName, derivedClass->str()); virtualDestructorError(base, baseName, derivedClass->str());
} }
} }
}
}
}
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -1418,7 +1418,7 @@ private:
" public:\n" " public:\n"
" ~B(){int a;}\n" " ~B(){int a;}\n"
"};\n"); "};\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Class AA<double> which is inherited by class B does not have a virtual destructor\n", errout.str()); ASSERT_EQUALS("[test.cpp:9]: (error) Class AA<double> which is inherited by class B does not have a virtual destructor\n", errout.str());
} }
void checkUninitVar(const char code[]) void checkUninitVar(const char code[])