From 8aa9e448f59e23bea2c3ba628927873ebb676a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 26 Apr 2021 18:04:27 +0200 Subject: [PATCH] Parser; Set varid for structured binding variables --- lib/tokenize.cpp | 17 +++++++++++------ test/testvarid.cpp | 14 +++++++++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e6b16cc24..836c740f3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3857,15 +3857,20 @@ void Tokenizer::setVarIdPass1() continue; bool decl; - if (isCPP() && Token::Match(tok->previous(), "for ( const| auto &|&&| [")) { + if (isCPP() && mSettings->standards.cpp >= Standards::CPP17 && Token::Match(tok, "[(;{}] const| auto &|&&| [")) { + // Structured bindings tok2 = Token::findsimplematch(tok, "["); - while (tok2 && tok2->str() != "]") { - if (Token::Match(tok2, "%name% [,]]")) - variableMap.addVariable(tok2->str()); - tok2 = tok2->next(); + if ((Token::simpleMatch(tok->previous(), "for (") && Token::simpleMatch(tok2->link(), "] :")) || + Token::simpleMatch(tok2->link(), "] =")) { + while (tok2 && tok2->str() != "]") { + if (Token::Match(tok2, "%name% [,]]")) + variableMap.addVariable(tok2->str()); + tok2 = tok2->next(); + } + continue; } - 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 7c8b960b3..97e4c46ac 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -216,6 +216,8 @@ private: TEST_CASE(decltype2); TEST_CASE(exprid1); + + TEST_CASE(structuredBindings); } std::string tokenize(const char code[], const char filename[] = "test.cpp") { @@ -224,7 +226,7 @@ private: Settings settings; settings.platform(Settings::Unix64); settings.standards.c = Standards::C89; - settings.standards.cpp = Standards::CPP11; + settings.standards.cpp = Standards::CPPLatest; settings.checkUnusedTemplates = true; Tokenizer tokenizer(&settings, this); @@ -243,7 +245,7 @@ private: Settings settings; settings.platform(Settings::Unix64); settings.standards.c = Standards::C89; - settings.standards.cpp = Standards::CPP11; + settings.standards.cpp = Standards::CPPLatest; settings.checkUnusedTemplates = true; Tokenizer tokenizer(&settings, this); @@ -1285,7 +1287,7 @@ private: "2: list < int > :: iterator it@2 ;\n" "3: std :: vector < std :: string > dirs@3 ;\n" "4: std :: map < int , int > coords@4 ;\n" - "5: std :: unordered_map < int , int > xy@5 ;\n" + "5: std :: tr1 :: unordered_map < int , int > xy@5 ;\n" "6: std :: list < boost :: wave :: token_id > tokens@6 ;\n" "7: static std :: vector < CvsProcess * > ex1@7 ;\n" "8: extern std :: vector < CvsProcess * > ex2@8 ;\n" @@ -3429,6 +3431,12 @@ private: ASSERT_EQUALS(expected, actual); } + + void structuredBindings() { + const char code[] = "int foo() { auto [x,y] = xy(); return x+y; }"; + ASSERT_EQUALS("1: int foo ( ) { auto [ x@1 , y@2 ] = xy ( ) ; return x@1 + y@2 ; }\n", + tokenize(code)); + } }; REGISTER_TEST(TestVarID)