Skip to content

Commit c0056d2

Browse files
committed
Fixed #7347 (AST: wrong ast when template variable is declared and initiailized in if 'if (A::B<C> abc = 123)')
1 parent 40e14f4 commit c0056d2

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/tokenlist.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,26 @@ struct AST_state {
438438
explicit AST_state(bool cpp_) : depth(0), inArrayAssignment(0), cpp(cpp_), assign(0U) {}
439439
};
440440

441+
static Token * skipDecl(Token *tok)
442+
{
443+
if (!Token::Match(tok->previous(), "( %name%"))
444+
return tok;
445+
446+
Token *vartok = tok;
447+
while (Token::Match(vartok, "%name%|*|&|::|<")) {
448+
if (vartok->str() == "<") {
449+
if (vartok->link())
450+
vartok = vartok->link();
451+
else
452+
return tok;
453+
} else if (Token::Match(vartok, "%name% [:=]")) {
454+
return vartok;
455+
}
456+
vartok = vartok->next();
457+
}
458+
return tok;
459+
}
460+
441461
static bool iscast(const Token *tok)
442462
{
443463
if (!Token::Match(tok, "( ::| %name%"))
@@ -577,6 +597,7 @@ static void compileTerm(Token *&tok, AST_state& state)
577597
if (tok->str() == "<")
578598
tok = tok->link()->next();
579599
} else if (!state.cpp || !Token::Match(tok, "new|delete %name%|*|&|::|(|[")) {
600+
tok = skipDecl(tok);
580601
while (tok->next() && tok->next()->isName())
581602
tok = tok->next();
582603
state.op.push(tok);
@@ -960,7 +981,7 @@ static void compileExpression(Token *&tok, AST_state& state)
960981
static Token * createAstAtToken(Token *tok, bool cpp)
961982
{
962983
if (Token::simpleMatch(tok, "for (")) {
963-
Token *tok2 = tok->tokAt(2);
984+
Token *tok2 = skipDecl(tok->tokAt(2));
964985
Token *init1 = nullptr;
965986
Token * const endPar = tok->next()->link();
966987
while (tok2 && tok2 != endPar && tok2->str() != ";") {

test/testtokenize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8104,6 +8104,9 @@ class TestTokenizer : public TestFixture {
81048104
ASSERT_EQUALS("fori1=current0=,iNUM<=i++;;(", testAst("for(i = (1), current = 0; i <= (NUM); ++i)"));
81058105
ASSERT_EQUALS("foreachxy,((", testAst("for(each(x,y)){}")); // it's not well-defined what this ast should be
81068106
ASSERT_EQUALS("forab:(", testAst("for (int a : b);"));
8107+
ASSERT_EQUALS("forab:(", testAst("for (int *a : b);"));
8108+
ASSERT_EQUALS("forcd:(", testAst("for (a<b> c : d);"));
8109+
ASSERT_EQUALS("forde:(", testAst("for (a::b<c> d : e);"));
81078110
ASSERT_EQUALS("forx*0=yz;;(", testAst("for(*x=0;y;z)"));
81088111

81098112
// problems with multiple expressions

0 commit comments

Comments
 (0)