From 26612ab846c3164435bfcb3593fee1bd6983494f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 23 Jan 2016 11:43:48 +0100 Subject: [PATCH] Fixed #5332 (Tokenizer: if braces not added properly 'if (x==123) label: {}') --- lib/tokenize.cpp | 5 +++++ test/testtokenize.cpp | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 3d7e3ec69..1980a9af7 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4521,6 +4521,11 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition) Token::createMutualLinks(tokOpenBrace, tokCloseBrace); tokBracesEnd = tokCloseBrace; + } else if (Token::Match(tokAfterCondition, "%name% : {")) { + tokAfterCondition->previous()->insertToken("{"); + tokAfterCondition->linkAt(2)->insertToken("}"); + tokBracesEnd = tokAfterCondition->linkAt(2)->next(); + Token::createMutualLinks(tokAfterCondition->previous(), tokBracesEnd); } else { Token * tokEnd = simplifyAddBracesToCommand(tokAfterCondition); if (!tokEnd) // Ticket #4887 diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 780fe1fff..aab65e355 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -117,6 +117,7 @@ private: TEST_CASE(ifAddBraces18); // #3424 - if if { } else else TEST_CASE(ifAddBraces19); // #3928 - if for if else TEST_CASE(ifAddBraces20); // #5012 - syntax error 'else }' + TEST_CASE(ifAddBraces21); // #5332 - if (x) label: {} .. TEST_CASE(whileAddBraces); TEST_CASE(doWhileAddBraces); @@ -1247,6 +1248,11 @@ private: ASSERT_THROW(tokenizeAndStringify(code, true), InternalError); } + void ifAddBraces21() { // #5332 - if (x) label: {} ... + const char code[] = "void f() { if(x) label: {} a=1; }"; + ASSERT_EQUALS("void f ( ) { if ( x ) { label : ; { } } a = 1 ; }", tokenizeAndStringify(code, false)); + } + void whileAddBraces() { const char code[] = ";while(a);"; ASSERT_EQUALS("; while ( a ) { ; }", tokenizeAndStringify(code, true));