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); }