#6729 Simplification reference to constants.
This commit is contained in:
parent
2275d63f99
commit
6857dbe864
|
@ -3624,44 +3624,9 @@ bool Tokenizer::simplifyTokenList2()
|
||||||
// e.g. const static int value = sizeof(X)/sizeof(Y);
|
// e.g. const static int value = sizeof(X)/sizeof(Y);
|
||||||
simplifyCalculations();
|
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())
|
if (_settings->terminated())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Simplify simple calculations..
|
|
||||||
simplifyCalculations();
|
|
||||||
|
|
||||||
// Replace "*(ptr + num)" => "ptr[num]"
|
// Replace "*(ptr + num)" => "ptr[num]"
|
||||||
simplifyOffsetPointerDereference();
|
simplifyOffsetPointerDereference();
|
||||||
|
|
||||||
|
@ -6390,6 +6355,7 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
tok1->deleteThis();
|
tok1->deleteThis();
|
||||||
tok = tok1;
|
tok = tok1;
|
||||||
goback = true;
|
goback = true;
|
||||||
|
ret = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2664,6 +2664,26 @@ private:
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(code, tokenizeAndStringify(code, true));
|
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
|
void simplifyKnownVariables51() { // #4409 hang
|
||||||
|
|
Loading…
Reference in New Issue