[clang-tools-extra] [clang-tidy] Create bugprone-public-enable-shared-from-this check (PR #102299)
Julian Schmidt via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 12 03:29:30 PDT 2024
https://github.com/5chmidti requested changes to this pull request.
> 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(...)`).
https://github.com/llvm/llvm-project/pull/102299
More information about the cfe-commits
mailing list