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

Function checkFunctionUsage

lib/checkclass.cpp:1315–1363  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1313//---------------------------------------------------------------------------
1314
1315static bool checkFunctionUsage(const Function *privfunc, const Scope* scope)
1316{
1317 if (!scope)
1318 return true; // Assume it is used, if scope is not seen
1319
1320 for (auto func = scope->functionList.cbegin(); func != scope->functionList.cend(); ++func) {
1321 if (func->functionScope) {
1322 if (Token::Match(func->tokenDef, "%name% (")) {
1323 for (const Token *ftok = func->tokenDef->tokAt(2); ftok && ftok->str() != ")"; ftok = ftok->next()) {
1324 if (Token::Match(ftok, "= %name% [(,)]") && ftok->strAt(1) == privfunc->name())
1325 return true;
1326 if (ftok->str() == "(")
1327 ftok = ftok->link();
1328 }
1329 }
1330 for (const Token *ftok = func->functionScope->classDef->linkAt(1); ftok != func->functionScope->bodyEnd; ftok = ftok->next()) {
1331 if (ftok->function() == privfunc)
1332 return true;
1333 if (ftok->varId() == 0U && ftok->str() == privfunc->name()) // TODO: This condition should be redundant
1334 return true;
1335 }
1336 } else if ((func->type != FunctionType::eCopyConstructor &&
1337 func->type != FunctionType::eOperatorEqual &&
1338 !func->isDefault() && !func->isDelete()) ||
1339 func->access != AccessControl::Private) // Assume it is used, if a function implementation isn't seen, but empty private copy constructors and assignment operators are OK
1340 return true;
1341 }
1342
1343 for (auto iter = scope->definedTypesMap.cbegin(); iter != scope->definedTypesMap.cend(); ++iter) {
1344 const Type *type = iter->second;
1345 if (type->enclosingScope == scope && checkFunctionUsage(privfunc, type->classScope))
1346 return true;
1347 }
1348
1349 for (const Variable &var : scope->varlist) {
1350 if (var.isStatic()) {
1351 const Token* tok = Token::findmatch(scope->bodyStart, "%varid% =|(|{", var.declarationId());
1352 if (tok)
1353 tok = tok->tokAt(2);
1354 while (tok && tok->str() != ";") {
1355 if (tok->function() == privfunc)
1356 return true;
1357 tok = tok->next();
1358 }
1359 }
1360 }
1361
1362 return false; // Unused in this scope
1363}
1364
1365void CheckClassImpl::privateFunctions()
1366{

Callers 1

privateFunctionsMethod · 0.85

Calls 6

nextMethod · 0.80
linkAtMethod · 0.80
tokAtMethod · 0.45
strMethod · 0.45
nameMethod · 0.45
functionMethod · 0.45

Tested by

no test coverage detected