[clang-tools-extra] WIP: [clang-tidy] Add SmartPtrName to MakeSmartPtrCheck for flexible … (PR #117529)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 25 01:01:47 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Helmut Januschka (hjanuschka)

<details>
<summary>Changes</summary>

Introduced a `SmartPtrName` field in `MakeSmartPtrCheck` to allow matching on other smart pointer types, such as `base::scoped_refptr`, in addition to `std::shared_ptr`. This enables more versatile usage of the check without duplicating matcher logic.


WIP, TODO:
  - add test case.



```
#include <iostream>

namespace base {
template <typename T>
class scoped_refptr {
 public:
  explicit scoped_refptr(T* p) : ptr_(p) {}

  void reset(T* p) {
    delete ptr_;
    ptr_ = p;
  }

  // For demonstration purposes
  T* get() const { return ptr_; }

  ~scoped_refptr() {
    delete ptr_;
  }

 private:
  T* ptr_;  // Raw pointer to the managed object
};

// Mock MakeRefCounted function
template <typename T, typename... Args>
scoped_refptr<T> MakeRefCounted(Args&&... args) {
  return scoped_refptr<T>(new T(std::forward<Args>(args)...));
}

class Foo {
 public:
  Foo() { std::cout << "Foo constructed.\n"; }
  ~Foo() { std::cout << "Foo destroyed.\n"; }
};

}  // namespace base

// Test case
int main() {
  auto ptr = base::MakeRefCounted<base::Foo>();  // Create a scoped_refptr
  ptr.reset(new base::Foo());  // Reset with a new instance
  return 0;
}

```

works, but need to figure out why turning this into a llvm-lit does not work

---
Full diff: https://github.com/llvm/llvm-project/pull/117529.diff


5 Files Affected:

- (modified) clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.cpp (+3-2) 
- (modified) clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.h (+3) 
- (modified) clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp (+3-3) 
- (modified) clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h (+1) 
- (modified) clang-tools-extra/docs/clang-tidy/checks/modernize/make-shared.rst (+6) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.cpp
index 69f7d9f69eeed0..34009046fec6ae 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.cpp
@@ -16,13 +16,14 @@ using namespace clang::ast_matchers;
 namespace clang::tidy::modernize {
 
 MakeSharedCheck::MakeSharedCheck(StringRef Name, ClangTidyContext *Context)
-    : MakeSmartPtrCheck(Name, Context, "std::make_shared") {}
+    : MakeSmartPtrCheck(Name, Context, "std::make_shared"),
+      MakeSmartPtrType(Options.get("MakeSmartPtrType", "::std::shared_ptr")) {}
 
 MakeSharedCheck::SmartPtrTypeMatcher
 MakeSharedCheck::getSmartPointerTypeMatcher() const {
   return qualType(hasUnqualifiedDesugaredType(
       recordType(hasDeclaration(classTemplateSpecializationDecl(
-          hasName("::std::shared_ptr"), templateArgumentCountIs(1),
+          hasName(MakeSmartPtrType), templateArgumentCountIs(1),
           hasTemplateArgument(0, templateArgument(refersToType(
                                      qualType().bind(PointerType)))))))));
 }
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.h b/clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.h
index caaf4ae403c34f..932796e3a147f1 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.h
@@ -26,6 +26,9 @@ namespace clang::tidy::modernize {
 /// For the user-facing documentation see:
 /// http://clang.llvm.org/extra/clang-tidy/checks/modernize/make-shared.html
 class MakeSharedCheck : public MakeSmartPtrCheck {
+private:
+  const StringRef MakeSmartPtrType;
+
 public:
   MakeSharedCheck(StringRef Name, ClangTidyContext *Context);
 
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
index d1d7e9dcfa9c0d..3f77e6727d19f8 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -46,6 +46,7 @@ MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
                areDiagsSelfContained()),
       MakeSmartPtrFunctionHeader(
           Options.get("MakeSmartPtrFunctionHeader", "<memory>")),
+      MakeSmartPtrType(Options.get("MakeSmartPtrType", "::std::shared_ptr")),
       MakeSmartPtrFunctionName(
           Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)),
       IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)),
@@ -55,6 +56,7 @@ MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
 void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "IncludeStyle", Inserter.getStyle());
   Options.store(Opts, "MakeSmartPtrFunctionHeader", MakeSmartPtrFunctionHeader);
+  Options.store(Opts, "MakeSmartPtrType", MakeSmartPtrType);
   Options.store(Opts, "MakeSmartPtrFunction", MakeSmartPtrFunctionName);
   Options.store(Opts, "IgnoreMacros", IgnoreMacros);
   Options.store(Opts, "IgnoreDefaultInitialization",
@@ -115,7 +117,6 @@ void MakeSmartPtrCheck::check(const MatchFinder::MatchResult &Result) {
   // 'smart_ptr' refers to 'std::shared_ptr' or 'std::unique_ptr' or other
   // pointer, 'make_smart_ptr' refers to 'std::make_shared' or
   // 'std::make_unique' or other function that creates smart_ptr.
-
   SourceManager &SM = *Result.SourceManager;
   const auto *Construct =
       Result.Nodes.getNodeAs<CXXConstructExpr>(ConstructorCall);
@@ -361,8 +362,7 @@ bool MakeSmartPtrCheck::replaceNew(DiagnosticBuilder &Diag,
       Diag << FixItHint::CreateRemoval(
           SourceRange(NewStart, InitRange.getBegin()));
       Diag << FixItHint::CreateRemoval(SourceRange(InitRange.getEnd(), NewEnd));
-    }
-    else {
+    } else {
       // New array expression with default/value initialization:
       //   smart_ptr<Foo[]>(new int[5]());
       //   smart_ptr<Foo[]>(new Foo[5]());
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
index 02374dc06d9be5..c4b407e5090656 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
@@ -46,6 +46,7 @@ class MakeSmartPtrCheck : public ClangTidyCheck {
 private:
   utils::IncludeInserter Inserter;
   const StringRef MakeSmartPtrFunctionHeader;
+  const StringRef MakeSmartPtrType;
   const StringRef MakeSmartPtrFunctionName;
   const bool IgnoreMacros;
   const bool IgnoreDefaultInitialization;
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/make-shared.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/make-shared.rst
index 9c1fceaa060002..31d3de7a7893f3 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize/make-shared.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/make-shared.rst
@@ -39,6 +39,12 @@ Options
    A string specifying the corresponding header of make-shared-ptr function.
    Default is `memory`.
 
+
+.. option:: MakeSmartPtrType
+
+   A string specifying the corresponding pointer type.
+   Default is `::std::shared_ptr`.
+
 .. option:: IncludeStyle
 
    A string specifying which include-style is used, `llvm` or `google`. Default

``````````

</details>


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


More information about the cfe-commits mailing list