From 84995485eaaeebc12d2129da560a99ac471bbab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 27 Feb 2020 07:18:07 +0100 Subject: [PATCH] VarId: fixed varids for 'for (auto [x,y]: xy)' --- lib/tokenize.cpp | 9 +++++++++ test/testvarid.cpp | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 44e3efe47..5e80a203c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3429,6 +3429,15 @@ void Tokenizer::setVarIdPass1() continue; bool decl; + if (isCPP() && Token::Match(tok->previous(), "for ( const| auto &|&&| [")) { + tok2 = Token::findsimplematch(tok, "["); + while (tok2 && tok2->str() != "]") { + if (Token::Match(tok2, "%name% [,]]")) + variableMap.addVariable(tok2->str()); + tok2 = tok2->next(); + } + continue; + } try { /* Ticket #8151 */ decl = setVarIdParseDeclaration(&tok2, variableMap.map(), scopeStack.top().isExecutable, isCPP(), isC()); } catch (const Token * errTok) { diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 708240dd3..418bf94eb 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -166,6 +166,8 @@ private: TEST_CASE(varid_trailing_return2); // #9066 TEST_CASE(varid_parameter_pack); // #9383 + TEST_CASE(varid_for_auto_cpp17); + TEST_CASE(varidclass1); TEST_CASE(varidclass2); TEST_CASE(varidclass3); @@ -2550,6 +2552,22 @@ private: ASSERT_EQUALS(exp1, tokenize(code1)); } + void varid_for_auto_cpp17() { + const char code[] = "void f() {\n" + " for (auto [x,y,z]: xyz) {\n" + " x+y+z;\n" + " }\n" + " x+y+z;\n" + "}"; + const char exp1[] = "1: void f ( ) {\n" + "2: for ( auto [ x@1 , y@2 , z@3 ] : xyz ) {\n" + "3: x@1 + y@2 + z@3 ;\n" + "4: }\n" + "5: x + y + z ;\n" + "6: }\n"; + ASSERT_EQUALS(exp1, tokenize(code)); + } + void varidclass1() { const std::string actual = tokenize( "class Fred\n"