================ @@ -0,0 +1,52 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ForbidNonVirtualBaseDtorCheck.h" +#include "clang/AST/DeclCXX.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Specifiers.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +void ForbidNonVirtualBaseDtorCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + cxxRecordDecl(isDefinition(), + hasAnyBase(cxxBaseSpecifier().bind("BaseSpecifier"))) + .bind("derived"), + this); +} + +void ForbidNonVirtualBaseDtorCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *Derived = Result.Nodes.getNodeAs<CXXRecordDecl>("derived"); + const auto *BaseSpecifier = + Result.Nodes.getNodeAs<CXXBaseSpecifier>("BaseSpecifier"); + if (!Derived || !BaseSpecifier) + return; + if (BaseSpecifier->getAccessSpecifier() != AS_public) + return; + const auto *BaseType = BaseSpecifier->getType()->getAsCXXRecordDecl(); + if (!BaseType || !BaseType->hasDefinition()) + return; + const auto *Dtor = BaseType->getDestructor(); + if (Dtor && Dtor->isVirtual()) + return; + if (Dtor && Dtor->getAccess() == AS_protected && !Dtor->isVirtual()) + return; + if (Derived->isEmpty()) ---------------- zeyi2 wrote:
IIUC `isEmpty()` returns false if the base class is non-empty. Could we use `field_empty()` instead to check if the derived class itself adds data? https://github.com/llvm/llvm-project/pull/183384 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
