tokenizer: improved sizeof handling

This commit is contained in:
Daniel Marjamäki 2009-02-02 17:27:34 +00:00
parent 0e291c772c
commit 27c0f786bc
3 changed files with 66 additions and 39 deletions

View File

@ -679,7 +679,7 @@ void Tokenizer::simplifyTokenList()
int sz = 100; int sz = 100;
// Try to locate variable declaration.. // Try to locate variable declaration..
const Token *decltok = Token::findmatch(_tokens, (std::string("%type% ") + tok->strAt(3) + " [").c_str()); const Token *decltok = Token::findmatch(_tokens, "%type% %varid% [", tok->tokAt(3)->varId());
if (decltok) if (decltok)
{ {
sz = SizeOfType(decltok->strAt(0)); sz = SizeOfType(decltok->strAt(0));
@ -704,7 +704,10 @@ void Tokenizer::simplifyTokenList()
if (size <= 0) if (size <= 0)
continue; continue;
const char *varname = tok->strAt(1); const unsigned int varid = tok->next()->varId();
if (varid == 0)
continue;
int total_size = size * std::atoi(tok->strAt(3)); int total_size = size * std::atoi(tok->strAt(3));
// Replace 'sizeof(var)' with number // Replace 'sizeof(var)' with number
@ -723,10 +726,7 @@ void Tokenizer::simplifyTokenList()
break; break;
} }
// Todo: Token::Match varname directly else if (Token::Match(tok2, "sizeof ( %varid% )", varid))
else if (Token::Match(tok2, "sizeof ( %var% )"))
{
if (strcmp(tok2->strAt(2), varname) == 0)
{ {
std::ostringstream str; std::ostringstream str;
str << total_size; str << total_size;
@ -739,7 +739,6 @@ void Tokenizer::simplifyTokenList()
} }
} }
} }
}

View File

@ -43,10 +43,10 @@ private:
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// Assign variable ids // Assign variable ids
tokenizer.setVarId(); tokenizer.setVarId();
tokenizer.simplifyTokenList();
// Fill function list // Fill function list
tokenizer.fillFunctionList(); tokenizer.fillFunctionList();

View File

@ -67,6 +67,7 @@ private:
TEST_CASE(varid1); TEST_CASE(varid1);
TEST_CASE(varid2); TEST_CASE(varid2);
// TODO TEST_CASE(varid3);
TEST_CASE(file1); TEST_CASE(file1);
TEST_CASE(file2); TEST_CASE(file2);
@ -78,7 +79,7 @@ private:
TEST_CASE(reduce_redundant_paranthesis); // Ticket #61 TEST_CASE(reduce_redundant_paranthesis); // Ticket #61
TEST_CASE(sizeof1); TEST_CASE(sizeof1);
// TODO TEST_CASE(sizeof2); TEST_CASE(sizeof2);
} }
@ -645,6 +646,30 @@ private:
} }
} }
void varid3()
{
const std::string code("static char str[4];\n"
"void f()\n"
"{\n"
" char str[10];\n"
" str[0] = 0;\n"
"}\n");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "str [ 4"))
ASSERT_EQUALS(1, tok->varId());
else if (tok->str() == "str")
ASSERT_EQUALS(2, tok->varId());
}
}
void file1() void file1()
{ {
@ -806,32 +831,11 @@ private:
void sizeof1() void sizeof1()
{ {
const char code[] = "int i[4];\n" const char code[] = "void foo()\n"
"sizeof(i);\n"
"sizeof(*i);\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" int i [ 4 ] ; 16 ; 4 ;"), ostr.str());
}
void sizeof2()
{
const char code[] = "int i[4];\n"
"void f()\n"
"{\n" "{\n"
" int i[10];\n" " int i[4];\n"
" sizeof(i);\n" " sizeof(i);\n"
" sizeof(*i);\n" " sizeof(*i);\n"
" sizeof(i[0]);\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
@ -839,12 +843,36 @@ private:
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
std::ostringstream ostr; std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str(); ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; 4 ; 4 ; }"), ostr.str()); ASSERT_EQUALS(std::string(" void foo ( ) { int i [ 4 ] ; 16 ; 4 ; }"), ostr.str());
}
void sizeof2()
{
const char code[] = "static int i[4];\n"
"void f()\n"
"{\n"
" int i[10];\n"
" sizeof(i);\n"
"}\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; }"), ostr.str());
} }
}; };