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

Method replaceTemplateUsage

lib/templatesimplifier.cpp:3425–3489  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3423}
3424
3425void TemplateSimplifier::replaceTemplateUsage(
3426 const TokenAndName &instantiation,
3427 const std::list<std::string> &typeStringsUsedInTemplateInstantiation,
3428 const std::string &newName)
3429{
3430 std::list<std::pair<Token *, Token *>> removeTokens;
3431 for (Token *nameTok = mTokenList.front(); nameTok; nameTok = nameTok->next()) {
3432 if (!Token::Match(nameTok, "%name% <") ||
3433 Token::Match(nameTok, "template|const_cast|dynamic_cast|reinterpret_cast|static_cast"))
3434 continue;
3435
3436 std::set<TemplateSimplifier::TokenAndName*>* pointers = nameTok->templateSimplifierPointers();
3437
3438 // check if instantiation matches token instantiation from pointer
3439 if (pointers && !pointers->empty()) {
3440 // check full name
3441 if (instantiation.fullName() != (*pointers->begin())->fullName()) {
3442 // FIXME: fallback to just matching name
3443 if (nameTok->str() != instantiation.name())
3444 continue;
3445 }
3446 }
3447 // no pointer available look at tokens directly
3448 else {
3449 // FIXME: fallback to just matching name
3450 if (nameTok->str() != instantiation.name())
3451 continue;
3452 }
3453
3454 if (!matchTemplateParameters(nameTok, typeStringsUsedInTemplateInstantiation))
3455 continue;
3456
3457 Token *tok2 = nameTok->next()->findClosingBracket();
3458
3459 if (!tok2)
3460 break;
3461
3462 const Token * const nameTok1 = nameTok;
3463 nameTok->str(newName);
3464
3465 // matching template usage => replace tokens..
3466 // Foo < int > => Foo<int>
3467 for (const Token *tok = nameTok1->next(); tok != tok2; tok = tok->next()) {
3468 if (tok->isName() && tok->templateSimplifierPointers() && !tok->templateSimplifierPointers()->empty()) {
3469 for (auto ti = mTemplateInstantiations.cbegin(); ti != mTemplateInstantiations.cend();) {
3470 if (ti->token() == tok) {
3471 mTemplateInstantiations.erase(ti);
3472 break;
3473 }
3474 ++ti;
3475 }
3476 }
3477 }
3478 // Fix crash in #9007
3479 if (Token::simpleMatch(nameTok->previous(), ">"))
3480 mTemplateNamePos.erase(nameTok->previous());
3481 removeTokens.emplace_back(nameTok, tok2->next());
3482

Callers

nothing calls this directly

Calls 12

matchTemplateParametersFunction · 0.85
frontMethod · 0.80
nextMethod · 0.80
fullNameMethod · 0.80
tokenMethod · 0.80
simpleMatchFunction · 0.70
emptyMethod · 0.45
beginMethod · 0.45
strMethod · 0.45
nameMethod · 0.45
findClosingBracketMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected