Fix #11309 debug: Scope::checkVariable found variable 'v' with varid 0 (#5006)

* Fix #11309 debug: Scope::checkVariable found variable 'v' with varid 0

* Format
This commit is contained in:
chrchr-github 2023-04-24 20:27:10 +02:00 committed by GitHub
parent 6820b70dd1
commit 9ea223b367
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View File

@ -6000,14 +6000,25 @@ static std::string getExpression(const Token *tok)
void Tokenizer::splitTemplateRightAngleBrackets(bool check)
{
std::set<std::string> vars;
std::vector<std::pair<std::string, int>> 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<std::string, int>& 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<std::string, int>& 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<std::string, int>& v) {
return v.first == tok->next()->str();
})) {
Token *endTok = tok->tokAt(2)->findClosingBracket();
if (check) {
if (Token::simpleMatch(endTok, ">>"))

View File

@ -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<std::shared_ptr<int>> 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() {