diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 72008a62b..6ce28a0ca 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4473,11 +4473,11 @@ Token *Tokenizer::simplifyAddBracesToCommand(Token *tok) tokEnd=simplifyAddBracesPair(tok,true); } else if (tok->str()=="while") { Token *tokPossibleDo=tok->previous(); - if (tokPossibleDo && - tokPossibleDo->str()=="}") - tokPossibleDo=tokPossibleDo->link(); - if (!tokPossibleDo || - tokPossibleDo->strAt(-1) != "do") + if (Token::simpleMatch(tok->previous(), "{")) + tokPossibleDo = nullptr; + else if (Token::simpleMatch(tokPossibleDo,"}")) + tokPossibleDo = tokPossibleDo->link(); + if (!tokPossibleDo || tokPossibleDo->strAt(-1) != "do") tokEnd=simplifyAddBracesPair(tok,true); } else if (tok->str()=="do") { tokEnd=simplifyAddBracesPair(tok,false); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 51fd35492..566824d2a 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1343,6 +1343,17 @@ private: "}"; ASSERT_EQUALS(result, tokenizeAndStringify(code, true)); } + + { + // #8148 - while inside the do-while body + const char code[] = "void foo() {\n" + " do { while (x) f(); } while (y);\n" + "}"; + const char result[] = "void foo ( ) {\n" + "do { while ( x ) { f ( ) ; } } while ( y ) ;\n" + "}"; + ASSERT_EQUALS(result, tokenizeAndStringify(code, true)); + } } void forAddBraces1() {