From 7a729320787880007dc5191d8a3655c4c29cbafa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 20 Feb 2010 18:13:09 +0100 Subject: [PATCH] Tokenizer: simple simplification of array sizes --- lib/tokenize.cpp | 26 ++++++++++++++++++++++++++ lib/tokenize.h | 3 +++ test/testtokenize.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f2a3d1e40..c89df408b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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(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) diff --git a/lib/tokenize.h b/lib/tokenize.h index cdd15a250..5c6bcae30 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -139,6 +139,9 @@ public: private: #endif + /** Insert array size where it isn't given */ + void arraySize(); + /** Remove redundant assignment */ void removeRedundantAssignment(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 3bc3a045d..19d13bdd0 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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 ( ) ;\n}void fn2 ( 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)