From 4d3013d43d049399a4f208fdea2662065ee7e3b9 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sun, 26 Feb 2012 02:44:16 +0100 Subject: [PATCH] Improve 'Tokenizer::elseif' code in order to not touch 'else if' inside a macro parenthesis. --- lib/tokenize.cpp | 15 ++++++++------- test/testsimplifytokens.cpp | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 72babb620..3ef9ba253 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6611,16 +6611,17 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign void Tokenizer::elseif() { for (Token *tok = _tokens; tok; tok = tok->next()) { + if (tok->str() == "(" || tok->str() == "[" || + (tok->str() == "{" && tok->previous() && tok->previous()->str() == "=")) + tok = tok->link(); + if (!Token::simpleMatch(tok, "else if")) continue; - int indent = 0; - for (Token *tok2 = tok; indent >= 0 && tok2; tok2 = tok2->next()) { - if (Token::Match(tok2, "(|{")) - ++indent; - else if (Token::Match(tok2, ")|}")) - --indent; + for (Token *tok2 = tok; tok2; tok2 = tok2->next()) { + if (Token::Match(tok2, "(|{|[")) + tok2 = tok2->link(); - if (indent == 0 && Token::Match(tok2, "}|;")) { + if (Token::Match(tok2, "}|;")) { if (tok2->next() && tok2->next()->str() != "else") { tok->insertToken("{"); tok2->insertToken("}"); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index ce2a50924..0a7d03a9b 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -869,6 +869,7 @@ private: Tokenizer tokenizer(&settings, this); std::istringstream istr(code); tokenizer.createTokens(istr); + tokenizer.tokenize(istr, "test.cpp"); tokenizer.elseif(); return tokenizer.tokens()->stringifyList(false); }