virtualCallInConstructor: Updated warning message
This commit is contained in:
parent
3b07b749d6
commit
b3b364b42f
|
@ -2312,12 +2312,30 @@ void CheckClass::virtualFunctionCallInConstructorError(
|
||||||
const char * scopeFunctionTypeName = scopeFunction ? getFunctionTypeName(scopeFunction->type) : "constructor";
|
const char * scopeFunctionTypeName = scopeFunction ? getFunctionTypeName(scopeFunction->type) : "constructor";
|
||||||
|
|
||||||
ErrorPath errorPath;
|
ErrorPath errorPath;
|
||||||
|
int lineNumber = 1;
|
||||||
for (std::list<const Token *>::const_iterator it = tokStack.begin(); it != tokStack.end(); ++it)
|
for (std::list<const Token *>::const_iterator it = tokStack.begin(); it != tokStack.end(); ++it)
|
||||||
errorPath.push_back(ErrorPathItem(*it, "Calling " + (*it)->str()));
|
errorPath.push_back(ErrorPathItem(*it, "Calling " + (*it)->str()));
|
||||||
if (!errorPath.empty())
|
if (!errorPath.empty()) {
|
||||||
|
lineNumber = errorPath.front().first->linenr();
|
||||||
errorPath.back().second = funcname + " is a virtual method";
|
errorPath.back().second = funcname + " is a virtual method";
|
||||||
|
}
|
||||||
|
|
||||||
reportError(errorPath, Severity::warning, "virtualCallInConstructor", "Call of virtual function '" + funcname + "' in " + scopeFunctionTypeName + ".\n"
|
std::string constructorName;
|
||||||
|
if (scopeFunction) {
|
||||||
|
const Token *endToken = scopeFunction->argDef->link()->next();
|
||||||
|
if (scopeFunction->type == Function::Type::eDestructor)
|
||||||
|
constructorName = "~";
|
||||||
|
for (const Token *tok = scopeFunction->tokenDef; tok != endToken; tok = tok->next()) {
|
||||||
|
if (!constructorName.empty() && Token::Match(tok->previous(), "%name%|%num% %name%|%num%"))
|
||||||
|
constructorName += ' ';
|
||||||
|
constructorName += tok->str();
|
||||||
|
if (tok->str() == ")")
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reportError(errorPath, Severity::warning, "virtualCallInConstructor",
|
||||||
|
"Virtual function '" + funcname + "' is called from " + scopeFunctionTypeName + " '" + constructorName + "' at line " + MathLib::toString(lineNumber) + ".\n"
|
||||||
"Call of pure virtual function '" + funcname + "' in " + scopeFunctionTypeName + ". Dynamic binding is not used.", CWE(0U), false);
|
"Call of pure virtual function '" + funcname + "' in " + scopeFunctionTypeName + ". Dynamic binding is not used.", CWE(0U), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6346,7 +6346,7 @@ private:
|
||||||
"};\n"
|
"};\n"
|
||||||
"A::A()\n"
|
"A::A()\n"
|
||||||
"{f();}\n");
|
"{f();}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (warning) Call of virtual function 'f' in constructor.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (warning) Virtual function 'f' is called from constructor 'A()' at line 7.\n", errout.str());
|
||||||
|
|
||||||
checkVirtualFunctionCall("class A\n"
|
checkVirtualFunctionCall("class A\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
|
Loading…
Reference in New Issue