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
11 changes: 10 additions & 1 deletion lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
return 0;

unsigned int level = 0;
unsigned int inTernary = 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve button

Minor naming nit: inTernary reads like a bool, but it is a nesting counter. Something like ternaryLevel would match the existing level variable in this function and make --ternaryLevel read more naturally.


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] == '?')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve button

Since the surrounding code compares whole strings (tok->str() == ">", tok->str() == ","), tok->str() == "?" / tok->str() == ":" would be more consistent and would not rely on the Token::Match above to rule out other tokens starting with those characters. It might also be worth updating the // Skip '=', '?', ':' comment to say that an unmatched : means this is not a template argument list.

++inTernary;
else if (tok->str()[0] == ':') {
if (inTernary == 0)
return 0;
--inTernary;
}
tok = tok->next();
}
if (!tok)
return 0;

Expand Down
23 changes: 23 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,28 @@ class TestTokenizer : public TestFixture {
}
}

void vardecl36() {
const char code[] = "struct S {\n"
" bool f() const;\n"
" int g() const;\n"
" int x, y;\n"
"};\n"
"bool S::f() const {\n"
" const bool b = x ? g() < 0 : 0 > y;\n"
" return b;\n"
"}\n";
const char expected[] = "struct S {\n"
"bool f ( ) const ;\n"
"int g ( ) const ;\n"
"int x ; int y ;\n"
"} ;\n"
"bool S :: f ( ) const {\n"
"const bool b = x ? ( g ( ) < 0 ) : 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