Changed AST for variable declarations with initializations

This commit is contained in:
Daniel Marjamäki 2019-03-09 19:09:15 +01:00
parent 1b4895a579
commit e17ddfd964
3 changed files with 23 additions and 3 deletions

View File

@ -613,8 +613,9 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
continue;
}
// Assign reference to non-local variable
} else if (Token::Match(tok->astParent(), "&|&&") && Token::simpleMatch(tok->astParent()->astParent(), "=") &&
tok->variable() && tok->variable()->declarationId() == tok->varId() && tok->variable()->isStatic() &&
} else if (Token::Match(tok->previous(), "&|&& %var% =") && tok->astParent() == tok->next() &&
tok->variable() && tok->variable()->nameToken() == tok &&
tok->variable()->declarationId() == tok->varId() && tok->variable()->isStatic() &&
!tok->variable()->isArgument()) {
ErrorPath errorPath;
const Variable *var = getLifetimeVariable(tok, errorPath);

View File

@ -1165,6 +1165,18 @@ static Token * createAstAtToken(Token *tok, bool cpp)
if (Token::Match(tok, "%type% <") && !Token::Match(tok->linkAt(1), "> [({]"))
return tok->linkAt(1);
if (Token::Match(tok, "%type% %name%|*|&|::") && tok->str() != "return") {
bool decl = false;
Token *typetok = tok;
while (Token::Match(typetok, "%type%|::|*|&")) {
if (typetok->isStandardType() || Token::Match(typetok, "struct|const|static"))
decl = true;
typetok = typetok->next();
}
if (decl && Token::Match(typetok->previous(), "[*&] %var% ="))
tok = typetok;
}
if (Token::Match(tok, "return|case") || (cpp && tok->str() == "throw") || !tok->previous() || Token::Match(tok, "%name% %op%|(|[|.|::|<|?|;") || Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{")) {
if (cpp && (Token::Match(tok->tokAt(-2), "[;{}] new|delete %name%") || Token::Match(tok->tokAt(-3), "[;{}] :: new|delete %name%")))
tok = tok->previous();

View File

@ -8214,6 +8214,12 @@ private:
tokenList.createLinks();
tokenList.createLinks2();
// set varid..
for (Token *tok = tokenList.list.front(); tok; tok = tok->next()) {
if (tok->str() == "var")
tok->varId(1);
}
// Create AST..
tokenList.prepareTernaryOpForAST();
tokenList.list.createAst();
@ -8289,7 +8295,6 @@ private:
ASSERT_EQUALS("a\"\"=", testAst("a=\"\""));
ASSERT_EQUALS("a\'\'=", testAst("a=\'\'"));
ASSERT_EQUALS("a1[\"\"=", testAst("char a[1]=\"\";"));
ASSERT_EQUALS("'X''a'>", testAst("('X' > 'a')"));
ASSERT_EQUALS("'X''a'>", testAst("(L'X' > L'a')"));
@ -8342,7 +8347,9 @@ private:
ASSERT_EQUALS("ifCA_FarReadfilenew(,sizeofobjtype(,(!(", testAst("if (!CA_FarRead(file, (void far *)new, sizeof(objtype)))")); // #5910 - don't hang if C code is parsed as C++
// Variable declaration
ASSERT_EQUALS("a1[\"\"=", testAst("char a[1]=\"\";"));
ASSERT_EQUALS("charp*(3[char5[3[new=", testAst("char (*p)[3] = new char[5][3];"));
ASSERT_EQUALS("varp=", testAst("const int *var = p;"));
}
void astexpr2() { // limit for large expressions