reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
// RUN: %check_clang_tidy %s modernize-replace-auto-ptr %t -- -- -I %S/Inputs/modernize-replace-auto-ptr

// CHECK-FIXES: #include <utility>

#include "memory.h"

// Instrumentation for auto_ptr_ref test.
struct Base {};
struct Derived : Base {};
std::auto_ptr<Derived> create_derived_ptr();
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: auto_ptr is deprecated, use unique_ptr instead [modernize-replace-auto-ptr]
// CHECK-FIXES: std::unique_ptr<Derived> create_derived_ptr();


// Test function return values (declaration)
std::auto_ptr<char> f_5();
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: auto_ptr is deprecated
// CHECK-FIXES: std::unique_ptr<char> f_5()


// Test function parameters.
void f_6(std::auto_ptr<int>);
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: auto_ptr is deprecated
// CHECK-FIXES: void f_6(std::unique_ptr<int>);
void f_7(const std::auto_ptr<int> &);
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: auto_ptr is deprecated
// CHECK-FIXES: void f_7(const std::unique_ptr<int> &);


// Test on record type fields.
struct A {
  std::auto_ptr<int> field;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<int> field;

  typedef std::auto_ptr<int> int_ptr_type;
  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: auto_ptr is deprecated
  // CHECK-FIXES: typedef std::unique_ptr<int> int_ptr_type;
};


// FIXME: Test template WITH instantiation.
template <typename T> struct B {
  typedef typename std::auto_ptr<T> created_type;
  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: auto_ptr is deprecated
  // CHECK-FIXES: typedef typename std::unique_ptr<T> created_type;

  created_type create() { return std::auto_ptr<T>(new T()); }
  // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: auto_ptr is deprecated
  // CHECK-FIXES: created_type create() { return std::unique_ptr<T>(new T()); }
};


// Test 'using' in a namespace (declaration)
namespace ns_1 {
// Test multiple using declarations.
  using std::auto_ptr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: auto_ptr is deprecated
  // CHECK-FIXES: using std::unique_ptr;
  using std::auto_ptr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: auto_ptr is deprecated
  // CHECK-FIXES: using std::unique_ptr;
}


namespace ns_2 {
template <typename T> struct auto_ptr {};
}

void f_1() {
  std::auto_ptr<int> a;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<int> a;

  // Check that spaces aren't modified unnecessarily.
  std:: auto_ptr <int> b;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: auto_ptr is deprecated
  // CHECK-FIXES: std:: unique_ptr <int> b;
  std :: auto_ptr < char > c(new char());
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: auto_ptr is deprecated
  // CHECK-FIXES: std :: unique_ptr < char > c(new char());

  // Test construction from a temporary.
  std::auto_ptr<char> d = std::auto_ptr<char>();
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-MESSAGES: :[[@LINE-2]]:32: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<char> d = std::unique_ptr<char>();

  typedef std::auto_ptr<int> int_ptr_t;
  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: auto_ptr is deprecated
  // CHECK-FIXES: typedef std::unique_ptr<int> int_ptr_t;
  int_ptr_t e(new int());

  // Test pointers.
  std::auto_ptr<int> *f;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<int> *f;

  // Test 'static' declarations.
  static std::auto_ptr<int> g;
  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: auto_ptr is deprecated
  // CHECK-FIXES: static std::unique_ptr<int> g;

  // Test with cv-qualifiers.
  const std::auto_ptr<int> h;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: auto_ptr is deprecated
  // CHECK-FIXES: const std::unique_ptr<int> h;
  volatile std::auto_ptr<int> i;
  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: auto_ptr is deprecated
  // CHECK-FIXES: volatile std::unique_ptr<int> i;
  const volatile std::auto_ptr<int> j;
  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: auto_ptr is deprecated
  // CHECK-FIXES: const volatile std::unique_ptr<int> j;

  // Test auto and initializer-list.
  auto k = std::auto_ptr<int>{};
  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: auto_ptr is deprecated
  // CHECK-FIXES: auto k = std::unique_ptr<int>{};
  std::auto_ptr<int> l{std::auto_ptr<int>()};
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-MESSAGES: :[[@LINE-2]]:29: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<int> l{std::unique_ptr<int>()};

  // Test interlocked auto_ptr.
  std::auto_ptr<std::auto_ptr<int> > m;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<std::unique_ptr<int> > m;

  // Test temporaries.
  std::auto_ptr<char>();
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<char>();

  // Test void-specialization.
  std::auto_ptr<void> n;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<void> n;

  // Test template WITH instantiation (instantiation).
  B<double> o;
  std::auto_ptr<double> p(o.create());
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<double> p(o.create());

  // Test 'using' in a namespace ("definition").
  ns_1::auto_ptr<int> q;
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: auto_ptr is deprecated
  // CHECK-FIXES: ns_1::unique_ptr<int> q;

  // Test construction with an 'auto_ptr_ref'.
  std::auto_ptr<Base> r(create_derived_ptr());
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<Base> r(create_derived_ptr());
}

// Test without the nested name specifiers.
void f_2() {
  using namespace std;

  auto_ptr<int> a;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: auto_ptr is deprecated
  // CHECK-FIXES: unique_ptr<int> a;
}

// Test using declaration.
void f_3() {
  using std::auto_ptr;
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: auto_ptr is deprecated
  // CHECK-FIXES: using std::unique_ptr;

  auto_ptr<int> a;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: auto_ptr is deprecated
  // CHECK-FIXES: unique_ptr<int> a;
}

// Test messing-up with macros.
void f_4() {
#define MACRO_1 <char>
  std::auto_ptr MACRO_1 p(new char());
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr MACRO_1 p(new char());
#define MACRO_2 auto_ptr
  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: auto_ptr is deprecated
  // CHECK-FIXES: #define MACRO_2 unique_ptr
  std::MACRO_2<int> q;
#define MACRO_3(Type) std::auto_ptr<Type>
  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: auto_ptr is deprecated
  // CHECK-FIXES: #define MACRO_3(Type) std::unique_ptr<Type>
  MACRO_3(float)r(new float());
#define MACRO_4 std::auto_ptr
  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: auto_ptr is deprecated
  // CHECK-FIXES: #define MACRO_4 std::unique_ptr
  using MACRO_4;
#undef MACRO_1
#undef MACRO_2
#undef MACRO_3
#undef MACRO_4
}

// Test function return values (definition).
std::auto_ptr<char> f_5()
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<char> f_5()
{
  // Test constructor.
  return std::auto_ptr<char>(new char());
  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: auto_ptr is deprecated
  // CHECK-FIXES: return std::unique_ptr<char>(new char());
}

// Test that non-std auto_ptr aren't replaced.
void f_8() {
  ns_2::auto_ptr<char> a;
  using namespace ns_2;
  auto_ptr<int> b;
}

// Fail to modify when the template is never instantiated.
//
// This might not be an issue. If it's never used it doesn't really matter if
// it's changed or not. If it's a header and one of the source use it, then it
// will still be changed.
template <typename X>
void f() {
  std::auto_ptr<X> p;
}

// FIXME: Alias template could be replaced if a matcher existed.
namespace std {
template <typename T> using aaaaaaaa = auto_ptr<T>;
}

// We want to avoid replacing 'aaaaaaaa' by unique_ptr here. It's better to
// change the type alias directly.
std::aaaaaaaa<int> d;


void takes_ownership_fn(std::auto_ptr<int> x);
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: auto_ptr is deprecated
// CHECK-FIXES: void takes_ownership_fn(std::unique_ptr<int> x);

std::auto_ptr<int> get_by_value();
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: auto_ptr is deprecated
// CHECK-FIXES: std::unique_ptr<int> get_by_value();

class Wrapper {
 public:
  std::auto_ptr<int> &get_wrapped();
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated

 private:
  std::auto_ptr<int> wrapped;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
};

void f() {
  std::auto_ptr<int> a, b, c;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<int> a, b, c;
  Wrapper wrapper_a, wrapper_b;

  a = b;
  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::move to transfer ownership
  // CHECK-FIXES: a = std::move(b);

  wrapper_a.get_wrapped() = wrapper_b.get_wrapped();
  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::move to transfer ownership
  // CHECK-FIXES: wrapper_a.get_wrapped() = std::move(wrapper_b.get_wrapped());

  // Test that 'std::move()' is inserted when call to the
  // copy-constructor are made.
  takes_ownership_fn(c);
  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::move to transfer ownership
  // CHECK-FIXES: takes_ownership_fn(std::move(c));
  takes_ownership_fn(wrapper_a.get_wrapped());
  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::move to transfer ownership
  // CHECK-FIXES: takes_ownership_fn(std::move(wrapper_a.get_wrapped()));

  std::auto_ptr<int> d[] = { std::auto_ptr<int>(new int(1)),
                             std::auto_ptr<int>(new int(2)) };
  // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: auto_ptr is deprecated
  // CHECK-MESSAGES: :[[@LINE-3]]:35: warning: auto_ptr is deprecated
  // CHECK-MESSAGES: :[[@LINE-3]]:35: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<int> d[] = { std::unique_ptr<int>(new int(1)),
  // CHECK-FIXES-NEXT:                         std::unique_ptr<int>(new int(2)) };
  std::auto_ptr<int> e = d[0];
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-MESSAGES: :[[@LINE-2]]:26: warning: use std::move to transfer ownership
  // CHECK: std::unique_ptr<int> e = std::move(d[0]);

  // Test that std::move() is not used when assigning an rvalue
  std::auto_ptr<int> f;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<int> f;
  f = std::auto_ptr<int>(new int(0));
  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: auto_ptr is deprecated
  // CHECK-NEXT: f = std::unique_ptr<int>(new int(0));

  std::auto_ptr<int> g = get_by_value();
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated
  // CHECK-FIXES: std::unique_ptr<int> g = get_by_value();
}