#6729 Simplification reference to constants.

This commit is contained in:
Frank Zingsheim 2015-06-01 10:00:03 +02:00 committed by Alexander Mai
parent 2275d63f99
commit 6857dbe864
2 changed files with 21 additions and 35 deletions

View File

@ -3624,44 +3624,9 @@ bool Tokenizer::simplifyTokenList2()
// e.g. const static int value = sizeof(X)/sizeof(Y);
simplifyCalculations();
// Replace constants..
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "const static| %type% %name% = %num% ;") ||
Token::Match(tok, "const static| %type% %name% ( %num% ) ;")) {
int offset = 0;
if (tok->strAt(1) == "static")
offset = 1;
const unsigned int varId(tok->tokAt(2 + offset)->varId());
if (varId == 0) {
tok = tok->tokAt(5 + offset);
continue;
}
const std::string& num = tok->strAt(4 + offset);
int indent = 1;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{") {
++indent;
} else if (tok2->str() == "}") {
--indent;
if (indent == 0)
break;
}
// Compare constants, but don't touch members of other structures
else if (tok2->varId() == varId) {
tok2->str(num);
}
}
}
}
if (_settings->terminated())
return false;
// Simplify simple calculations..
simplifyCalculations();
// Replace "*(ptr + num)" => "ptr[num]"
simplifyOffsetPointerDereference();
@ -6390,6 +6355,7 @@ bool Tokenizer::simplifyKnownVariables()
tok1->deleteThis();
tok = tok1;
goback = true;
ret = true;
}
}

View File

@ -2664,6 +2664,26 @@ private:
"}";
ASSERT_EQUALS(code, tokenizeAndStringify(code, true));
}
{
//don't simplify '&x'!
const char code[] = "const int * foo ( ) {\n"
"const int x1 = 1 ;\n"
"f ( & x1 ) ;\n"
"const int x2 = 1 ;\n"
"f ( y , & x2 ) ;\n"
"const int x3 = 1 ;\n"
"t = & x3 ;\n"
"const int x4 = 1 ;\n"
"t = y + & x4 ;\n"
"const int x5 = 1 ;\n"
"z [ & x5 ] = y ;\n"
"const int x6 = 1 ;\n"
"v = { & x6 } ;\n"
"const int x7 = 1 ;\n"
"return & x7 ;\n"
"}";
ASSERT_EQUALS(code, tokenizeAndStringify(code, true));
}
}
void simplifyKnownVariables51() { // #4409 hang