Skip to content

Commit 29b651f

Browse files
Partial fix for #11543 checkLibraryFunction warning for smartpointer in container (#4781)
1 parent 36192c5 commit 29b651f

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ void SymbolDatabase::createSymbolDatabaseSetVariablePointers()
13231323
Token* memberTok = tok->next()->link()->tokAt(2);
13241324
const Scope* scope = vt->containerTypeToken->scope();
13251325
const Type* contType{};
1326-
const std::string typeStr = vt->containerTypeToken->expressionString();
1326+
const std::string& typeStr = vt->containerTypeToken->str(); // TODO: handle complex type expressions
13271327
while (scope && !contType) {
13281328
contType = scope->findType(typeStr); // find the type stored in the container
13291329
scope = scope->nestedIn;
@@ -2276,20 +2276,27 @@ void Variable::setValueType(const ValueType &valueType)
22762276
setFlag(fIsSmartPointer, true);
22772277
}
22782278

2279-
const Type *Variable::smartPointerType() const
2279+
const Type* Variable::smartPointerType() const
22802280
{
22812281
if (!isSmartPointer())
22822282
return nullptr;
22832283

22842284
if (mValueType->smartPointerType)
22852285
return mValueType->smartPointerType;
22862286

2287-
// TODO: Cache result
2288-
const Token *ptrType = typeStartToken();
2289-
while (Token::Match(ptrType, "%name%|::"))
2290-
ptrType = ptrType->next();
2291-
if (Token::Match(ptrType, "< %name% >"))
2292-
return ptrType->scope()->findType(ptrType->next()->str());
2287+
// TODO: Cache result, handle more complex type expression
2288+
const Token* typeTok = typeStartToken();
2289+
while (Token::Match(typeTok, "%name%|::"))
2290+
typeTok = typeTok->next();
2291+
if (Token::Match(typeTok, "< %name% >")) {
2292+
const Scope* scope = typeTok->scope();
2293+
const Type* ptrType{};
2294+
while (scope && !ptrType) {
2295+
ptrType = scope->findType(typeTok->next()->str());
2296+
scope = scope->nestedIn;
2297+
}
2298+
return ptrType;
2299+
}
22932300
return nullptr;
22942301
}
22952302

test/testfunctions.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,17 @@ class TestFunctions : public TestFixture {
19661966
"}\n");
19671967
ASSERT_EQUALS("", errout.str());
19681968

1969+
check("struct S {\n" // #11543
1970+
" S() {}\n"
1971+
" std::vector<std::shared_ptr<S>> v;\n"
1972+
" void f(int i) const;\n"
1973+
"};\n"
1974+
"void S::f(int i) const {\n"
1975+
" for (const std::shared_ptr<S>& c : v)\n"
1976+
" c->f(i);\n"
1977+
"}\n");
1978+
ASSERT_EQUALS("", errout.str());
1979+
19691980
check("namespace N {\n"
19701981
" struct S { static const std::set<std::string> s; };\n"
19711982
"}\n"

0 commit comments

Comments
 (0)