Skip to content
Draft
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
54 changes: 45 additions & 9 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5583,8 +5583,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])

simplifyFunctionTryCatch();

simplifyHeadersAndUnusedTemplates();

// Remove __asm..
simplifyAsm();

Expand Down Expand Up @@ -5612,6 +5610,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
// remove __attribute__((?))
simplifyAttribute();

simplifyHeadersAndUnusedTemplates();

validate();

const SHOWTIME_MODES showTime = mTimerResults ? mSettings.showtime : SHOWTIME_MODES::SHOWTIME_NONE;
Expand Down Expand Up @@ -6363,7 +6363,7 @@ void Tokenizer::simplifyHeadersAndUnusedTemplates()
}
}

if (!tok->previous() || Token::Match(tok->previous(), "[;{}]")) {
if (!tok->previous() || Token::Match(tok->previous(), "[;{}:]")) {
// Remove unused function declarations
if (isIncluded && removeUnusedIncludedFunctions) {
while (true) {
Expand Down Expand Up @@ -6396,7 +6396,7 @@ void Tokenizer::simplifyHeadersAndUnusedTemplates()

if (removeUnusedTemplates || (isIncluded && removeUnusedIncludedTemplates)) {
if (Token::Match(tok, "template < %name%")) {
const Token *closingBracket = tok->next()->findClosingBracket();
Token *closingBracket = tok->next()->findClosingBracket();
if (Token::Match(closingBracket, "> class|struct %name% [;:{]") && keep.find(closingBracket->strAt(2)) == keep.end()) {
const Token *endToken = closingBracket->tokAt(3);
if (endToken->str() == ":") {
Expand All @@ -6410,11 +6410,47 @@ void Tokenizer::simplifyHeadersAndUnusedTemplates()
Token::eraseTokens(tok, endToken);
tok->deleteThis();
}
} else if (Token::Match(closingBracket, "> %type% %name% (") && Token::simpleMatch(closingBracket->linkAt(3), ") {") && keep.find(closingBracket->strAt(2)) == keep.end()) {
const Token *endToken = closingBracket->linkAt(3)->linkAt(1)->next();
Token::eraseTokens(tok, endToken);
tok->deleteThis();
goBack = true;
} else {
Token *funcTok = closingBracket->next();
while (funcTok) {
if (Token::Match(funcTok, "constexpr|static|inline|const|%type%|&|&&|*") && !Token::Match(funcTok, "%name% (")) {
funcTok = funcTok->next();
continue;
}
break;
}
if (!Token::Match(funcTok, "%name% (")) {
tok = funcTok;
continue;
}
funcTok = funcTok->linkAt(1);
if (funcTok) {
funcTok = funcTok->next();
}
while (funcTok) {
if (Token::Match(funcTok, "throw|noexcept (")) {
funcTok = funcTok->linkAt(1);
if (funcTok) {
funcTok = funcTok->next();
}
continue;
}
if (Token::Match(funcTok, "const|volatile|&|&&")) {
funcTok = funcTok->next();
continue;
}
break;
}
if (!Token::simpleMatch(funcTok, "{")) {
tok = funcTok;
continue;
}
funcTok = funcTok->link();
if (funcTok) {
Token::eraseTokens(tok, funcTok->next());
tok->deleteThis();
goBack = true;
}
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,70 @@ def test_check_unused_templates_func(tmp_path): # #13714
(void)(*((int*)0));
}

class S {
public:
template<typename T>
void f_t_3()
{
(void)(*((int*)0));
}
}

template<typename T>
static inline void f_t_4()
{
(void)(*((int*)0));
}

template<typename T>
const void *f_t_5()
{
(void)(*((int*)0));
}

template<typename T>
void f_t_6() __attribute__((noreturn))
{
(void)(*((int*)0));
}

template<typename T>
__attribute__((noreturn)) void f_t_7()
{
(void)(*((int*)0));
}

template<typename T>
__declspec(noreturn) void f_t_8()
{
(void)(*((int*)0));
}

template<typename T>
[[noreturn]] void f_t_9()
{
(void)(*((int*)0));
}

template<typename T>
void f_t_10() noexcept(true)
{
(void)(*((int*)0));
}

template<typename T>
void f_t_11() throw(true)
{
(void)(*((int*)0));
}

struct S {
template<typename T>
void f_t_12() const {
(void)(*((int*)0));
}
};

void f() {}
""")

Expand Down