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:
parent
32e1831716
commit
67915749b0
|
@ -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 ||
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue