Get type from auto with index operator (#4826)

This commit is contained in:
chrchr-github 2023-02-28 15:05:45 +01:00 committed by GitHub
parent 80050b11dd
commit 17e776861e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 0 deletions

View File

@ -2265,6 +2265,12 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool poi
if (Token::simpleMatch(tok2, "=") && Token::Match(tok2->astOperand2(), "!!=") && tok != tok2->astOperand2()) {
tok2 = tok2->astOperand2();
if (Token::simpleMatch(tok2, "[") && tok2->astOperand1()) {
const ValueType* vt = tok2->astOperand1()->valueType();
if (vt && vt->containerTypeToken)
return { vt->containerTypeToken, vt->containerTypeToken->linkAt(-1) };
}
const Token* varTok = tok2; // try to find a variable
if (Token::Match(varTok, ":: %name%"))
varTok = varTok->next();

View File

@ -1986,6 +1986,18 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(std::vector<std::unordered_map<int, std::unordered_set<int>>>& v, int i, int j) {\n"
" auto& s = v[i][j];\n"
" s.insert(0);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("int f(const std::vector<std::string>& v, int i, char c) {\n"
" const auto& s = v[i];\n"
" return s.find(c);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
settings = settings_old;
}

View File

@ -481,6 +481,7 @@ private:
TEST_CASE(valueType1);
TEST_CASE(valueType2);
TEST_CASE(valueType3);
TEST_CASE(valueTypeThis);
TEST_CASE(variadic1); // #7453
@ -8109,6 +8110,19 @@ private:
}
}
void valueType3() {
GET_SYMBOL_DB("void f(std::vector<std::unordered_map<int, std::unordered_set<int>>>& v, int i, int j) {\n"
" auto& s = v[i][j];\n"
" s.insert(0);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
const Token* tok = tokenizer.tokens();
tok = Token::findsimplematch(tok, "s .");
ASSERT(tok && tok->valueType());
ASSERT_EQUALS("container(std :: set|unordered_set <)", tok->valueType()->str());
}
void valueTypeThis() {
ASSERT_EQUALS("C *", typeOf("class C { C() { *this = 0; } };", "this"));
ASSERT_EQUALS("const C *", typeOf("class C { void foo() const; }; void C::foo() const { *this = 0; }", "this"));