Skip to content

Commit e17ddfd

Browse files
committed
Changed AST for variable declarations with initializations
1 parent 1b4895a commit e17ddfd

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎lib/checkautovariables.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,9 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
613613
continue;
614614
}
615615
// Assign reference to non-local variable
616-
} else if (Token::Match(tok->astParent(), "&|&&") && Token::simpleMatch(tok->astParent()->astParent(), "=") &&
617-
tok->variable() && tok->variable()->declarationId() == tok->varId() && tok->variable()->isStatic() &&
616+
} else if (Token::Match(tok->previous(), "&|&& %var% =") && tok->astParent() == tok->next() &&
617+
tok->variable() && tok->variable()->nameToken() == tok &&
618+
tok->variable()->declarationId() == tok->varId() && tok->variable()->isStatic() &&
618619
!tok->variable()->isArgument()) {
619620
ErrorPath errorPath;
620621
const Variable *var = getLifetimeVariable(tok, errorPath);

‎lib/tokenlist.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,18 @@ static Token * createAstAtToken(Token *tok, bool cpp)
11651165
if (Token::Match(tok, "%type% <") && !Token::Match(tok->linkAt(1), "> [({]"))
11661166
return tok->linkAt(1);
11671167

1168+
if (Token::Match(tok, "%type% %name%|*|&|::") && tok->str() != "return") {
1169+
bool decl = false;
1170+
Token *typetok = tok;
1171+
while (Token::Match(typetok, "%type%|::|*|&")) {
1172+
if (typetok->isStandardType() || Token::Match(typetok, "struct|const|static"))
1173+
decl = true;
1174+
typetok = typetok->next();
1175+
}
1176+
if (decl && Token::Match(typetok->previous(), "[*&] %var% ="))
1177+
tok = typetok;
1178+
}
1179+
11681180
if (Token::Match(tok, "return|case") || (cpp && tok->str() == "throw") || !tok->previous() || Token::Match(tok, "%name% %op%|(|[|.|::|<|?|;") || Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{")) {
11691181
if (cpp && (Token::Match(tok->tokAt(-2), "[;{}] new|delete %name%") || Token::Match(tok->tokAt(-3), "[;{}] :: new|delete %name%")))
11701182
tok = tok->previous();

‎test/testtokenize.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8214,6 +8214,12 @@ class TestTokenizer : public TestFixture {
82148214
tokenList.createLinks();
82158215
tokenList.createLinks2();
82168216

8217+
// set varid..
8218+
for (Token *tok = tokenList.list.front(); tok; tok = tok->next()) {
8219+
if (tok->str() == "var")
8220+
tok->varId(1);
8221+
}
8222+
82178223
// Create AST..
82188224
tokenList.prepareTernaryOpForAST();
82198225
tokenList.list.createAst();
@@ -8289,7 +8295,6 @@ class TestTokenizer : public TestFixture {
82898295

82908296
ASSERT_EQUALS("a\"\"=", testAst("a=\"\""));
82918297
ASSERT_EQUALS("a\'\'=", testAst("a=\'\'"));
8292-
ASSERT_EQUALS("a1[\"\"=", testAst("char a[1]=\"\";"));
82938298

82948299
ASSERT_EQUALS("'X''a'>", testAst("('X' > 'a')"));
82958300
ASSERT_EQUALS("'X''a'>", testAst("(L'X' > L'a')"));
@@ -8342,7 +8347,9 @@ class TestTokenizer : public TestFixture {
83428347
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++
83438348

83448349
// Variable declaration
8350+
ASSERT_EQUALS("a1[\"\"=", testAst("char a[1]=\"\";"));
83458351
ASSERT_EQUALS("charp*(3[char5[3[new=", testAst("char (*p)[3] = new char[5][3];"));
8352+
ASSERT_EQUALS("varp=", testAst("const int *var = p;"));
83468353
}
83478354

83488355
void astexpr2() { // limit for large expressions

0 commit comments

Comments
 (0)