AST: Adjust AST for variable declaration 'char var[] = str;'

This commit is contained in:
Daniel Marjamäki 2020-10-31 17:05:42 +01:00
parent 6cf571af97
commit 376860f796
2 changed files with 17 additions and 3 deletions

View File

@ -1553,8 +1553,22 @@ static Token * createAstAtToken(Token *tok, bool cpp)
typecount++;
typetok = typetok->next();
}
if (Token::Match(typetok, "%var% =") && typetok->varId())
tok = typetok;
if (Token::Match(typetok, "%var% =|[") && typetok->varId()) {
Token *tok2 = typetok->next();
while (Token::simpleMatch(tok2, "["))
tok2 = tok2->link()->next();
if (Token::simpleMatch(tok2, "=")) {
AST_state state(cpp);
state.op.push(typetok);
Token * const start = tok2;
compileBinOp(tok2, state, compileAssignTernary);
if (tok2 == start || !tok2)
return start;
createAstAtTokenInner(start->next(), tok2, cpp);
return tok2;
} else if (Token::simpleMatch(tok2, ";"))
return tok;
}
// Do not create AST for function declaration
if (typetok &&

View File

@ -7857,7 +7857,7 @@ private:
void astvardecl() {
// Variable declaration
ASSERT_EQUALS("a1[\"\"=", testAst("char a[1]=\"\";"));
ASSERT_EQUALS("var\"\"=", testAst("char var[1]=\"\";"));
ASSERT_EQUALS("charp*(3[char5[3[new=", testAst("char (*p)[3] = new char[5][3];"));
ASSERT_EQUALS("varp=", testAst("const int *var = p;"));