Fixed ticket #3557 (Tokenizer: simplification of '[]' doesn't work well):

extract undefined size array simplification and handle multiple arrays and combos between pointers and arrays, don't handle the definitions as arguments of function.
This commit is contained in:
Edoardo Prezioso 2012-01-26 17:07:10 +01:00
parent 6906001366
commit 6e164ae7ed
3 changed files with 46 additions and 7 deletions

View File

@ -3598,13 +3598,7 @@ bool Tokenizer::simplifyTokenList()
simplifySizeof();
// change array to pointer..
for (Token *tok = _tokens; tok; tok = tok->next()) {
if (Token::Match(tok, "%type% %var% [ ] [,;=]")) {
tok->next()->deleteNext(2);
tok->insertToken("*");
}
}
simplifyUndefinedSizeArray();
// Replace constants..
for (Token *tok = _tokens; tok; tok = tok->next()) {
@ -4732,6 +4726,35 @@ bool Tokenizer::simplifyQuestionMark()
return ret;
}
void Tokenizer::simplifyUndefinedSizeArray()
{
for (Token *tok = _tokens; tok; tok = tok->next()) {
if (Token::Match(tok, "%type%")) {
Token *tok2 = tok->next();
while (tok2 && tok2->str() == "*")
tok2 = tok2->next();
if (!Token::Match(tok2, "%var% [ ]"))
continue;
tok = tok2->previous();
Token *end = tok2->next();
unsigned int count = 0;
while (Token::Match(end, "[ ] [;=[]")) {
end = end->tokAt(2);
++count;
}
if (Token::Match(end, "[;=]")) {
do {
tok2->deleteNext(2);
tok->insertToken("*");
} while (--count);
tok = end;
} else
tok = tok->tokAt(3);
}
}
}
void Tokenizer::simplifyCasts()
{
for (Token *tok = _tokens; tok; tok = tok->next()) {

View File

@ -349,6 +349,11 @@ public:
*/
void simplifyCasts();
/**
* Change (multiple) arrays to (multiple) pointers.
*/
void simplifyUndefinedSizeArray();
/**
* A simplify function that replaces a variable with its value in cases
* when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)"

View File

@ -402,6 +402,8 @@ private:
TEST_CASE(removeRedundantFor);
TEST_CASE(consecutiveBraces);
TEST_CASE(undefinedSizeArray);
}
std::string tok(std::string code, bool simplify = true, Settings::PlatformType type = Settings::Unspecified) {
@ -7623,6 +7625,15 @@ private:
ASSERT_EQUALS("void f ( ) { for ( ; ; ) { } }", tok("void f () { for(;;){} }", true));
ASSERT_EQUALS("void f ( ) { { scope_lock lock ; foo ( ) ; } { scope_lock lock ; bar ( ) ; } }", tok("void f () { {scope_lock lock; foo();} {scope_lock lock; bar();} }", true));
}
void undefinedSizeArray() {
ASSERT_EQUALS("int * x ;", tok("int x [];"));
ASSERT_EQUALS("int * * x ;", tok("int x [][];"));
ASSERT_EQUALS("int * * x ;", tok("int * x [];"));
ASSERT_EQUALS("int * * * x ;", tok("int * x [][];"));
ASSERT_EQUALS("int * * * * x ;", tok("int * * x [][];"));
ASSERT_EQUALS("void f ( int x [ ] , double y [ ] ) { }", tok("void f(int x[], double y[]) { }"));
}
};
REGISTER_TEST(TestSimplifyTokens)