brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.1 KiB · b9ffeec Raw
295 lines · cpp
1// RUN: %check_clang_tidy %s modernize-use-nullptr %t -- -- -fno-delayed-template-parsing2 3const unsigned int g_null = 0;4#define NULL 05 6void test_assignment() {7  int *p1 = 0;8  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr [modernize-use-nullptr]9  // CHECK-FIXES: int *p1 = nullptr;10  p1 = 0;11  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr12  // CHECK-FIXES: p1 = nullptr;13 14  int *p2 = NULL;15  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr16  // CHECK-FIXES: int *p2 = nullptr;17 18  p2 = p1;19  // CHECK-FIXES: p2 = p1;20 21  int *p3;22  p3 = NULL;23  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr24  // CHECK-FIXES: p3 = nullptr;25 26  int *p4 = p3;27  // CHECK-FIXES: int *p4 = p3;28 29  int i1 = 0;30 31  int i2 = NULL;32 33  const int null = 0;34  int i3 = null;35 36  int *p5, *p6, *p7;37  p5 = p6 = p7 = NULL;38  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr39  // CHECK-FIXES: p5 = p6 = p7 = nullptr;40}41 42struct Foo {43  Foo(int *p = NULL) : m_p1(p) {}44  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use nullptr45  // CHECK-FIXES: Foo(int *p = nullptr) : m_p1(p) {}46 47  void bar(int *p = 0) {}48  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use nullptr49  // CHECK-FIXES: void bar(int *p = nullptr) {}50 51  void baz(int i = 0) {}52 53  int *m_p1;54  static int *m_p2;55};56 57int *Foo::m_p2 = NULL;58// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr59// CHECK-FIXES: int *Foo::m_p2 = nullptr;60 61struct Baz {62  Baz() : i(0) {}63  int i;64};65 66void test_cxx_cases() {67  Foo f;68 69  f.bar(NULL);70  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr71  // CHECK-FIXES: f.bar(nullptr);72 73  f.baz(g_null);74 75  f.m_p1 = 0;76  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr77  // CHECK-FIXES: f.m_p1 = nullptr;78 79  Baz b2;80  int Baz::*memptr(0);81  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr82  // CHECK-FIXES: int Baz::*memptr(nullptr);83 84  memptr = 0;85  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr86  // CHECK-FIXES: memptr = nullptr;87}88 89void test_function_default_param1(void *p = 0);90// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr91// CHECK-FIXES: void test_function_default_param1(void *p = nullptr);92 93void test_function_default_param2(void *p = NULL);94// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr95// CHECK-FIXES: void test_function_default_param2(void *p = nullptr);96 97void test_function(int *p) {}98 99void test_function_no_ptr_param(int i) {}100 101void test_function_call() {102  test_function(0);103  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr104  // CHECK-FIXES: test_function(nullptr);105 106  test_function(NULL);107  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr108  // CHECK-FIXES: test_function(nullptr);109 110  test_function_no_ptr_param(0);111}112 113char *test_function_return1() {114  return 0;115  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr116  // CHECK-FIXES: return nullptr;117}118 119void *test_function_return2() {120  return NULL;121  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr122  // CHECK-FIXES: return nullptr;123}124 125int test_function_return3() {126  return 0;127}128 129int test_function_return4() {130  return NULL;131}132 133int test_function_return5() {134  return g_null;135}136 137int *test_function_return_cast() {138#define RET return139  RET 0;140  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use nullptr141  // CHECK-FIXES: RET nullptr;142#undef RET143}144 145// Test parentheses expressions resulting in a nullptr.146int *test_parentheses_expression() {147  return(0);148  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr149  // CHECK-FIXES: return(nullptr);150}151 152int *test_nested_parentheses_expression() {153  return((((0))));154  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr155  // CHECK-FIXES: return((((nullptr))));156}157 158void *test_parentheses_explicit_cast() {159  return(static_cast<void*>(0));160  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr161  // CHECK-FIXES: return(static_cast<void*>(nullptr));162}163 164void *test_parentheses_explicit_cast_sequence1() {165  return(static_cast<void*>(static_cast<int*>((void*)NULL)));166  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr167  // CHECK-FIXES: return(static_cast<void*>(nullptr));168}169 170void *test_parentheses_explicit_cast_sequence2() {171  return(static_cast<void*>(reinterpret_cast<int*>((float*)(0))));172  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr173  // CHECK-FIXES: return(static_cast<void*>(nullptr));174}175 176// Test explicit cast expressions resulting in nullptr.177struct Bam {178  Bam(int *a) {}179  Bam(float *a) {}180  Bam operator=(int *a) { return Bam(a); }181  Bam operator=(float *a) { return Bam(a); }182};183 184void ambiguous_function(int *a) {}185void ambiguous_function(float *a) {}186void const_ambiguous_function(const int *p) {}187void const_ambiguous_function(const float *p) {}188 189void test_explicit_cast_ambiguous1() {190  ambiguous_function((int*)0);191  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use nullptr192  // CHECK-FIXES: ambiguous_function((int*)nullptr);193}194 195void test_explicit_cast_ambiguous2() {196  ambiguous_function((int*)(0));197  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use nullptr198  // CHECK-FIXES: ambiguous_function((int*)nullptr);199}200 201void test_explicit_cast_ambiguous3() {202  ambiguous_function(static_cast<int*>(reinterpret_cast<int*>((float*)0)));203  // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: use nullptr204  // CHECK-FIXES: ambiguous_function(static_cast<int*>(nullptr));205}206 207Bam test_explicit_cast_ambiguous4() {208  return(((int*)(0)));209  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr210  // CHECK-FIXES: return(((int*)nullptr));211}212 213void test_explicit_cast_ambiguous5() {214  // Test for ambiguous overloaded constructors.215  Bam k((int*)(0));216  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use nullptr217  // CHECK-FIXES: Bam k((int*)nullptr);218 219  // Test for ambiguous overloaded operators.220  k = (int*)0;221  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr222  // CHECK-FIXES: k = (int*)nullptr;223}224 225void test_const_pointers_abiguous() {226  const_ambiguous_function((int*)0);227  // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use nullptr228  // CHECK-FIXES: const_ambiguous_function((int*)nullptr);229}230 231// Test where the implicit cast to null is surrounded by another implicit cast232// with possible explicit casts in-between.233void test_const_pointers() {234  const int *const_p1 = 0;235  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr236  // CHECK-FIXES: const int *const_p1 = nullptr;237  const int *const_p2 = NULL;238  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr239  // CHECK-FIXES: const int *const_p2 = nullptr;240  const int *const_p3 = (int*)0;241  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use nullptr242  // CHECK-FIXES: const int *const_p3 = (int*)nullptr;243  int *t;244  const int *const_p4 = static_cast<int*>(t ? t : static_cast<int*>(0));245  // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: use nullptr246  // CHECK-FIXES: const int *const_p4 = static_cast<int*>(t ? t : static_cast<int*>(nullptr));247}248 249void test_nested_implicit_cast_expr() {250  int func0(void*, void*);251  int func1(int, void*, void*);252 253  (double)func1(0, 0, 0);254  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr255  // CHECK-MESSAGES: :[[@LINE-2]]:23: warning: use nullptr256  // CHECK-FIXES: (double)func1(0, nullptr, nullptr);257  (double)func1(func0(0, 0), 0, 0);258  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: use nullptr259  // CHECK-MESSAGES: :[[@LINE-2]]:26: warning: use nullptr260  // CHECK-MESSAGES: :[[@LINE-3]]:30: warning: use nullptr261  // CHECK-MESSAGES: :[[@LINE-4]]:33: warning: use nullptr262  // CHECK-FIXES: (double)func1(func0(nullptr, nullptr), nullptr, nullptr);263}264 265// FIXME: currently, the check doesn't work as it should with templates.266template<typename T>267class A {268 public:269  A(T *p = NULL) {270    Ptr = static_cast<T*>(NULL);271 272    Ptr = static_cast<T*>(reinterpret_cast<int*>((void*)NULL));273    // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr274    // CHECK-FIXES: Ptr = static_cast<T*>(nullptr);275    // FIXME: a better fix-it is: Ptr = nullptr;276 277    T *p2 = static_cast<T*>(reinterpret_cast<int*>((void*)NULL));278    // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr279    // CHECK-FIXES: T *p2 = static_cast<T*>(nullptr);280    // FIXME: a better fix-it is: T *p2 = nullptr;281 282    Ptr = NULL;283  }284 285  void f() {286    Ptr = NULL;287  }288  T *Ptr;289};290 291template<typename T>292T *f2(T *a = NULL) {293  return a ? a : NULL;294}295