Skip to content

Commit 9ea223b

Browse files
Fix #11309 debug: Scope::checkVariable found variable 'v' with varid 0 (#5006)
* Fix #11309 debug: Scope::checkVariable found variable 'v' with varid 0 * Format
1 parent 6820b70 commit 9ea223b

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

lib/tokenize.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6000,14 +6000,25 @@ static std::string getExpression(const Token *tok)
60006000

60016001
void Tokenizer::splitTemplateRightAngleBrackets(bool check)
60026002
{
6003-
std::set<std::string> vars;
6003+
std::vector<std::pair<std::string, int>> vars;
60046004

6005+
int scopeLevel = 0;
60056006
for (Token *tok = list.front(); tok; tok = tok->next()) {
6007+
if (tok->str() == "{")
6008+
++scopeLevel;
6009+
else if (tok->str() == "}") {
6010+
vars.erase(std::remove_if(vars.begin(), vars.end(), [scopeLevel](const std::pair<std::string, int>& v) {
6011+
return v.second == scopeLevel;
6012+
}), vars.end());
6013+
--scopeLevel;
6014+
}
60066015
if (Token::Match(tok, "[;{}] %type% %type% [;,=]") && tok->next()->isStandardType())
6007-
vars.insert(tok->strAt(2));
6016+
vars.emplace_back(tok->strAt(2), scopeLevel);
60086017

60096018
// Ticket #6181: normalize C++11 template parameter list closing syntax
6010-
if (tok->previous() && tok->str() == "<" && TemplateSimplifier::templateParameters(tok) && vars.find(tok->previous()->str()) == vars.end()) {
6019+
if (tok->previous() && tok->str() == "<" && TemplateSimplifier::templateParameters(tok) && std::none_of(vars.begin(), vars.end(), [&](const std::pair<std::string, int>& v) {
6020+
return v.first == tok->previous()->str();
6021+
})) {
60116022
Token *endTok = tok->findClosingBracket();
60126023
if (check) {
60136024
if (Token::Match(endTok, ">>|>>="))
@@ -6022,7 +6033,9 @@ void Tokenizer::splitTemplateRightAngleBrackets(bool check)
60226033
endTok->insertToken("=");
60236034
endTok->insertToken(">");
60246035
}
6025-
} else if (Token::Match(tok, "class|struct|union|=|:|public|protected|private %name% <") && vars.find(tok->next()->str()) == vars.end()) {
6036+
} else if (Token::Match(tok, "class|struct|union|=|:|public|protected|private %name% <") && std::none_of(vars.begin(), vars.end(), [&](const std::pair<std::string, int>& v) {
6037+
return v.first == tok->next()->str();
6038+
})) {
60266039
Token *endTok = tok->tokAt(2)->findClosingBracket();
60276040
if (check) {
60286041
if (Token::simpleMatch(endTok, ">>"))

test/testtokenize.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,6 +3939,14 @@ class TestTokenizer : public TestFixture {
39393939
"}";
39403940
ASSERT_EQUALS(std::string::npos, tokenizeAndStringify(code).find("> >"));
39413941
}
3942+
{
3943+
const char code[] = "struct S { bool vector; };\n"
3944+
"struct T { std::vector<std::shared_ptr<int>> v; };\n";
3945+
ASSERT_EQUALS("struct S { bool vector ; } ;\n"
3946+
"struct T { std :: vector < std :: shared_ptr < int > > v ; } ;",
3947+
tokenizeAndStringify(code));
3948+
ASSERT_EQUALS("", errout.str());
3949+
}
39423950
}
39433951

39443952
void cpp03template1() {

0 commit comments

Comments
 (0)