Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,13 +626,26 @@ void TemplateSimplifier::deleteToken(Token *tok)
tok->deleteThis();
}

bool TemplateSimplifier::removeTemplate(Token *tok)
static void invalidateForwardDecls(const Token* beg, const Token* end, std::map<Token*, Token*>* forwardDecls) {
if (!forwardDecls)
return;
for (auto& fwd : *forwardDecls) {
for (const Token* tok = beg; tok != end; tok = tok->next())
if (fwd.second == tok) {
fwd.second = nullptr;
break;
}
}
}

bool TemplateSimplifier::removeTemplate(Token *tok, std::map<Token*, Token*>* forwardDecls)
{
if (!Token::simpleMatch(tok, "template <"))
return false;

Token *end = findTemplateDeclarationEnd(tok);
if (end && end->next()) {
invalidateForwardDecls(tok, end->next(), forwardDecls);
eraseTokens(tok, end->next());
deleteToken(tok);
return true;
Expand Down Expand Up @@ -2417,8 +2430,10 @@ static void invalidateInst(const Token* beg, const Token* end, std::vector<newIn
return;
for (auto& inst : *newInst) {
for (const Token* tok = beg; tok != end; tok = tok->next())
if (inst.token == tok)
if (inst.token == tok) {
inst.token = nullptr;
break;
}
}
}

Expand Down Expand Up @@ -3850,8 +3865,8 @@ void TemplateSimplifier::simplifyTemplates(
// remove forward declaration if found
auto it1 = mTemplateForwardDeclarationsMap.find(it->token());
if (it1 != mTemplateForwardDeclarationsMap.end())
removeTemplate(it1->second);
removeTemplate(it->token());
removeTemplate(it1->second, &mTemplateForwardDeclarationsMap);
removeTemplate(it->token(), &mTemplateForwardDeclarationsMap);
}
mTemplateDeclarations.erase(decl);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/templatesimplifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ class CPPCHECKLIB TemplateSimplifier {
/**
* Remove a specific "template < ..." template class/function
*/
static bool removeTemplate(Token *tok);
static bool removeTemplate(Token *tok, std::map<Token*, Token*>* forwardDecls = nullptr);

/** Syntax error */
NORETURN static void syntaxError(const Token *tok);
Expand Down
20 changes: 20 additions & 0 deletions test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4054,6 +4054,26 @@ class TestSimplifyTemplate : public TestFixture {
"b<a100<int>> d100;";
// don't bother checking the output because this is not instantiated properly
tok(code); // don't crash

const char code2[] = "template<typename T> void f();\n" // #11489
"template<typename T> void f(int);\n"
"void g() {\n"
" f<int>();\n"
" f<char>(1);\n"
"}\n"
"template<typename T>\n"
"void f(int) {}\n"
"template<typename T>\n"
"void f() {\n"
" f<T>(0);\n"
"}\n";
const char exp2[] = "template < typename T > void f ( ) ; "
"void f<int> ( int ) ; "
"void f<char> ( int ) ; "
"void g ( ) { f<int> ( ) ; f<char> ( 1 ) ; } "
"void f<int> ( ) { f<int> ( 0 ) ; } "
"void f<char> ( int ) { }";
ASSERT_EQUALS(exp2, tok(code2));
}

void template159() { // #9886
Expand Down