diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f9a9d9603..ecebb5001 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6000,14 +6000,25 @@ static std::string getExpression(const Token *tok) void Tokenizer::splitTemplateRightAngleBrackets(bool check) { - std::set vars; + std::vector> vars; + int scopeLevel = 0; for (Token *tok = list.front(); tok; tok = tok->next()) { + if (tok->str() == "{") + ++scopeLevel; + else if (tok->str() == "}") { + vars.erase(std::remove_if(vars.begin(), vars.end(), [scopeLevel](const std::pair& v) { + return v.second == scopeLevel; + }), vars.end()); + --scopeLevel; + } if (Token::Match(tok, "[;{}] %type% %type% [;,=]") && tok->next()->isStandardType()) - vars.insert(tok->strAt(2)); + vars.emplace_back(tok->strAt(2), scopeLevel); // Ticket #6181: normalize C++11 template parameter list closing syntax - if (tok->previous() && tok->str() == "<" && TemplateSimplifier::templateParameters(tok) && vars.find(tok->previous()->str()) == vars.end()) { + if (tok->previous() && tok->str() == "<" && TemplateSimplifier::templateParameters(tok) && std::none_of(vars.begin(), vars.end(), [&](const std::pair& v) { + return v.first == tok->previous()->str(); + })) { Token *endTok = tok->findClosingBracket(); if (check) { if (Token::Match(endTok, ">>|>>=")) @@ -6022,7 +6033,9 @@ void Tokenizer::splitTemplateRightAngleBrackets(bool check) endTok->insertToken("="); endTok->insertToken(">"); } - } else if (Token::Match(tok, "class|struct|union|=|:|public|protected|private %name% <") && vars.find(tok->next()->str()) == vars.end()) { + } else if (Token::Match(tok, "class|struct|union|=|:|public|protected|private %name% <") && std::none_of(vars.begin(), vars.end(), [&](const std::pair& v) { + return v.first == tok->next()->str(); + })) { Token *endTok = tok->tokAt(2)->findClosingBracket(); if (check) { if (Token::simpleMatch(endTok, ">>")) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 94bb21615..7d50009d4 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -3939,6 +3939,14 @@ private: "}"; ASSERT_EQUALS(std::string::npos, tokenizeAndStringify(code).find("> >")); } + { + const char code[] = "struct S { bool vector; };\n" + "struct T { std::vector> v; };\n"; + ASSERT_EQUALS("struct S { bool vector ; } ;\n" + "struct T { std :: vector < std :: shared_ptr < int > > v ; } ;", + tokenizeAndStringify(code)); + ASSERT_EQUALS("", errout.str()); + } } void cpp03template1() {