-
Notifications
You must be signed in to change notification settings - Fork 12.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-tidy] modernize-make-shared: Add MakeSmartPtrType option #117529
base: main
Are you sure you want to change the base?
Conversation
…type matching 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.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-tidy Author: Helmut Januschka (hjanuschka) ChangesIntroduced a WIP, TODO:
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:
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
|
@llvm/pr-subscribers-clang-tools-extra Author: Helmut Januschka (hjanuschka) ChangesIntroduced a WIP, TODO:
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:
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
|
clang-tools-extra/test/clang-tidy/checkers/modernize/make-shared-ptr-name.cpp
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You modify also check related to make_unique.
Do:
- Add MakeSmartPtrType in MakeSmartPtrCheck as protected (do not initialize in constructor)
- Add MakeSmartPtrType to storeOptions
- Read MakeSmartPtrType in MakeSharedPtrCheck & MakeUniquePtrCheck constructors (set different default values)
- Use MakeSmartPtrType in getSmartPointerTypeMatcher in both classes
- Modify documentation for both checks
- Add entry for both checks in release notes
- Consider doing same for 'std::make_shared'
- Consider supporting multiple smart ptr types and multiple make functions
With current implementation you may run into issue where when setting custom smart ptr, you won't have anymore support for shared_ptr.
Other option would be to add "make-custom" check, and leave it unconfigured.
You may wait until others comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add MakeSmartPtrType in MakeSmartPtrCheck as protected (do not initialize in constructor)
- Add MakeSmartPtrType to storeOptions
- Read MakeSmartPtrType in MakeSharedPtrCheck & MakeUniquePtrCheck constructors (set different default values)
- Use MakeSmartPtrType in getSmartPointerTypeMatcher in both classes
- Modify documentation for both checks
- Add entry for both checks in release notes
- Consider doing same for 'std::make_shared'
Consider supporting multiple smart ptr types and multiple make functions
a bit unsure about this one, do you have any sample where there is usage of multiple?!
really not sure about this! the rest is addressed, and ready for another review
Co-authored-by: EugeneZelenko <[email protected]>
Co-authored-by: EugeneZelenko <[email protected]>
Co-authored-by: EugeneZelenko <[email protected]>
Co-authored-by: EugeneZelenko <[email protected]>
Co-authored-by: EugeneZelenko <[email protected]>
Co-authored-by: EugeneZelenko <[email protected]>
Co-authored-by: EugeneZelenko <[email protected]>
Co-authored-by: EugeneZelenko <[email protected]>
You can test this locally with the following command:git-clang-format --diff 3cecf17065919da0a7fa9b38f37592e5462c2f85 4b7099bbe3d951241986b038c2492aba3c463dc3 --extensions h,cpp -- clang-tools-extra/test/clang-tidy/checkers/modernize/make-shared-ptr-name.cpp clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.cpp clang-tools-extra/clang-tidy/modernize/MakeSharedCheck.h clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h clang-tools-extra/clang-tidy/modernize/MakeUniqueCheck.cpp View the diff from clang-format here.diff --git a/clang-tools-extra/clang-tidy/modernize/MakeUniqueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeUniqueCheck.cpp
index 7e57b46241..160bc3cef3 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeUniqueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MakeUniqueCheck.cpp
@@ -14,9 +14,7 @@ namespace clang::tidy::modernize {
MakeUniqueCheck::MakeUniqueCheck(StringRef Name,
clang::tidy::ClangTidyContext *Context)
- : MakeSmartPtrCheck(Name, Context,
- "std::make_unique",
- "::std::unique_ptr"),
+ : MakeSmartPtrCheck(Name, Context, "std::make_unique", "::std::unique_ptr"),
RequireCPlusPlus14(Options.get("MakeSmartPtrFunction", "").empty()) {}
MakeUniqueCheck::SmartPtrTypeMatcher
|
hey 👋,
here is a PR, that adds a new configuration option
MakeSmartPtrType
to the modernize-make-shared check, allowing customization of the smart pointer type being checked. This enables the check to work with custom smart pointer implementations that follow theshared_ptr
pattern, likebase::scoped_refptr
in chromium.happy to address any given feedback or improvements of any kind.