From 9bb1a1b7a5c846f7783ef20f86e8b81029bf83f6 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Thu, 27 Oct 2011 02:57:38 +0200 Subject: [PATCH] Added code for simplification of 'for ( a; b; c;); -> '{ a; for (; b; c;) ; }'. It's not enabled because it fails many tests with testrunner. --- lib/tokenize.cpp | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4a03f4069..517e48304 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2274,9 +2274,42 @@ bool Tokenizer::tokenize(std::istream &code, simplifyIfNot(); simplifyIfNotNull(); + //simplify for: move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }" + //not enabled because it fails many tests with testrunner. + //@todo fix these fails before enabling this simplification + /*for (Token* tok = _tokens; tok; tok = tok->next()) { + if (tok->str() == "(" && ( !tok->previous() || tok->previous()->str() != "for")) { + tok = tok->link(); + continue; + } + if (!Token::Match(tok->previous(),"[{};] for (") || Token::simpleMatch(tok, "for ( ;")) + continue; + + //find the two needed semicolons inside the 'for' + const Token *firstsemicolon = Token::findmatch(tok->next(), ";", tok->next()->link()); + if (!firstsemicolon || !Token::findmatch(firstsemicolon->next(), ";", tok->next()->link())) + continue; + + tok = tok->previous(); + tok->insertToken(";"); + tok->insertToken("{"); + tok = tok->next(); + Token *end = tok->tokAt(3)->link(); + if (Token::simpleMatch(end, ") {")) { + end = end->next()->link(); + end->insertToken("}"); + Token::createMutualLinks(tok, end->next()); + end = end->link()->previous(); + } else { + end->insertToken("}"); + Token::createMutualLinks(tok, end->next()); + } + Token *begin = tok->tokAt(4); + end = firstsemicolon->previous(); + Token::move(begin, end, tok); + }*/ /** * @todo simplify "for" - * - move out start-statement "for (a;b;c);" => "{ a; for(;b;c); }" * - try to change "for" loop to a "while" loop instead */ @@ -8636,18 +8669,18 @@ void Tokenizer::simplifyWhile0() { for (Token *tok = _tokens; tok; tok = tok->next()) { // while (0) - const bool while0(Token::Match(tok, "while ( 0|false )")); + const bool while0(Token::Match(tok->previous(), "[{};] while ( 0|false )")); // for (0) - not banal, ticket #3140 - const bool for0((Token::Match(tok, "for ( %var% = %num% ; %var% < %num% ;") && + const bool for0((Token::Match(tok->previous(), "[{};] for ( %var% = %num% ; %var% < %num% ;") && tok->strAt(2) == tok->strAt(6) && tok->strAt(4) == tok->strAt(8)) || - (Token::Match(tok, "for ( %type% %var% = %num% ; %var% < %num% ;") && + (Token::Match(tok->previous(), "[{};] for ( %type% %var% = %num% ; %var% < %num% ;") && tok->strAt(3) == tok->strAt(7) && tok->strAt(5) == tok->strAt(9))); if (!while0 && !for0) continue; - if (while0 && Token::simpleMatch(tok->previous(), "}")) { + if (while0 && tok->previous()->str() == "}") { // find "do" Token *tok2 = tok->previous()->link(); tok2 = tok2->previous();