Skip to content
Open
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
13 changes: 11 additions & 2 deletions lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,14 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
return 0;
if (tok->str() != "<")
return 0;
if (Token::Match(tok->previous(), "%var% <"))
if (Token::Match(tok->previous(), "%var%|) <") && !(tok->linkAt(-1) && Token::simpleMatch(tok->linkAt(-1)->tokAt(-1), "operator")))
return 0;
tok = tok->next();
if (!tok || tok->str() == ">")
return 0;

unsigned int level = 0;
unsigned int ternaryLevel = 0;

while (tok) {
// skip template template
Expand Down Expand Up @@ -460,8 +461,16 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
}

// Skip '=', '?', ':'
if (Token::Match(tok, "=|?|:"))
if (Token::Match(tok, "=|?|:")) {
if (tok->str()[0] == '?')
Comment thread
chrchr-github marked this conversation as resolved.
++ternaryLevel;
else if (tok->str()[0] == ':') {
if (ternaryLevel == 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new ?/: balancing isn't covered by the added tests. In both f1 and f2 the < follows ) (g() < ...), so the new %var%|) < early return fires first and this code never runs. Please add a case where the < follows a name without a varid (enum constant, macro, static member of unknown type), e.g. x ? N < 0 : 0 > y.

Note: the f2 shape with such a name, N < x ? x != y : 0 > y, still has balanced ?/:, so I think this still returns 1 and the expression is treated as a template. If that's in scope for #15086, it's worth a test too.

Nit: tok->str() == "?" / == ":" reads better than tok->str()[0].

return 0;
--ternaryLevel;
}
tok = tok->next();
}
if (!tok)
return 0;

Expand Down
33 changes: 33 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(vardecl33);
TEST_CASE(vardecl34);
TEST_CASE(vardecl35);
TEST_CASE(vardecl36);
TEST_CASE(vardecl_stl_1);
TEST_CASE(vardecl_stl_2);
TEST_CASE(vardecl_stl_3);
Expand Down Expand Up @@ -3035,6 +3036,38 @@ class TestTokenizer : public TestFixture {
}
}

void vardecl36() {
const char code[] = "struct S {\n" // #15086
" bool f1() const;\n"
" bool f2() const;\n"
" int g() const;\n"
" int x, y;\n"
"};\n"
"bool S::f1() const {\n"
" const bool b = x ? g() < 0 : 0 > y;\n"
" return b;\n"
"}\n"
"bool S::f2() const {\n"
" const bool b = g() < x ? x != y : 0 > y;\n"
" return b;\n"
"}\n";
const char expected[] = "struct S {\n"
"bool f1 ( ) const ;\n"
"bool f2 ( ) const ;\n"
"int g ( ) const ;\n"
"int x ; int y ;\n"
"} ;\n"
"bool S :: f1 ( ) const {\n"
"const bool b = x ? ( g ( ) < 0 ) : 0 > y ;\n"
"return b ;\n"
"}\n"
"bool S :: f2 ( ) const {\n"
"const bool b = g ( ) < x ? x != y : 0 > y ;\n"
"return b ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
}

void volatile_variables() {
{
const char code[] = "volatile int a=0;\n"
Expand Down
Loading