From 29b651f2648f10a718f72e0c4edf0f233e72eb8c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 25 Feb 2023 15:47:02 +0100 Subject: [PATCH] Partial fix for #11543 checkLibraryFunction warning for smartpointer in container (#4781) --- lib/symboldatabase.cpp | 23 +++++++++++++++-------- test/testfunctions.cpp | 11 +++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 1f5c4b22e..d393d0181 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1323,7 +1323,7 @@ void SymbolDatabase::createSymbolDatabaseSetVariablePointers() Token* memberTok = tok->next()->link()->tokAt(2); const Scope* scope = vt->containerTypeToken->scope(); const Type* contType{}; - const std::string typeStr = vt->containerTypeToken->expressionString(); + const std::string& typeStr = vt->containerTypeToken->str(); // TODO: handle complex type expressions while (scope && !contType) { contType = scope->findType(typeStr); // find the type stored in the container scope = scope->nestedIn; @@ -2276,7 +2276,7 @@ void Variable::setValueType(const ValueType &valueType) setFlag(fIsSmartPointer, true); } -const Type *Variable::smartPointerType() const +const Type* Variable::smartPointerType() const { if (!isSmartPointer()) return nullptr; @@ -2284,12 +2284,19 @@ const Type *Variable::smartPointerType() const if (mValueType->smartPointerType) return mValueType->smartPointerType; - // TODO: Cache result - const Token *ptrType = typeStartToken(); - while (Token::Match(ptrType, "%name%|::")) - ptrType = ptrType->next(); - if (Token::Match(ptrType, "< %name% >")) - return ptrType->scope()->findType(ptrType->next()->str()); + // TODO: Cache result, handle more complex type expression + const Token* typeTok = typeStartToken(); + while (Token::Match(typeTok, "%name%|::")) + typeTok = typeTok->next(); + if (Token::Match(typeTok, "< %name% >")) { + const Scope* scope = typeTok->scope(); + const Type* ptrType{}; + while (scope && !ptrType) { + ptrType = scope->findType(typeTok->next()->str()); + scope = scope->nestedIn; + } + return ptrType; + } return nullptr; } diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 50648588b..ba9b628fa 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1966,6 +1966,17 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("struct S {\n" // #11543 + " S() {}\n" + " std::vector> v;\n" + " void f(int i) const;\n" + "};\n" + "void S::f(int i) const {\n" + " for (const std::shared_ptr& c : v)\n" + " c->f(i);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("namespace N {\n" " struct S { static const std::set s; };\n" "}\n"