Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,18 @@ class TestFunctions : public TestFixture {
"}\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"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be test that the valueType of s is unordered_set.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out the ValueType was correct all along, but we don't use that when looking up member functions.
I have added a test where there is no ambiguity between containers.

"}\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;
}

Expand Down
14 changes: 14 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ class TestSymbolDatabase : public TestFixture {

TEST_CASE(valueType1);
TEST_CASE(valueType2);
TEST_CASE(valueType3);
TEST_CASE(valueTypeThis);

TEST_CASE(variadic1); // #7453
Expand Down Expand Up @@ -8115,6 +8116,19 @@ class TestSymbolDatabase : public TestFixture {
}
}

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"));
Expand Down