brintos

brintos / llvm-project-archived public Read only

0
0
Text · 10.8 KiB · 371f3dd Raw
304 lines · cpp
1// RUN: %check_clang_tidy %s modernize-replace-auto-ptr %t -- -- -isystem %S/Inputs/replace-auto-ptr2 3// CHECK-FIXES: #include <utility>4 5#include "memory.h"6 7// Instrumentation for auto_ptr_ref test.8struct Base {};9struct Derived : Base {};10std::auto_ptr<Derived> create_derived_ptr();11// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: auto_ptr is deprecated, use unique_ptr instead [modernize-replace-auto-ptr]12// CHECK-FIXES: std::unique_ptr<Derived> create_derived_ptr();13 14 15// Test function return values (declaration)16std::auto_ptr<char> f_5();17// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: auto_ptr is deprecated18// CHECK-FIXES: std::unique_ptr<char> f_5();19 20 21// Test function parameters.22void f_6(std::auto_ptr<int>);23// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: auto_ptr is deprecated24// CHECK-FIXES: void f_6(std::unique_ptr<int>);25void f_7(const std::auto_ptr<int> &);26// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: auto_ptr is deprecated27// CHECK-FIXES: void f_7(const std::unique_ptr<int> &);28 29 30// Test on record type fields.31struct A {32  std::auto_ptr<int> field;33  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated34  // CHECK-FIXES: std::unique_ptr<int> field;35 36  typedef std::auto_ptr<int> int_ptr_type;37  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: auto_ptr is deprecated38  // CHECK-FIXES: typedef std::unique_ptr<int> int_ptr_type;39};40 41 42// FIXME: Test template WITH instantiation.43template <typename T> struct B {44  typedef typename std::auto_ptr<T> created_type;45  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: auto_ptr is deprecated46  // CHECK-FIXES: typedef typename std::unique_ptr<T> created_type;47 48  created_type create() { return std::auto_ptr<T>(new T()); }49  // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: auto_ptr is deprecated50  // CHECK-FIXES: created_type create() { return std::unique_ptr<T>(new T()); }51};52 53 54// Test 'using' in a namespace (declaration)55namespace ns_1 {56// Test multiple using declarations.57  using std::auto_ptr;58  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: auto_ptr is deprecated59  // CHECK-FIXES: using std::unique_ptr;60  using std::auto_ptr;61  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: auto_ptr is deprecated62  // CHECK-FIXES: using std::unique_ptr;63}64 65 66namespace ns_2 {67template <typename T> struct auto_ptr {};68}69 70void f_1() {71  std::auto_ptr<int> a;72  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated73  // CHECK-FIXES: std::unique_ptr<int> a;74 75  // Check that spaces aren't modified unnecessarily.76  std:: auto_ptr <int> b;77  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: auto_ptr is deprecated78  // CHECK-FIXES: std:: unique_ptr <int> b;79  std :: auto_ptr < char > c(new char());80  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: auto_ptr is deprecated81  // CHECK-FIXES: std :: unique_ptr < char > c(new char());82 83  // Test construction from a temporary.84  std::auto_ptr<char> d = std::auto_ptr<char>();85  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated86  // CHECK-MESSAGES: :[[@LINE-2]]:32: warning: auto_ptr is deprecated87  // CHECK-FIXES: std::unique_ptr<char> d = std::unique_ptr<char>();88 89  typedef std::auto_ptr<int> int_ptr_t;90  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: auto_ptr is deprecated91  // CHECK-FIXES: typedef std::unique_ptr<int> int_ptr_t;92  int_ptr_t e(new int());93 94  // Test pointers.95  std::auto_ptr<int> *f;96  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated97  // CHECK-FIXES: std::unique_ptr<int> *f;98 99  // Test 'static' declarations.100  static std::auto_ptr<int> g;101  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: auto_ptr is deprecated102  // CHECK-FIXES: static std::unique_ptr<int> g;103 104  // Test with cv-qualifiers.105  const std::auto_ptr<int> h;106  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: auto_ptr is deprecated107  // CHECK-FIXES: const std::unique_ptr<int> h;108  volatile std::auto_ptr<int> i;109  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: auto_ptr is deprecated110  // CHECK-FIXES: volatile std::unique_ptr<int> i;111  const volatile std::auto_ptr<int> j;112  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: auto_ptr is deprecated113  // CHECK-FIXES: const volatile std::unique_ptr<int> j;114 115  // Test auto and initializer-list.116  auto k = std::auto_ptr<int>{};117  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: auto_ptr is deprecated118  // CHECK-FIXES: auto k = std::unique_ptr<int>{};119  std::auto_ptr<int> l{std::auto_ptr<int>()};120  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated121  // CHECK-MESSAGES: :[[@LINE-2]]:29: warning: auto_ptr is deprecated122  // CHECK-FIXES: std::unique_ptr<int> l{std::unique_ptr<int>()};123 124  // Test interlocked auto_ptr.125  std::auto_ptr<std::auto_ptr<int> > m;126  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated127  // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: auto_ptr is deprecated128  // CHECK-FIXES: std::unique_ptr<std::unique_ptr<int> > m;129 130  // Test temporaries.131  std::auto_ptr<char>();132  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated133  // CHECK-FIXES: std::unique_ptr<char>();134 135  // Test void-specialization.136  std::auto_ptr<void> n;137  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated138  // CHECK-FIXES: std::unique_ptr<void> n;139 140  // Test template WITH instantiation (instantiation).141  B<double> o;142  std::auto_ptr<double> p(o.create());143  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated144  // CHECK-FIXES: std::unique_ptr<double> p(o.create());145 146  // Test 'using' in a namespace ("definition").147  ns_1::auto_ptr<int> q;148  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: auto_ptr is deprecated149  // CHECK-FIXES: ns_1::unique_ptr<int> q;150 151  // Test construction with an 'auto_ptr_ref'.152  std::auto_ptr<Base> r(create_derived_ptr());153  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated154  // CHECK-FIXES: std::unique_ptr<Base> r(create_derived_ptr());155}156 157// Test without the nested name specifiers.158void f_2() {159  using namespace std;160 161  auto_ptr<int> a;162  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: auto_ptr is deprecated163  // CHECK-FIXES: unique_ptr<int> a;164}165 166// Test using declaration.167void f_3() {168  using std::auto_ptr;169  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: auto_ptr is deprecated170  // CHECK-FIXES: using std::unique_ptr;171 172  auto_ptr<int> a;173  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: auto_ptr is deprecated174  // CHECK-FIXES: unique_ptr<int> a;175}176 177// Test messing-up with macros.178void f_4() {179#define MACRO_1 <char>180  std::auto_ptr MACRO_1 p(new char());181  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated182  // CHECK-FIXES: std::unique_ptr MACRO_1 p(new char());183#define MACRO_2 auto_ptr184  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: auto_ptr is deprecated185  // CHECK-FIXES: #define MACRO_2 unique_ptr186  std::MACRO_2<int> q;187#define MACRO_3(Type) std::auto_ptr<Type>188  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: auto_ptr is deprecated189  // CHECK-FIXES: #define MACRO_3(Type) std::unique_ptr<Type>190  MACRO_3(float)r(new float());191#define MACRO_4 std::auto_ptr192  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: auto_ptr is deprecated193  // CHECK-FIXES: #define MACRO_4 std::unique_ptr194  using MACRO_4;195#undef MACRO_1196#undef MACRO_2197#undef MACRO_3198#undef MACRO_4199}200 201// Test function return values (definition).202std::auto_ptr<char> f_5()203  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: auto_ptr is deprecated204  // CHECK-FIXES: std::unique_ptr<char> f_5()205{206  // Test constructor.207  return std::auto_ptr<char>(new char());208  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: auto_ptr is deprecated209  // CHECK-FIXES: return std::unique_ptr<char>(new char());210}211 212// Test that non-std auto_ptr aren't replaced.213void f_8() {214  ns_2::auto_ptr<char> a;215  using namespace ns_2;216  auto_ptr<int> b;217}218 219// Fail to modify when the template is never instantiated.220//221// This might not be an issue. If it's never used it doesn't really matter if222// it's changed or not. If it's a header and one of the source use it, then it223// will still be changed.224template <typename X>225void f() {226  std::auto_ptr<X> p;227}228 229// FIXME: Alias template could be replaced if a matcher existed.230namespace std {231template <typename T> using aaaaaaaa = auto_ptr<T>;232}233 234// We want to avoid replacing 'aaaaaaaa' by unique_ptr here. It's better to235// change the type alias directly.236std::aaaaaaaa<int> d;237 238 239void takes_ownership_fn(std::auto_ptr<int> x);240// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: auto_ptr is deprecated241// CHECK-FIXES: void takes_ownership_fn(std::unique_ptr<int> x);242 243std::auto_ptr<int> get_by_value();244// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: auto_ptr is deprecated245// CHECK-FIXES: std::unique_ptr<int> get_by_value();246 247class Wrapper {248 public:249  std::auto_ptr<int> &get_wrapped();250  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated251 252 private:253  std::auto_ptr<int> wrapped;254  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated255};256 257void f() {258  std::auto_ptr<int> a, b, c;259  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated260  // CHECK-FIXES: std::unique_ptr<int> a, b, c;261  Wrapper wrapper_a, wrapper_b;262 263  a = b;264  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::move to transfer ownership265  // CHECK-FIXES: a = std::move(b);266 267  wrapper_a.get_wrapped() = wrapper_b.get_wrapped();268  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::move to transfer ownership269  // CHECK-FIXES: wrapper_a.get_wrapped() = std::move(wrapper_b.get_wrapped());270 271  // Test that 'std::move()' is inserted when call to the272  // copy-constructor are made.273  takes_ownership_fn(c);274  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::move to transfer ownership275  // CHECK-FIXES: takes_ownership_fn(std::move(c));276  takes_ownership_fn(wrapper_a.get_wrapped());277  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::move to transfer ownership278  // CHECK-FIXES: takes_ownership_fn(std::move(wrapper_a.get_wrapped()));279 280  std::auto_ptr<int> d[] = { std::auto_ptr<int>(new int(1)),281                             std::auto_ptr<int>(new int(2)) };282  // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: auto_ptr is deprecated283  // CHECK-MESSAGES: :[[@LINE-3]]:35: warning: auto_ptr is deprecated284  // CHECK-MESSAGES: :[[@LINE-3]]:35: warning: auto_ptr is deprecated285  // CHECK-FIXES: std::unique_ptr<int> d[] = { std::unique_ptr<int>(new int(1)),286  // CHECK-FIXES-NEXT:                         std::unique_ptr<int>(new int(2)) };287  std::auto_ptr<int> e = d[0];288  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated289  // CHECK-MESSAGES: :[[@LINE-2]]:26: warning: use std::move to transfer ownership290  // CHECK: std::unique_ptr<int> e = std::move(d[0]);291 292  // Test that std::move() is not used when assigning an rvalue293  std::auto_ptr<int> f;294  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated295  // CHECK-FIXES: std::unique_ptr<int> f;296  f = std::auto_ptr<int>(new int(0));297  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: auto_ptr is deprecated298  // CHECK-NEXT: f = std::unique_ptr<int>(new int(0));299 300  std::auto_ptr<int> g = get_by_value();301  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: auto_ptr is deprecated302  // CHECK-FIXES: std::unique_ptr<int> g = get_by_value();303}304