From bab740289c6a47ac27550fee01ca62ee0e8a787d Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Fri, 28 Oct 2011 00:37:39 +0200 Subject: [PATCH] Complete todo: convert 'for' into 'while'. It's not possible to enable this code because of the disaster it will be created for all the checks with the 'for' pattern. --- lib/tokenize.cpp | 85 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 22 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7f75e8d3c..0d9b715d9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2282,36 +2282,77 @@ bool Tokenizer::tokenize(std::istream &code, tok = tok->link(); continue; } - if (!Token::Match(tok->previous(),"[{};] for (") || Token::simpleMatch(tok, "for ( ;")) + if (!Token::Match(tok->previous(),"[{};] 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())) + if (!firstsemicolon) continue; + const Token *secondsemicolon = Token::findmatch(firstsemicolon->next(), ";", tok->next()->link()); + if (!secondsemicolon) + continue; + if (Token::findmatch(secondsemicolon->next(), ";", tok->next()->link())) + continue; //no more than two semicolons! + if (!tok->next()->link()->next()) + continue; //there should be always something after 'for (...)' - 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 *fortok = tok; + Token *begin = tok->tokAt(2); + Token *end = tok->next()->link(); + if ( begin->str() != ";" ) { + tok = tok->previous(); + tok->insertToken(";"); + tok->insertToken("{"); + tok = tok->next(); + if (end->next()->str() =="{") { + end = end->next()->link(); + end->insertToken("}"); + Token::createMutualLinks(tok, end->next()); + end = end->link()->previous(); + } else { + if (end->next()->str() != ";") + end->insertToken(";"); + end = end->next(); + end->insertToken("}"); + Token::createMutualLinks(tok, end->next()); + } + end = firstsemicolon->previous(); + Token::move(begin, end, tok); + tok = fortok; + end = fortok->next()->link(); } - Token *begin = tok->tokAt(4); - end = firstsemicolon->previous(); + //every 'for' is changed to 'for(;b;c), now it's possible to convert the 'for' to a 'while'. + //precisely, 'for(;b;c){code}'-> 'while(b){code + c;}' + fortok->str("while"); + begin = firstsemicolon->previous(); + begin->deleteNext(); + begin = secondsemicolon->previous(); + begin->deleteNext(); + begin = begin->next(); + if (begin->str() == ")") { //'for(;b;)' -> 'while(b)' + if (begin->previous()->str() == "(") //'for(;;)' -> 'while(true)' + begin->previous()->insertToken("true"); + tok = fortok; + continue; + } + if (end->next()->str() =="{") { + tok = end->next()->link()->previous(); + tok->insertToken(";"); + } else { + tok = end; + if (end->next()->str() != ";") + tok->insertToken(";"); + tok->insertToken("{"); + tok = tok->tokAt(2); + tok->insertToken("}"); + Token::createMutualLinks(tok->previous(), tok->next()); + tok = tok->previous(); + } + end = end->previous(); Token::move(begin, end, tok); - }*/ - /** - * @todo simplify "for" - * - try to change "for" loop to a "while" loop instead - */ + tok = fortok; + }*/ simplifyConst();