================ @@ -0,0 +1,62 @@ +//===--- IncorrectEnableSharedFromThisCheck.cpp - clang-tidy --------------===// +// +// 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 "IncorrectEnableSharedFromThisCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/DeclCXX.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +void IncorrectEnableSharedFromThisCheck::registerMatchers(MatchFinder *Finder) { + const auto EnableSharedFromThis = + cxxRecordDecl(hasName("enable_shared_from_this"), isInStdNamespace()); + const auto QType = hasCanonicalType(hasDeclaration( + cxxRecordDecl( + anyOf(EnableSharedFromThis.bind("enable_rec"), + cxxRecordDecl(hasAnyBase(cxxBaseSpecifier( + isPublic(), hasType(hasCanonicalType( + hasDeclaration(EnableSharedFromThis)))))))) + .bind("base_rec"))); + Finder->addMatcher( + cxxRecordDecl( + hasDirectBase(cxxBaseSpecifier(unless(isPublic()), hasType(QType)) + .bind("base"))) + .bind("derived"), + this); +} + +void IncorrectEnableSharedFromThisCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *BaseSpec = Result.Nodes.getNodeAs<CXXBaseSpecifier>("base"); + const auto *Base = Result.Nodes.getNodeAs<CXXRecordDecl>("base_rec"); + const auto *Derived = Result.Nodes.getNodeAs<CXXRecordDecl>("derived"); + const bool IsEnableSharedFromThisDirectBase = + Result.Nodes.getNodeAs<CXXRecordDecl>("enable_rec") == Base; + const SourceRange ReplacementRange = BaseSpec->getSourceRange(); + const std::string ReplacementString = + // BaseSpec->getType().getAsString() results in + // std::enable_shared_from_this<ClassName> or + // alias/typedefs of std::enable_shared_from_this<ClassName> + "public " + BaseSpec->getType().getAsString(); ---------------- MichelleCDjunaidi wrote:
approach confirmed to not work due to getBaseTypeLoc() not including the virtual or the access specifier. As a result, doing a blind SourceRange(BaseSpec->getBeginLoc(), BaseSpec->getBaseTypeLoc()) replacement with "public" just ends up with ``` //alias the template itself template <typename T> using esft_template = std::enable_shared_from_this<T>; -class PrivateAliasTemplateClassBase : private esft_template<PrivateAliasTemplateClassBase> {}; +class PrivateAliasTemplateClassBase : public<PrivateAliasTemplateClassBase> {}; // // -class DefaultAliasTemplateClassBase : esft_template<DefaultAliasTemplateClassBase> {}; +class DefaultAliasTemplateClassBase : public<DefaultAliasTemplateClassBase> {}; // // class PublicAliasTemplateClassBase : public esft_template<PublicAliasTemplateClassBase> {}; -struct PrivateAliasTemplateStructBase : private esft_template<PrivateAliasTemplateStructBase> {}; +struct PrivateAliasTemplateStructBase : public<PrivateAliasTemplateStructBase> {}; // // struct A : std::enable_shared_from_this<A> {}; #define MACRO_A A -class B : MACRO_A {}; -class C : private MACRO_A {}; +class B : public {}; +class C : public {}; ``` advice would be appreciated, because as it stands it doesn't seem possible to extract only the access specifier range. Current approach in my head is probably having a dedicated if statement just for macros, but that's kind of not ideal @5chmidti https://github.com/llvm/llvm-project/pull/102299 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits