From fb7796e431a7e5bb8b3990942dbb4463e4f74844 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 9 Aug 2024 14:00:10 +0300 Subject: [PATCH 1/3] Fix 12999: Improve smart_pointer ValueType assignment --- lib/symboldatabase.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 2a8a4dce291..cfe1437697d 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -7305,9 +7305,9 @@ static const Token* parsedecl(const Token* type, type = type->next(); } break; - } else if (!valuetype->typeScope && (type->str() == "struct" || type->str() == "enum")) + } else if (!valuetype->typeScope && (type->str() == "struct" || type->str() == "enum") && valuetype->type != ValueType::Type::SMART_POINTER) valuetype->type = type->str() == "struct" ? ValueType::Type::RECORD : ValueType::Type::NONSTD; - else if (!valuetype->typeScope && type->type() && type->type()->classScope) { + else if (!valuetype->typeScope && type->type() && type->type()->classScope && valuetype->type != ValueType::Type::SMART_POINTER) { if (type->type()->classScope->type == Scope::ScopeType::eEnum) { valuetype->sign = ValueType::Sign::SIGNED; valuetype->type = getEnumType(type->type()->classScope, settings.platform); From 2a5ba83292a05c2175f0f5534b540c3594b719e0 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Sun, 11 Aug 2024 15:23:22 +0300 Subject: [PATCH 2/3] Tests --- test/testsymboldatabase.cpp | 17 +++++++++++++++++ test/testunusedvar.cpp | 1 - 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 5a69a402fbe..7f5bb0231f5 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -221,6 +221,7 @@ class TestSymbolDatabase : public TestFixture { TEST_CASE(VariableValueType3); TEST_CASE(VariableValueType4); // smart pointer type TEST_CASE(VariableValueType5); // smart pointer type + TEST_CASE(VariableValueType6); // smart pointer type TEST_CASE(VariableValueTypeReferences); TEST_CASE(VariableValueTypeTemplate); @@ -1264,6 +1265,22 @@ class TestSymbolDatabase : public TestFixture { ASSERT(p->valueType()->pointer == 1); } + void VariableValueType6() { + GET_SYMBOL_DB("struct Data{};\n" + "void foo(std::shared_ptr* p) { std::unique_ptr data = std::unique_ptr(new Data); }"); + + const Token * check{nullptr}; + for (const Token * tok = tokenizer.tokens(); tok; tok = tok->next()) { + if (Token::Match(tok, "> ( new")) { + check = tok->next(); + break; + } + } + ASSERT(check); + ASSERT(check->valueType()); + ASSERT(check->valueType()->smartPointerTypeToken); + } + void VariableValueTypeReferences() { { GET_SYMBOL_DB("void foo(int x) {}\n"); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index d8efbd702d4..e5d0b6ad99f 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -6552,7 +6552,6 @@ class TestUnusedVar : public TestFixture { " auto a2 = std::unique_ptr(new A());\n" "}\n"); ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'a' is assigned a value that is never used.\n" - "[test.cpp:8]: (style) Variable 'a2' is assigned a value that is never used.\n" "[test.cpp:8]: (style) Variable 'a2' is assigned a value that is never used.\n", // duplicate errout_str()); From 64799f1549c63e23857487236863f9bf23876277 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Sun, 11 Aug 2024 18:42:03 +0300 Subject: [PATCH 3/3] Cleanup --- test/testsymboldatabase.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 7f5bb0231f5..7a4af16df6e 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -1267,15 +1267,9 @@ class TestSymbolDatabase : public TestFixture { void VariableValueType6() { GET_SYMBOL_DB("struct Data{};\n" - "void foo(std::shared_ptr* p) { std::unique_ptr data = std::unique_ptr(new Data); }"); + "void foo() { std::unique_ptr data = std::unique_ptr(new Data); }"); - const Token * check{nullptr}; - for (const Token * tok = tokenizer.tokens(); tok; tok = tok->next()) { - if (Token::Match(tok, "> ( new")) { - check = tok->next(); - break; - } - } + const Token* check = Token::findsimplematch(tokenizer.tokens(), "( new"); ASSERT(check); ASSERT(check->valueType()); ASSERT(check->valueType()->smartPointerTypeToken);