Fixed #4723 (False positive: Pure virtual call within conditional clause)

conditional clauses directly in constructor/destructor cannot prevent
pure virtual function call otherwise this part of the code would never been called
This commit is contained in:
Frank Zingsheim 2013-09-27 09:25:38 +02:00
parent 32e1831716
commit 67915749b0
2 changed files with 33 additions and 8 deletions

View File

@ -1996,14 +1996,19 @@ const std::list<const Token *> & CheckClass::callsPureVirtualFunction(const Func
for (const Token *tok = function.arg->link();
tok && tok != function.functionScope->classEnd;
tok = tok->next()) {
if ((Token::simpleMatch(tok,") {") &&
tok->link() &&
Token::Match(tok->link()->previous(),"if|switch")) ||
Token::simpleMatch(tok,"else {")
) {
// Assume pure virtual function call is prevented by "if|else|switch" condition
tok = tok->linkAt(1);
continue;
if (function.type != Function::eConstructor &&
function.type != Function::eCopyConstructor &&
function.type != Function::eMoveConstructor &&
function.type != Function::eDestructor) {
if ((Token::simpleMatch(tok,") {") &&
tok->link() &&
Token::Match(tok->link()->previous(),"if|switch")) ||
Token::simpleMatch(tok,"else {")
) {
// Assume pure virtual function call is prevented by "if|else|switch" condition
tok = tok->linkAt(1);
continue;
}
}
const Function * callFunction=tok->function();
if (!callFunction ||

View File

@ -5679,6 +5679,26 @@ private:
"A::~A()\n"
"{nonpure();}\n");
ASSERT_EQUALS("[test.cpp:10] -> [test.cpp:5] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in destructor.\n", errout.str());
checkPureVirtualFunctionCall("class A\n"
"{\n"
" virtual void pure()=0;\n"
" A(bool b);\n"
"};\n"
"A::A(bool b)\n"
"{if (b) pure();}\n");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in constructor.\n", errout.str());
checkPureVirtualFunctionCall("class A\n"
" {\n"
" virtual void pure()=0; \n"
" virtual ~A(); \n"
" int m; \n"
"};\n"
"A::~A()\n"
"{if (b) pure();}\n");
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in destructor.\n", errout.str());
}
void pureVirtualFunctionCallOtherClass() {