diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e9b464687..5779cb4b9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4213,6 +4213,17 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition) if (tokAfterCondition->str()=="{") { // already surrounded by braces tokBracesEnd=tokAfterCondition->link(); + } else if (Token::Match(tokAfterCondition, "try {") && + Token::Match(tokAfterCondition->linkAt(1), "} catch (")) { + tokAfterCondition->previous()->insertToken("{"); + Token * tokOpenBrace = tokAfterCondition->previous(); + Token * tokEnd = tokAfterCondition->linkAt(1)->linkAt(2)->linkAt(1); + + tokEnd->insertToken("}"); + Token * tokCloseBrace = tokEnd->next(); + + Token::createMutualLinks(tokOpenBrace, tokCloseBrace); + tokBracesEnd = tokCloseBrace; } else { Token * tokEnd = simplifyAddBracesToCommand(tokAfterCondition); if (!tokEnd) // Ticket #4887 diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 3a8ed7b4e..64d7a770d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -121,7 +121,8 @@ private: TEST_CASE(whileAddBraces); TEST_CASE(doWhileAddBraces); - TEST_CASE(forAddBraces); + TEST_CASE(forAddBraces1); + TEST_CASE(forAddBraces2); // #5088 TEST_CASE(pointers_condition); @@ -1459,7 +1460,7 @@ private: } } - void forAddBraces() { + void forAddBraces1() { { const char code[] = "void f() {\n" " for(;;)\n" @@ -1491,6 +1492,15 @@ private: } } + void forAddBraces2() { // #5088 + const char code[] = "void f() {\n" + " for(;;) try { } catch (...) { }\n" + "}"; + const char expected[] = "void f ( ) {\n" + "for ( ; ; ) { try { } catch ( . . . ) { } }\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } std::string simplifyKnownVariables(const char code[]) { errout.str("");