Fixed #7347 (AST: wrong ast when template variable is declared and initiailized in if 'if (A::B<C> abc = 123)')

This commit is contained in:
Daniel Marjamäki 2016-01-26 10:40:44 +01:00
parent 40e14f401f
commit c0056d2455
2 changed files with 25 additions and 1 deletions

View File

@ -438,6 +438,26 @@ struct AST_state {
explicit AST_state(bool cpp_) : depth(0), inArrayAssignment(0), cpp(cpp_), assign(0U) {}
};
static Token * skipDecl(Token *tok)
{
if (!Token::Match(tok->previous(), "( %name%"))
return tok;
Token *vartok = tok;
while (Token::Match(vartok, "%name%|*|&|::|<")) {
if (vartok->str() == "<") {
if (vartok->link())
vartok = vartok->link();
else
return tok;
} else if (Token::Match(vartok, "%name% [:=]")) {
return vartok;
}
vartok = vartok->next();
}
return tok;
}
static bool iscast(const Token *tok)
{
if (!Token::Match(tok, "( ::| %name%"))
@ -577,6 +597,7 @@ static void compileTerm(Token *&tok, AST_state& state)
if (tok->str() == "<")
tok = tok->link()->next();
} else if (!state.cpp || !Token::Match(tok, "new|delete %name%|*|&|::|(|[")) {
tok = skipDecl(tok);
while (tok->next() && tok->next()->isName())
tok = tok->next();
state.op.push(tok);
@ -960,7 +981,7 @@ static void compileExpression(Token *&tok, AST_state& state)
static Token * createAstAtToken(Token *tok, bool cpp)
{
if (Token::simpleMatch(tok, "for (")) {
Token *tok2 = tok->tokAt(2);
Token *tok2 = skipDecl(tok->tokAt(2));
Token *init1 = nullptr;
Token * const endPar = tok->next()->link();
while (tok2 && tok2 != endPar && tok2->str() != ";") {

View File

@ -8104,6 +8104,9 @@ private:
ASSERT_EQUALS("fori1=current0=,iNUM<=i++;;(", testAst("for(i = (1), current = 0; i <= (NUM); ++i)"));
ASSERT_EQUALS("foreachxy,((", testAst("for(each(x,y)){}")); // it's not well-defined what this ast should be
ASSERT_EQUALS("forab:(", testAst("for (int a : b);"));
ASSERT_EQUALS("forab:(", testAst("for (int *a : b);"));
ASSERT_EQUALS("forcd:(", testAst("for (a<b> c : d);"));
ASSERT_EQUALS("forde:(", testAst("for (a::b<c> d : e);"));
ASSERT_EQUALS("forx*0=yz;;(", testAst("for(*x=0;y;z)"));
// problems with multiple expressions