MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / virtualFunctionCallInConstructorError

Method virtualFunctionCallInConstructorError

lib/checkclass.cpp:3054–3090  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3052}
3053
3054void CheckClassImpl::virtualFunctionCallInConstructorError(
3055 const Function * scopeFunction,
3056 const std::list<const Token *> & tokStack,
3057 const std::string &funcname)
3058{
3059 if (scopeFunction && !mSettings.severity.isEnabled(Severity::style) && !mSettings.isPremiumEnabled("virtualCallInConstructor"))
3060 return;
3061
3062 const char * scopeFunctionTypeName = scopeFunction ? getFunctionTypeName(scopeFunction->type) : "constructor";
3063
3064 ErrorPath errorPath;
3065 std::transform(tokStack.cbegin(), tokStack.cend(), std::back_inserter(errorPath), [](const Token* tok) {
3066 return ErrorPathItem(tok, "Calling " + tok->str());
3067 });
3068 int lineNumber = 1;
3069 if (!errorPath.empty()) {
3070 lineNumber = errorPath.front().first->linenr();
3071 errorPath.back().second = funcname + " is a virtual function";
3072 }
3073
3074 std::string constructorName;
3075 if (scopeFunction) {
3076 const Token *endToken = scopeFunction->argDef->link()->next();
3077 if (scopeFunction->type == FunctionType::eDestructor)
3078 constructorName = "~";
3079 for (const Token *tok = scopeFunction->tokenDef; tok != endToken; tok = tok->next()) {
3080 if (!constructorName.empty() && Token::Match(tok->previous(), "%name%|%num% %name%|%num%"))
3081 constructorName += ' ';
3082 constructorName += tok->str();
3083 if (tok->str() == ")")
3084 break;
3085 }
3086 }
3087
3088 reportError(std::move(errorPath), Severity::style, "virtualCallInConstructor",
3089 "Virtual function '" + funcname + "' is called from " + scopeFunctionTypeName + " '" + constructorName + "' at line " + std::to_string(lineNumber) + ". Dynamic binding is not used.", CWE(0U), Certainty::normal);
3090}
3091
3092void CheckClassImpl::pureVirtualFunctionCallInConstructorError(
3093 const Function * scopeFunction,

Callers 1

getErrorMessagesMethod · 0.80

Calls 9

getFunctionTypeNameFunction · 0.85
isPremiumEnabledMethod · 0.80
frontMethod · 0.80
nextMethod · 0.80
reportErrorFunction · 0.70
CWEClass · 0.70
isEnabledMethod · 0.45
strMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected