[clang-tidy][NFC] Use universal memory mock for smart ptrs (#186649)

This commit is contained in:
Baranov Victor 2026-03-16 02:09:10 +03:00 committed by GitHub
parent 53a1e056f3
commit 7aeb01f01f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
26 changed files with 204 additions and 327 deletions

View File

@ -1,8 +1,178 @@
#ifndef _MEMORY_
#define _MEMORY_
#include "stddef.h"
namespace std {
template <typename T>
struct default_delete {};
template <typename T>
struct default_delete<T[]> {};
template <typename T, typename Deleter = default_delete<T>>
class unique_ptr {
public:
unique_ptr() noexcept {}
explicit unique_ptr(T *p) noexcept {}
unique_ptr(T *p, Deleter d) noexcept {}
unique_ptr(const unique_ptr &) = delete;
unique_ptr(unique_ptr &&t) noexcept {}
template <typename U, typename E>
unique_ptr(unique_ptr<U, E> &&t) noexcept {}
~unique_ptr() {}
T &operator*() const { return *ptr; }
T *operator->() const { return ptr; }
explicit operator bool() const noexcept { return ptr != nullptr; }
T *get() const { return ptr; }
T *release() { return ptr; }
void reset() {}
void reset(T *p) {}
unique_ptr &operator=(unique_ptr &) = delete;
template <typename U, typename E>
unique_ptr &operator=(unique_ptr<U, E> &) = delete;
unique_ptr &operator=(unique_ptr &&) noexcept { return *this; }
template <typename U, typename E>
unique_ptr &operator=(unique_ptr<U, E> &&) noexcept { return *this; }
bool operator==(const unique_ptr &) const noexcept { return false; }
bool operator!=(const unique_ptr &) const noexcept { return true; }
private:
T *ptr = nullptr;
};
template <typename T, typename Deleter>
class unique_ptr<T[], Deleter> {
public:
unique_ptr() noexcept {}
template <typename U>
explicit unique_ptr(U p) noexcept {}
template <typename U>
unique_ptr(U p, Deleter d) noexcept {}
~unique_ptr() {}
T &operator[](size_t i) const { return ptr[i]; }
T *get() const { return ptr; }
explicit operator bool() const noexcept { return ptr != nullptr; }
void reset() {}
void reset(T *p) {}
private:
T *ptr = nullptr;
};
template <typename T, typename... Args>
unique_ptr<T> make_unique(Args &&...args) {
return unique_ptr<T>(new T(static_cast<Args &&>(args)...));
}
template <typename T>
class shared_ptr {
public:
shared_ptr() {}
explicit shared_ptr(T *p) {}
template <typename Y>
explicit shared_ptr(Y *p) {}
template <typename Y, typename D>
shared_ptr(Y *p, D d) {}
shared_ptr(const shared_ptr &) {}
shared_ptr(shared_ptr &&) {}
~shared_ptr() {}
T &operator*() const { return *this->get(); }
T *operator->() const { return this->get(); }
T *get() const { return ptr; }
void reset() {}
void reset(T *p) {}
explicit operator bool() const noexcept { return this->get() != nullptr; }
shared_ptr &operator=(shared_ptr &&) { return *this; }
template <typename U>
shared_ptr &operator=(shared_ptr<U> &&) { return *this; }
private:
T *ptr = nullptr;
};
template <typename T>
class shared_ptr<T[]> {
public:
shared_ptr() {}
explicit shared_ptr(T *p) {}
template <typename Y>
explicit shared_ptr(Y *p) {}
template <typename Y, typename D>
shared_ptr(Y *p, D d) {}
shared_ptr(const shared_ptr &) {}
shared_ptr(shared_ptr &&) {}
~shared_ptr() {}
T &operator[](size_t i) const { return ptr[i]; }
T *get() const { return ptr; }
void reset() {}
void reset(T *p) {}
explicit operator bool() const noexcept { return ptr != nullptr; }
private:
T *ptr = nullptr;
};
template <typename T, typename... Args>
shared_ptr<T> make_shared(Args &&...args) {
return shared_ptr<T>(new T(static_cast<Args &&>(args)...));
}
template <typename T>
class weak_ptr {
public:
weak_ptr() {}
bool expired() const { return true; }
};
template <typename Y>
struct auto_ptr_ref {
Y *ptr;
};
template <typename X>
class auto_ptr {
public:
typedef X element_type;
explicit auto_ptr(X *p = 0) throw() {}
auto_ptr(auto_ptr &a) throw() {}
template <typename Y>
auto_ptr(auto_ptr<Y> &a) throw() {}
auto_ptr &operator=(auto_ptr &a) throw() { return *this; }
template <typename Y>
auto_ptr &operator=(auto_ptr<Y> &a) throw() { return *this; }
auto_ptr &operator=(auto_ptr_ref<X> r) throw() { return *this; }
~auto_ptr() throw() {}
auto_ptr(auto_ptr_ref<X> r) throw() {}
template <typename Y>
operator auto_ptr_ref<Y>() throw() {
auto_ptr_ref<Y> r;
r.ptr = ptr;
return r;
}
template <typename Y>
operator auto_ptr<Y>() throw() { return auto_ptr<Y>(ptr); }
private:
X *ptr = nullptr;
};
template <>
class auto_ptr<void> {
public:
typedef void element_type;
};
template <typename T>
class allocator {};

View File

@ -1,16 +1,6 @@
// RUN: %check_clang_tidy %s bugprone-shared-ptr-array-mismatch %t
namespace std {
template <typename T>
struct shared_ptr {
template <class Y>
explicit shared_ptr(Y *) {}
template <class Y, class Deleter>
shared_ptr(Y *, Deleter) {}
};
} // namespace std
#include <memory>
struct A {};

View File

@ -1,27 +1,10 @@
// RUN: %check_clang_tidy %s bugprone-unhandled-self-assignment %t -- -- -fno-delayed-template-parsing
#include <utility>
#include <memory>
namespace std {
template <typename T> class default_delete {};
template <class T, typename Deleter = std::default_delete<T>>
class unique_ptr {
};
template <class T>
class shared_ptr {
};
template <class T>
class weak_ptr {
};
template <class T>
class auto_ptr {
};
namespace pmr {
template <typename TYPE = void>
class allocator {};

View File

@ -1,27 +1,6 @@
// RUN: %check_clang_tidy %s bugprone-unique-ptr-array-mismatch %t
namespace std {
template<class T> struct default_delete {};
template<class T> struct default_delete<T[]> {};
template<class T, class Deleter = std::default_delete<T>>
class unique_ptr {
public:
explicit unique_ptr(T* p) noexcept;
unique_ptr(T* p, Deleter d1 ) noexcept;
};
template <class T, class Deleter>
class unique_ptr<T[], Deleter> {
public:
template<class U>
explicit unique_ptr(U p) noexcept;
template<class U>
unique_ptr(U p, Deleter d1) noexcept;
};
} // namespace std
#include <memory>
struct A {};

View File

@ -1,6 +1,7 @@
// RUN: %check_clang_tidy %s bugprone-unused-return-value %t -- \
// RUN: --config="{CheckOptions: {bugprone-unused-return-value.AllowCastToVoid: true}}" -- -fexceptions
#include <vector>
#include <memory>
namespace std {
@ -26,19 +27,6 @@ ForwardIt remove_if(ForwardIt, ForwardIt, UnaryPredicate);
template <typename ForwardIt>
ForwardIt unique(ForwardIt, ForwardIt);
template <typename T>
struct default_delete;
template <typename T, typename Deleter = std::default_delete<T>>
struct unique_ptr {
unique_ptr();
unique_ptr(unique_ptr const&);
unique_ptr(unique_ptr &&);
unique_ptr& operator=(unique_ptr const&);
unique_ptr& operator=(unique_ptr &&);
T *release() noexcept;
};
template <typename T>
struct char_traits;

View File

@ -12,38 +12,12 @@
// RUN: -fno-delayed-template-parsing
#include <utility>
#include <memory>
typedef decltype(nullptr) nullptr_t;
namespace std {
template <typename T>
struct unique_ptr {
unique_ptr();
T *get() const;
explicit operator bool() const;
void reset(T *ptr);
T &operator*() const;
T *operator->() const;
T& operator[](size_t i) const;
};
template <typename T>
struct shared_ptr {
shared_ptr();
T *get() const;
explicit operator bool() const;
void reset(T *ptr);
T &operator*() const;
T *operator->() const;
};
template <typename T>
struct weak_ptr {
weak_ptr();
bool expired() const;
};
template <typename T>
struct optional {
optional();
@ -224,7 +198,7 @@ void standardSmartPtr() {
// CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
}
{
std::unique_ptr<A> ptr;
std::unique_ptr<A[]> ptr;
std::move(ptr);
ptr[0];
// CHECK-NOTES: [[@LINE-1]]:5: warning: 'ptr' used after it was moved

View File

@ -1,11 +1,6 @@
// RUN: %check_clang_tidy %s cppcoreguidelines-avoid-const-or-ref-data-members %t
namespace std {
template <typename T>
struct unique_ptr {};
template <typename T>
struct shared_ptr {};
} // namespace std
#include <memory>
namespace gsl {
template <typename T>

View File

@ -2,21 +2,7 @@
// CHECK-FIXES: #include <utility>
namespace std {
template <typename T>
struct default_delete {};
template <typename T, class Deleter = std::default_delete<T>>
struct unique_ptr {
unique_ptr();
explicit unique_ptr(T *);
template <typename U, typename E>
unique_ptr(unique_ptr<U, E> &&);
void reset(T *);
T *release();
};
} // namespace std
#include <memory>
struct Foo {};
struct Bar : Foo {};

View File

@ -1,45 +0,0 @@
#ifndef INPUTS_MEMORY_H
#define INPUTS_MEMORY_H
namespace std {
inline namespace _1 {
template <class Y> struct auto_ptr_ref {
Y *y_;
};
template <class X> class auto_ptr {
public:
typedef X element_type;
explicit auto_ptr(X *p = 0) throw() {}
auto_ptr(auto_ptr &) throw() {}
template <class Y> auto_ptr(auto_ptr<Y> &) throw() {}
auto_ptr &operator=(auto_ptr &) throw() { return *this; }
template <class Y> auto_ptr &operator=(auto_ptr<Y> &) throw() {
return *this;
}
auto_ptr &operator=(auto_ptr_ref<X> r) throw() { return *this; }
~auto_ptr() throw() {}
auto_ptr(auto_ptr_ref<X> r) throw() : x_(r.y_) {}
template <class Y> operator auto_ptr_ref<Y>() throw() {
auto_ptr_ref<Y> r;
r.y_ = x_;
return r;
}
template <class Y> operator auto_ptr<Y>() throw() { return auto_ptr<Y>(x_); }
private:
X *x_;
};
template <> class auto_ptr<void> {
public:
typedef void element_type;
};
} // namespace _1
} // end namespace std
#endif // INPUTS_MEMORY_H

View File

@ -1,33 +1 @@
namespace std {
template <typename type>
class __shared_ptr {
protected:
__shared_ptr();
__shared_ptr(type *ptr);
~__shared_ptr();
public:
type &operator*() { return *ptr; }
type *operator->() { return ptr; }
type *release();
void reset();
void reset(type *pt);
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
#include <memory>

View File

@ -1,28 +1 @@
namespace std {
template <typename T>
class default_delete {};
template <typename type, typename Deleter = std::default_delete<type>>
class unique_ptr {
public:
unique_ptr() {}
unique_ptr(type *ptr) {}
unique_ptr(const unique_ptr<type> &t) = delete;
unique_ptr(unique_ptr<type> &&t) {}
~unique_ptr() {}
type &operator*() { return *ptr; }
type *operator->() { return ptr; }
type *release() { return ptr; }
void reset() {}
void reset(type *pt) {}
void reset(type pt) {}
unique_ptr &operator=(unique_ptr &&) { return *this; }
template <typename T>
unique_ptr &operator=(unique_ptr<T> &&) { return *this; }
private:
type *ptr;
};
} // namespace std
#include <memory>

View File

@ -2,10 +2,9 @@
// RUN: -config="{CheckOptions: \
// RUN: {modernize-make-shared.MakeSmartPtrFunction: 'my::MakeShared', \
// RUN: modernize-make-shared.MakeSmartPtrFunctionHeader: 'make_shared_util.h' \
// RUN: }}" \
// RUN: -- -I %S/Inputs/smart-ptr
// RUN: }}"
#include "shared_ptr.h"
#include <memory>
// CHECK-FIXES: #include "make_shared_util.h"
void f() {

View File

@ -1,7 +1,7 @@
// RUN: %check_clang_tidy -std=c++11 %s modernize-make-unique %t -- -- -I %S/Inputs/smart-ptr
// RUN: %check_clang_tidy -std=c++11 %s modernize-make-unique %t
#include "unique_ptr.h"
// CHECK-FIXES: #include "unique_ptr.h"
#include <memory>
// CHECK-FIXES: #include <memory>
void f() {
auto my_ptr = std::unique_ptr<int>(new int(1));

View File

@ -2,12 +2,10 @@
// RUN: -config="{CheckOptions: \
// RUN: {modernize-make-unique.IgnoreDefaultInitialization: \
// RUN: 'false'}} \
// RUN: }" \
// RUN: -- -I %S/Inputs/smart-ptr
// RUN: }"
#include "unique_ptr.h"
#include <memory>
#include <vector>
// CHECK-FIXES: #include <memory>
void basic() {
std::unique_ptr<int> P1 = std::unique_ptr<int>(new int());

View File

@ -2,10 +2,9 @@
// RUN: -config="{CheckOptions: \
// RUN: {modernize-make-unique.MakeSmartPtrFunction: 'my::MakeUnique', \
// RUN: modernize-make-unique.MakeSmartPtrFunctionHeader: 'make_unique_util.h' \
// RUN: }}" \
// RUN: -- -I %S/Inputs/smart-ptr
// RUN: }}"
#include "unique_ptr.h"
#include <memory>
// CHECK-FIXES: #include "make_unique_util.h"
void f() {

View File

@ -2,7 +2,7 @@
// RUN: -config="{CheckOptions: {modernize-make-unique.IgnoreMacros: false}}" \
// RUN: -- -I %S/Inputs/smart-ptr
#include "unique_ptr.h"
#include <memory>
class Foo {};
class Bar {};

View File

@ -1,8 +1,8 @@
// RUN: %check_clang_tidy %s modernize-replace-auto-ptr %t -- -- -isystem %S/Inputs/replace-auto-ptr
// RUN: %check_clang_tidy %s modernize-replace-auto-ptr %t
// CHECK-FIXES: #include <utility>
#include "memory.h"
#include <memory>
// Instrumentation for auto_ptr_ref test.
struct Base {};

View File

@ -8,6 +8,7 @@
// RUN: '::std::make_pair; ::std::make_tuple; ::test::MakeSingle'}}"
#include <utility>
#include <memory>
namespace std {
template <typename E>
@ -313,12 +314,6 @@ tuple<typename remove_reference<Ts>::type...> make_tuple(Ts &&...) {
return {};
}
template <typename T>
class unique_ptr {
public:
explicit unique_ptr(T *) {}
~unique_ptr();
};
} // namespace std
namespace llvm {

View File

@ -6,7 +6,7 @@
// CHECK-FIXES: #include <ranges>
#include "use-ranges/fake_std.h"
#include "smart-ptr/unique_ptr.h"
#include <memory>
void Positives() {
std::vector<int> I, J;

View File

@ -1,10 +1,9 @@
// RUN: %check_clang_tidy %s readability-ambiguous-smartptr-reset-call %t -- \
// RUN: -config='{CheckOptions: \
// RUN: {readability-ambiguous-smartptr-reset-call.SmartPointers: "::std::unique_ptr;::other_ptr"}}' \
// RUN: --fix-notes -- -I %S/../modernize/Inputs/smart-ptr
// RUN: --fix-notes
#include "unique_ptr.h"
#include "shared_ptr.h"
#include <memory>
template <typename T>
struct other_ptr {

View File

@ -1,7 +1,6 @@
// RUN: %check_clang_tidy %s readability-ambiguous-smartptr-reset-call %t --fix-notes -- -I %S/../modernize/Inputs/smart-ptr
// RUN: %check_clang_tidy %s readability-ambiguous-smartptr-reset-call %t --fix-notes
#include "unique_ptr.h"
#include "shared_ptr.h"
#include <memory>
template <typename T>
struct non_default_reset_ptr {

View File

@ -4,14 +4,7 @@
#include <string>
#include <type_traits>
#include <vector>
namespace std {
template <typename T>
struct unique_ptr {
T &operator*() const;
T *operator->() const;
};
}
#include <memory>
template <typename T>
void f(const T *);

View File

@ -2,6 +2,7 @@
// RUN: -config="{CheckOptions: {readability-container-size-empty.ExcludedComparisonTypes: '::std::array;::IgnoredDummyType'}}" \
// RUN: -- -fno-delayed-template-parsing
#include <string>
#include <memory>
namespace std {
template <typename T> struct vector {
@ -682,14 +683,6 @@ void instantiator() {
instantiatedTemplateWithSizeCall<std::vector<int>>();
}
namespace std {
template <typename T>
struct unique_ptr {
T *operator->() const;
T &operator*() const;
};
} // namespace std
bool call_through_unique_ptr(const std::unique_ptr<std::vector<int>> &ptr) {
return ptr->size() > 0;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used

View File

@ -1,17 +1,7 @@
// RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t -- \
// RUN: -config="{CheckOptions: {readability-redundant-smartptr-get.IgnoreMacros: false}}"
namespace std {
template <typename T>
struct shared_ptr {
T &operator*() const;
T *operator->() const;
T *get() const;
explicit operator bool() const noexcept;
};
} // namespace std
#include <memory>
#define MACRO(p) p.get()

View File

@ -1,42 +1,9 @@
// RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t
#include <vector>
#include <memory>
#define NULL __null
namespace std {
template <typename T>
struct unique_ptr {
T& operator*() const;
T* operator->() const;
T* get() const;
explicit operator bool() const noexcept;
};
template <typename T>
struct unique_ptr<T[]> {
T& operator[](unsigned) const;
T* get() const;
explicit operator bool() const noexcept;
};
template <typename T>
struct shared_ptr {
T& operator*() const;
T* operator->() const;
T* get() const;
explicit operator bool() const noexcept;
};
template <typename T>
struct shared_ptr<T[]> {
T& operator[](unsigned) const;
T* get() const;
explicit operator bool() const noexcept;
};
} // namespace std
struct Bar {
void Do();
void ConstDo() const;

View File

@ -1,24 +1,8 @@
// RUN: %check_clang_tidy %s readability-uniqueptr-delete-release %t -check-suffix=NULLPTR
// RUN: %check_clang_tidy %s readability-uniqueptr-delete-release %t -check-suffix=RESET -config='{ \
// RUN: CheckOptions: {readability-uniqueptr-delete-release.PreferResetCall: true}}'
namespace std {
template <typename T>
struct default_delete {};
template <typename T, typename D = default_delete<T>>
class unique_ptr {
public:
unique_ptr();
~unique_ptr();
explicit unique_ptr(T*);
template <typename U, typename E>
unique_ptr(unique_ptr<U, E>&&);
T* release();
void reset(T *P = nullptr);
T &operator*() const;
T *operator->() const;
};
} // namespace std
#include <memory>
std::unique_ptr<int>& ReturnsAUnique();
@ -30,7 +14,7 @@ void Positives() {
// CHECK-FIXES-NULLPTR: P = nullptr;
// CHECK-FIXES-RESET: P.reset();
auto P2 = P;
auto &P2 = P;
delete P2.release();
// CHECK-MESSAGES-NULLPTR: :[[@LINE-1]]:3: warning: prefer '= nullptr' to reset 'unique_ptr<>' objects
// CHECK-MESSAGES-RESET: :[[@LINE-2]]:3: warning: prefer 'reset()' to reset 'unique_ptr<>' objects