Tokenizer: fixed simplification of static constants

This commit is contained in:
Daniel Marjamäki 2013-01-19 21:13:51 +01:00
parent 233210a619
commit 67c9720299
3 changed files with 14 additions and 6 deletions

View File

@ -3259,14 +3259,17 @@ bool Tokenizer::simplifyTokenList()
// Replace constants.. // Replace constants..
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "const %type% %var% = %num% ;")) { if (Token::Match(tok, "const static| %type% %var% = %num% ;")) {
unsigned int varId = tok->tokAt(2)->varId(); unsigned int offset = 0;
if (tok->strAt(1) == "static")
offset = 1;
const unsigned int varId(tok->tokAt(2 + offset)->varId());
if (varId == 0) { if (varId == 0) {
tok = tok->tokAt(5); tok = tok->tokAt(5);
continue; continue;
} }
const std::string& num = tok->strAt(4); const std::string& num = tok->strAt(4 + offset);
int indent = 1; int indent = 1;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next()) { for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{") { if (tok2->str() == "{") {

View File

@ -591,6 +591,11 @@ private:
const char code2[] = " void f ( ) { if ( aa ) { a = 0 ; } else { { a = 2 ; } } }"; const char code2[] = " void f ( ) { if ( aa ) { a = 0 ; } else { { a = 2 ; } } }";
ASSERT_EQUALS(tok(code2), tok(code1)); ASSERT_EQUALS(tok(code2), tok(code1));
} }
{
const char code1[] = "static const int x=1; void f() { if(x) { a=0; } }";
ASSERT_EQUALS("void f ( ) { a = 0 ; }", tok(code1));
}
} }
void combine_strings() { void combine_strings() {

View File

@ -5092,9 +5092,9 @@ private:
void simplify_constants4() { void simplify_constants4() {
const char code[] = "static const int bSize = 4;\n" const char code[] = "static const int bSize = 4;\n"
"static const int aSize = 50;\n" "static const int aSize = 50;\n"
"const int x = bSize;\n" "x = bSize;\n"
"const int y = aSize;\n"; "y = aSize;\n";
ASSERT_EQUALS("const int x = 4 ;\nconst int y = 50 ;", tokenizeAndStringify(code,true)); ASSERT_EQUALS("x = 4 ;\ny = 50 ;", tokenizeAndStringify(code,true));
} }
void simplify_null() { void simplify_null() {