Skip to content

Commit

Permalink
[clang-tidy] Improve modernize-make-shared check (#70600)
Browse files Browse the repository at this point in the history
Improved modernize-make-shared check to support
std::shared_ptr implementations that inherit the
reset method from a base class.
In GCC that class is called __shared_ptr.

Fixes #64481
  • Loading branch information
PiotrZSL committed Nov 3, 2023
1 parent de88371 commit 51018d1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
20 changes: 12 additions & 8 deletions clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,18 @@ void MakeSmartPtrCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
this);

Finder->addMatcher(
traverse(TK_AsIs,
cxxMemberCallExpr(
thisPointerType(getSmartPointerTypeMatcher()),
callee(cxxMethodDecl(hasName("reset"))),
hasArgument(0, cxxNewExpr(CanCallCtor, unless(IsPlacement))
.bind(NewExpression)),
unless(isInTemplateInstantiation()))
.bind(ResetCall)),
traverse(
TK_AsIs,
cxxMemberCallExpr(
unless(isInTemplateInstantiation()),
hasArgument(0, cxxNewExpr(CanCallCtor, unless(IsPlacement))
.bind(NewExpression)),
callee(cxxMethodDecl(hasName("reset"))),
anyOf(thisPointerType(getSmartPointerTypeMatcher()),
on(ignoringImplicit(anyOf(
hasType(getSmartPointerTypeMatcher()),
hasType(pointsTo(getSmartPointerTypeMatcher())))))))
.bind(ResetCall)),
this);
}

Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ Changes in existing checks
iterators initialized by free functions like ``begin``, ``end``, or ``size``
and avoid crash for array of dependent array.

- Improved :doc:`modernize-make-shared
<clang-tidy/checks/modernize/make-shared>` check to support
``std::shared_ptr`` implementations that inherit the ``reset`` method from a
base class.

- Improved :doc:`modernize-return-braced-init-list
<clang-tidy/checks/modernize/return-braced-init-list>` check to ignore
false-positives when constructing the container with ``count`` copies of
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
namespace std {

template <typename type>
class shared_ptr {
class __shared_ptr {
protected:
__shared_ptr();
__shared_ptr(type *ptr);
~__shared_ptr();
public:
shared_ptr();
shared_ptr(type *ptr);
shared_ptr(const shared_ptr<type> &t) {}
shared_ptr(shared_ptr<type> &&t) {}
~shared_ptr();
type &operator*() { return *ptr; }
type *operator->() { return ptr; }
type *release();
void reset();
void reset(type *pt);
shared_ptr &operator=(shared_ptr &&);
template <typename T>
shared_ptr &operator=(shared_ptr<T> &&);

private:
type *ptr;
};

template <typename type>
class shared_ptr : public __shared_ptr<type> {
public:
shared_ptr();
shared_ptr(type *ptr);
shared_ptr(const shared_ptr<type> &t);
shared_ptr(shared_ptr<type> &&t);
~shared_ptr();
shared_ptr &operator=(shared_ptr &&);
template <typename T>
shared_ptr &operator=(shared_ptr<T> &&);
};

} // namespace std

0 comments on commit 51018d1

Please sign in to comment.