Get type from auto with index operator (#4826)
This commit is contained in:
parent
80050b11dd
commit
17e776861e
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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"));
|
||||
|
|
Loading…
Reference in New Issue