Fixed #9809 (Tokenizer; Fix handling of variable declaration with @)

This commit is contained in:
Daniel Marjamäki 2020-09-02 13:03:13 +02:00
parent c1e03d63b2
commit 12d51ae5c4
2 changed files with 29 additions and 19 deletions

View File

@ -10590,26 +10590,35 @@ void Tokenizer::simplifyAt()
std::set<std::string> var; std::set<std::string> var;
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "%name% @ %num% ;")) { if (Token::Match(tok, "%name%|] @ %num%|%name%|(")) {
var.insert(tok->str()); const Token *end = tok->tokAt(2);
tok->isAtAddress(true); if (end->isNumber())
Token::eraseTokens(tok,tok->tokAt(3)); end = end->next();
} else if (end->str() == "(") {
if (Token::Match(tok, "%name% @ %num% : %num% ;")) { int par = 0;
var.insert(tok->str()); while ((end = end->next()) != nullptr) {
tok->isAtAddress(true); if (end->str() == "(")
Token::eraseTokens(tok,tok->tokAt(5)); par++;
} else if (end->str() == ")") {
if (Token::Match(tok, "%name% @ %name% : %num% ;") && var.find(tok->strAt(2)) != var.end()) { if (--par < 0)
var.insert(tok->str()); break;
tok->isAtAddress(true); }
Token::eraseTokens(tok,tok->tokAt(5)); }
} end = end ? end->next() : nullptr;
} else if (var.find(end->str()) != var.end())
end = end->next();
else
continue;
// array declaration if (Token::Match(end, ": %num% ;"))
if (Token::Match(tok, "] @ %num% ;")) { end = end->tokAt(2);
tok->isAtAddress(true);
Token::eraseTokens(tok,tok->tokAt(3)); if (end && end->str() == ";") {
if (tok->isName())
var.insert(tok->str());
tok->isAtAddress(true);
Token::eraseTokens(tok, end);
}
} }
// keywords in compiler from cosmic software for STM8 // keywords in compiler from cosmic software for STM8

View File

@ -1121,6 +1121,7 @@ private:
ASSERT_EQUALS("int x ;", tokenizeAndStringify("int x@123;")); ASSERT_EQUALS("int x ;", tokenizeAndStringify("int x@123;"));
ASSERT_EQUALS("bool x ;", tokenizeAndStringify("bool x@123:1;")); ASSERT_EQUALS("bool x ;", tokenizeAndStringify("bool x@123:1;"));
ASSERT_EQUALS("char PORTB ; bool PB3 ;", tokenizeAndStringify("char PORTB @ 0x10; bool PB3 @ PORTB:3;\n")); ASSERT_EQUALS("char PORTB ; bool PB3 ;", tokenizeAndStringify("char PORTB @ 0x10; bool PB3 @ PORTB:3;\n"));
ASSERT_EQUALS("int x ;", tokenizeAndStringify("int x @ (0x1000 + 18);"));
ASSERT_EQUALS("int x [ 10 ] ;", tokenizeAndStringify("int x[10]@0x100;")); ASSERT_EQUALS("int x [ 10 ] ;", tokenizeAndStringify("int x[10]@0x100;"));