Skip to content

Commit 818fd24

Browse files
authored
Simplify template keyword bracket (danmar#3399)
1 parent 5313a40 commit 818fd24

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

lib/token.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@ class CPPCHECKLIB Token {
641641
setFlag(fIsInline, b);
642642
}
643643

644+
bool isTemplate() const {
645+
return getFlag(fIsTemplate);
646+
}
647+
void isTemplate(bool b) {
648+
setFlag(fIsTemplate, b);
649+
}
650+
644651
bool isBitfield() const {
645652
return mImpl->mBits > 0;
646653
}
@@ -1239,7 +1246,8 @@ class CPPCHECKLIB Token {
12391246
fIsSplitVarDeclComma = (1 << 29), // set to true when variable declarations are split up ('int a,b;' => 'int a; int b;')
12401247
fIsSplitVarDeclEq = (1 << 30), // set to true when variable declaration with initialization is split up ('int a=5;' => 'int a; a=5;')
12411248
fIsImplicitInt = (1U << 31), // Is "int" token implicitly added?
1242-
fIsInline = (1ULL << 32) // Is this a inline type
1249+
fIsInline = (1ULL << 32), // Is this a inline type
1250+
fIsTemplate = (1ULL << 33)
12431251
};
12441252

12451253
Token::Type mTokType;

lib/tokenize.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4509,7 +4509,8 @@ void Tokenizer::createLinks2()
45094509
type.pop();
45104510
}
45114511
} else if (token->str() == "<" &&
4512-
((token->previous() && token->previous()->isName() && !token->previous()->varId()) ||
4512+
((token->previous() && (token->previous()->isTemplate() ||
4513+
(token->previous()->isName() && !token->previous()->varId()))) ||
45134514
Token::Match(token->next(), ">|>>"))) {
45144515
type.push(token);
45154516
if (!templateToken && (token->previous()->str() == "template"))
@@ -5634,8 +5635,19 @@ void Tokenizer::removeExtraTemplateKeywords()
56345635
{
56355636
if (isCPP()) {
56365637
for (Token *tok = list.front(); tok; tok = tok->next()) {
5637-
if (Token::Match(tok, "%name%|> .|:: template %name%"))
5638+
if (Token::Match(tok, "%name%|>|) .|:: template %name%")) {
56385639
tok->next()->deleteNext();
5640+
Token* templateName = tok->tokAt(2);
5641+
while (Token::Match(templateName, "%name%|::")) {
5642+
templateName->isTemplate(true);
5643+
templateName = templateName->next();
5644+
}
5645+
if (Token::Match(templateName->previous(), "operator %op%|(")) {
5646+
templateName->isTemplate(true);
5647+
if (templateName->str() == "(" && templateName->link())
5648+
templateName->link()->isTemplate(true);
5649+
}
5650+
}
56395651
}
56405652
}
56415653
}

test/testtokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6632,6 +6632,12 @@ class TestTokenizer : public TestFixture {
66326632
" for (size_t e = 0; e < d; e++)\n"
66336633
" ;\n"
66346634
"}\n"));
6635+
6636+
ASSERT_NO_THROW(tokenizeAndStringify(
6637+
"template <std::size_t First, std::size_t... Indices, typename Functor>\n"
6638+
"constexpr void constexpr_for_fold_impl([[maybe_unused]] Functor&& f, std::index_sequence<Indices...>) noexcept {\n"
6639+
" (std::forward<Functor>(f).template operator() < First + Indices > (), ...);\n"
6640+
"}\n"));
66356641
}
66366642

66376643
void checkNamespaces() {

0 commit comments

Comments
 (0)