From edc510623775c43fd0c58013d3b4ad457917f43c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 12 Feb 2022 08:19:07 +0100 Subject: [PATCH] Fix #9092 FN missingOverride - subclass in namespace (#3793) --- lib/symboldatabase.cpp | 7 +++++-- lib/symboldatabase.h | 6 +++--- test/testclass.cpp | 10 ++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 6d19fe994..3638aeaf9 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -755,7 +755,7 @@ void SymbolDatabase::createSymbolDatabaseClassInfo() for (Type& type : typeList) { // finish filling in base class info for (Type::BaseInfo & i : type.derivedFrom) { - const Type* found = findType(i.nameTok, type.enclosingScope); + const Type* found = findType(i.nameTok, type.enclosingScope, /*lookOutside*/ true); if (found && found->findDependency(&type)) { // circular dependency //mTokenizer->syntaxError(nullptr); @@ -5496,7 +5496,7 @@ const Scope *SymbolDatabase::findScope(const Token *tok, const Scope *startScope //--------------------------------------------------------------------------- -const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startScope) const +const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startScope, bool lookOutside) const { // skip over struct or union if (Token::Match(startTok, "struct|union")) @@ -5541,6 +5541,9 @@ const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startSc type = scope1->definedType; if (type) return type; + } else if (scope->type == Scope::ScopeType::eNamespace && lookOutside) { + scope = scope->nestedIn; + continue; } else break; } diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 8310b0841..b6c8f36b0 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1373,9 +1373,9 @@ public: /** For unit testing only */ const Scope *findScopeByName(const std::string& name) const; - const Type* findType(const Token *startTok, const Scope *startScope) const; - Type* findType(const Token *startTok, Scope *startScope) const { - return const_cast(this->findType(startTok, const_cast(startScope))); + const Type* findType(const Token *startTok, const Scope *startScope, bool lookOutside = false) const; + Type* findType(const Token *startTok, Scope *startScope, bool lookOutside = false) const { + return const_cast(this->findType(startTok, const_cast(startScope), lookOutside)); } const Scope *findScope(const Token *tok, const Scope *startScope) const; diff --git a/test/testclass.cpp b/test/testclass.cpp index 3d0e205b6..17d0fbde3 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -7446,6 +7446,16 @@ private: " void foo(int);\n" "};"); ASSERT_EQUALS("", errout.str()); + + checkOverride("struct B {\n" // #9092 + " virtual int f(int i) const = 0;\n" + "};\n" + "namespace N {\n" + " struct D : B {\n" + " virtual int f(int i) const;\n" + " };\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:6]: (style) The function 'f' overrides a function in a base class but is not marked with a 'override' specifier.\n", errout.str()); } void overrideCVRefQualifiers() {