[clang-tools-extra] [clang-tidy] Create bugprone-public-enable-shared-from-this check (PR #102299)

via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 12 19:35:01 PDT 2024


MichelleCDjunaidi wrote:

> > maintaining the name of the matched classes in a vector and checking for every other class if it inherits from `::std::enable_shared_from_this` or classes in the vector.
> 
> You wouldn't want to save the name, because then you would do a lot of string comparisons when you could do pointer comparisons (which would require using `RDecl->getCanonical()` to ensure you get the same record decl, I believe). Instead of a vector, you could use a set. Then checking the base classes would simply be a call to `contains` (`llvm::SmallPtrSet`?).
> 
> > Wouldn't this necessitate changing the matchers and the check to instead allow RecursiveASTVisitor to handle the context instead?
> 
> Kind of. In this case, you would match the translation unit and call `TraverseTranslationUnitDecl` on it. Then you implement `bool VisitCXXRecordDecl(CXXRecordDecl* RDecl)` and check if `RDecl` is of type `std::enable_shared_from_this` and adding that to the set if it is. Otherwise, checking if it publicly inherits from any class that is contained in the set, and adding it to the set if it is. If the inheritance is not public, then you can issue a diagnostic. You can save a pointer to the check (`ClangTidyCheck* Check`) to do that (`Check->diag(...)`).

Thanks for the pointers! It's really helpful to have a starting point to learn the best practices, I'll try to figure this out and report back.

https://github.com/llvm/llvm-project/pull/102299


More information about the cfe-commits mailing list