From 873572d489d2efa1c6c5d72604b779667eaccdb7 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Fri, 30 Dec 2011 12:12:54 +0100 Subject: [PATCH] Improve C# simplification code with arrays of arrays. --- lib/tokenize.cpp | 27 ++++++++++++++++++++------- test/testtokenize.cpp | 6 ++++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d2b11465f..442e2393e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2639,14 +2639,27 @@ void Tokenizer::simplifyJavaAndCSharp() if (Token::Match(tok, ") throws %var% {")) tok->deleteNext(2); } else { - if (Token::Match(tok, "%type% [ ] %var% [=;]") && - (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) { - tok->deleteNext(2); - tok->insertToken("*"); - tok = tok->tokAt(2); - if (tok->next()->str() == "=") - tok = tok->next(); + //simplify C# arrays of arrays declarations + while (Token::Match(tok, "%type% [ ]") && + (!tok->previous() || Token::Match(tok->previous(), "[,;{}]"))) { + Token *tok2 = tok->tokAt(3); + unsigned int count = 1; + while (Token::simpleMatch(tok2, "[ ]")) { + tok2 = tok2->tokAt(2); + ++count; + } + if (!tok2) + break; + else if (Token::Match(tok2, "%var% [;,=]")) { + Token::eraseTokens(tok, tok2); + do { + tok->insertToken("*"); + } while (--count); + tok = tok2->tokAt(2); + } } + if (!tok) + break; } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 5760b5fcd..ba214c849 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5932,8 +5932,10 @@ private: bool expand = true; Settings::PlatformType platform = Settings::Unspecified; const std::string filename="test.cs"; - ASSERT_EQUALS("int * i ;", tokenizeAndStringify("int [] i;", simplify, expand, platform, filename)); - ASSERT_EQUALS("; int * i ;", tokenizeAndStringify("; int [] i;", simplify, expand, platform, filename)); + ASSERT_EQUALS("int * x ;", tokenizeAndStringify("int [] x;", simplify, expand, platform, filename)); + ASSERT_EQUALS("; int * x , int * y ;", tokenizeAndStringify("; int [] x, int [] y;", simplify, expand, platform, filename)); + ASSERT_EQUALS("; int * * x ;", tokenizeAndStringify("; int [][] x;", simplify, expand, platform, filename)); + ASSERT_EQUALS("; int * * * x ;", tokenizeAndStringify("; int [][][] x;", simplify, expand, platform, filename)); } std::string javatest(const char javacode[]) {