Unused templates: Remove unused template function with variadic arguments

This commit is contained in:
Daniel Marjamäki 2019-04-07 08:37:04 +02:00
parent 7237acdb47
commit 83106d5827
2 changed files with 32 additions and 2 deletions

View File

@ -4852,7 +4852,10 @@ void Tokenizer::simplifyHeaders()
if (removeUnusedTemplates || (isIncluded && removeUnusedIncludedTemplates)) {
if (Token::Match(tok->next(), "template < %name%")) {
const Token *tok2 = tok->tokAt(3);
while (Token::Match(tok2, "%name% %name% [,=>]")) {
while (Token::Match(tok2, "%name% %name% [,=>]") || Token::Match(tok2, "typename . . . %name% [,>]")) {
if (Token::simpleMatch(tok2, "typename . . ."))
tok2 = tok2->tokAt(5);
else
tok2 = tok2->tokAt(2);
if (Token::Match(tok2, "= %name% [,>]"))
tok2 = tok2->tokAt(2);
@ -4870,6 +4873,9 @@ void Tokenizer::simplifyHeaders()
endToken = endToken->link()->next();
if (endToken && endToken->str() == ";")
Token::eraseTokens(tok, endToken);
} else if (Token::Match(tok2, "> %type% %name% (") && Token::simpleMatch(tok2->linkAt(3), ") {") && keep.find(tok2->strAt(2)) == keep.end()) {
const Token *endToken = tok2->linkAt(3)->linkAt(1)->next();
Token::eraseTokens(tok, endToken);
}
}
}

View File

@ -92,6 +92,8 @@ private:
TEST_CASE(longtok);
TEST_CASE(removeUnusedTemplates);
TEST_CASE(simplifyCasts1);
TEST_CASE(simplifyCasts2);
TEST_CASE(simplifyCasts3);
@ -550,6 +552,18 @@ private:
return "";
}
std::string tokenizeAndStringify(const char code[], const Settings &settings) {
errout.str("");
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
if (!tokenizer.tokens())
return "";
return tokenizer.tokens()->stringifyList(false, true, false, true, false, 0, 0);
}
std::string tokenizeDebugListing(const char code[], bool simplify = false, const char filename[] = "test.cpp") {
errout.str("");
@ -974,6 +988,16 @@ private:
}
void removeUnusedTemplates() {
Settings s;
s.removeUnusedTemplates = true;
ASSERT_EQUALS(";",
tokenizeAndStringify("; template <typename... a> uint8_t b(std::tuple<uint8_t> d) {\n"
" std::tuple<a...> c{std::move(d)};\n"
" return std::get<0>(c);\n"
"}", s));
}
// Dont remove "(int *)"..
void simplifyCasts1() {