diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index a0f85051a..7e98d1abf 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2312,12 +2312,30 @@ void CheckClass::virtualFunctionCallInConstructorError( const char * scopeFunctionTypeName = scopeFunction ? getFunctionTypeName(scopeFunction->type) : "constructor"; ErrorPath errorPath; + int lineNumber = 1; for (std::list::const_iterator it = tokStack.begin(); it != tokStack.end(); ++it) 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"; + } - 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); } diff --git a/test/testclass.cpp b/test/testclass.cpp index c804d77f2..95aec8747 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -6346,7 +6346,7 @@ private: "};\n" "A::A()\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" "{\n"