Tokenizer: simple simplification of array sizes

This commit is contained in:
Daniel Marjamäki 2010-02-20 18:13:09 +01:00
parent e4cc8cf1a0
commit 7a72932078
3 changed files with 54 additions and 0 deletions

View File

@ -1062,6 +1062,8 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::s
return false;
}
arraySize();
simplifyDoWhileAddBraces();
simplifyIfAddBraces();
@ -1223,6 +1225,29 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::s
}
//---------------------------------------------------------------------------
/** Specify array size if it hasn't been given.. */
void Tokenizer::arraySize()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "%var% [ ] = {"))
{
unsigned int sz = 1;
const Token *tok2 = tok->tokAt(5);
while (Token::Match(tok2, "%any% ,"))
{
sz++;
tok2 = tok2->tokAt(2);
}
if (Token::Match(tok2, "%any% } ;"))
tok->next()->insertToken(MathLib::toString<long>(sz));
}
}
}
/**
* is the token pointing at a template parameters block..
* < int , 3 > => yes
@ -3145,6 +3170,7 @@ bool Tokenizer::removeReduntantConditions()
return ret;
}
void Tokenizer::simplifyIfAddBraces()
{
for (Token *tok = _tokens; tok; tok = tok ? tok->next() : NULL)

View File

@ -139,6 +139,9 @@ public:
private:
#endif
/** Insert array size where it isn't given */
void arraySize();
/** Remove redundant assignment */
void removeRedundantAssignment();

View File

@ -196,6 +196,8 @@ private:
TEST_CASE(removedeclspec);
TEST_CASE(cpp0xtemplate);
TEST_CASE(arraySize);
}
@ -3050,6 +3052,29 @@ private:
"}\n";
ASSERT_EQUALS(";\n\n\nint main ( )\n{\nfn2<int> ( ) ;\n}void fn2<int> ( int t = [ ] { return 1 ; } ( ) )\n{ }", tokenizeAndStringify(code));
}
std::string arraySize_(const std::string &code)
{
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code.c_str());
tokenizer.tokenize(istr, "test.cpp");
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
{
if (tok->isName())
ostr << " ";
ostr << tok->str();
}
return ostr.str();
}
void arraySize()
{
ASSERT_EQUALS("; int a[3]={1,2,3};", arraySize_(";int a[]={1,2,3};"));
}
};
REGISTER_TEST(TestTokenizer)