| 455 | } |
| 456 | |
| 457 | void CheckClassImpl::copyconstructors() |
| 458 | { |
| 459 | if (!mSettings.severity.isEnabled(Severity::warning)) |
| 460 | return; |
| 461 | |
| 462 | logChecker("CheckClass::checkCopyConstructors"); // warning |
| 463 | |
| 464 | for (const Scope * scope : mSymbolDatabase->classAndStructScopes) { |
| 465 | std::map<int, const Token*> allocatedVars; |
| 466 | std::map<int, const Token*> deallocatedVars; |
| 467 | |
| 468 | for (const Function &func : scope->functionList) { |
| 469 | if (func.type == FunctionType::eConstructor && func.functionScope) { |
| 470 | // Allocations in constructors |
| 471 | const Token* tok = func.token->linkAt(1); |
| 472 | for (const Token* const end = func.functionScope->bodyStart; tok != end; tok = tok->next()) { |
| 473 | if (Token::Match(tok, "%var% ( new") || |
| 474 | (Token::Match(tok, "%var% ( %name% (") && mSettings.library.getAllocFuncInfo(tok->tokAt(2)))) { |
| 475 | const Variable* var = tok->variable(); |
| 476 | if (var && var->isPointer() && var->scope() == scope) |
| 477 | allocatedVars[tok->varId()] = tok; |
| 478 | } |
| 479 | } |
| 480 | for (const Token* const end = func.functionScope->bodyEnd; tok != end; tok = tok->next()) { |
| 481 | if (Token::Match(tok, "%var% = new") || |
| 482 | (Token::Match(tok, "%var% = %name% (") && mSettings.library.getAllocFuncInfo(tok->tokAt(2)))) { |
| 483 | const Variable* var = tok->variable(); |
| 484 | if (var && var->isPointer() && var->scope() == scope && !var->isStatic()) |
| 485 | allocatedVars[tok->varId()] = tok; |
| 486 | } |
| 487 | } |
| 488 | } else if (func.type == FunctionType::eDestructor && func.functionScope) { |
| 489 | // Deallocations in destructors |
| 490 | const Token* tok = func.functionScope->bodyStart; |
| 491 | for (const Token* const end = func.functionScope->bodyEnd; tok != end; tok = tok->next()) { |
| 492 | if (Token::Match(tok, "delete %var%") || |
| 493 | (Token::Match(tok, "%name% ( %var%") && mSettings.library.getDeallocFuncInfo(tok))) { |
| 494 | const Token *vartok = tok->str() == "delete" ? tok->next() : tok->tokAt(2); |
| 495 | const Variable* var = vartok->variable(); |
| 496 | if (var && var->isPointer() && var->scope() == scope && !var->isStatic()) |
| 497 | deallocatedVars[vartok->varId()] = vartok; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | const bool hasAllocatedVars = !allocatedVars.empty(); |
| 504 | const bool hasDeallocatedVars = !deallocatedVars.empty(); |
| 505 | |
| 506 | if (hasAllocatedVars || hasDeallocatedVars) { |
| 507 | const Function *funcCopyCtor = nullptr; |
| 508 | const Function *funcOperatorEq = nullptr; |
| 509 | const Function *funcDestructor = nullptr; |
| 510 | for (const Function &func : scope->functionList) { |
| 511 | if (func.type == FunctionType::eCopyConstructor) |
| 512 | funcCopyCtor = &func; |
| 513 | else if (func.type == FunctionType::eOperatorEqual) |
| 514 | funcOperatorEq = &func; |