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

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 8 07:07:50 PDT 2024


================
@@ -0,0 +1,96 @@
+//===--- 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/DeclCXX.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void IncorrectEnableSharedFromThisCheck::registerMatchers(
+    MatchFinder *match_finder) {
+  match_finder->addMatcher(
+      cxxRecordDecl(
+          unless(isExpansionInSystemHeader()),
+          hasAnyBase(cxxBaseSpecifier(unless(isPublic()),
+                                      hasType(cxxRecordDecl(hasName(
+                                          "::std::enable_shared_from_this"))))))
+          .bind("class-base-std-enable"),
+      this);
+  match_finder->addMatcher(
+      cxxRecordDecl(unless(isExpansionInSystemHeader()),
+                    hasAnyBase(cxxBaseSpecifier(hasType(
+                        cxxRecordDecl(hasName("enable_shared_from_this"))))))
+          .bind("class-missing-std"),
+      this);
+}
+
+void IncorrectEnableSharedFromThisCheck::check(
+    const MatchFinder::MatchResult &result) {
+  const auto *StdEnableSharedClassDecl =
+      result.Nodes.getNodeAs<CXXRecordDecl>("class-base-std-enable");
+  const auto *MissingStdSharedClassDecl =
+      result.Nodes.getNodeAs<CXXRecordDecl>("class-missing-std");
+
+  if (StdEnableSharedClassDecl) {
+    for (const CXXBaseSpecifier &Base : StdEnableSharedClassDecl->bases()) {
+      const CXXRecordDecl *BaseType = Base.getType()->getAsCXXRecordDecl();
+      if (BaseType && BaseType->getQualifiedNameAsString() ==
+                          "std::enable_shared_from_this") {
+        SourceRange ReplacementRange = Base.getSourceRange();
+        std::string ReplacementString =
----------------
EugeneZelenko wrote:

```suggestion
        const std::string ReplacementString =
```

Will be good idea to run `misc-const-correctness` to find other variables that should be `const`.

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


More information about the cfe-commits mailing list