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%|) <"))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Needs to handle

template <typename T, typename U>
bool f(U u, bool b) {
  return u.template operator()<T>() || b;
}

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)
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