brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · 00d1289 Raw
65 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-misplaced-pointer-arithmetic-in-alloc %t2 3class C {4  int num;5public:6  explicit C(int n) : num(n) {}7};8 9void bad_new(int n, int m) {10  C *p = new C(n) + 10;11  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument12  // CHECK-FIXES: C *p = new C(n + 10);13 14  p = new C(n) - 10;15  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument16  // CHECK-FIXES: p = new C(n - 10);17 18  p = new C(n) + m;19  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument20  // CHECK-FIXES: p = new C(n + m);21 22  p = new C(n) - (m + 10);23  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument24  // CHECK-FIXES: p = new C(n - (m + 10));25 26  p = new C(n) - m + 10;27  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument28  // CHECK-FIXES: p = new C(n - m) + 10;29  // FIXME: Should be p = new C(n - m + 10);30}31 32void bad_new_array(int n, int m) {33  char *p = new char[n] + 10;34  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument35  // CHECK-FIXES: char *p = new char[n + 10];36 37  p = new char[n] - 10;38  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument39  // CHECK-FIXES: p = new char[n - 10];40 41  p = new char[n] + m;42  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument43  // CHECK-FIXES: p = new char[n + m];44 45  p = new char[n] - (m + 10);46  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument47  // CHECK-FIXES: p = new char[n - (m + 10)];48 49  p = new char[n] - m + 10;50  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: arithmetic operation is applied to the result of operator new[]() instead of its size-like argument51  // CHECK-FIXES: p = new char[n - m] + 10;52  // FIXME: should be p = new char[n - m + 10];53}54 55namespace std {56typedef decltype(sizeof(void*)) size_t;57}58 59void* operator new(std::size_t, void*);60 61void placement_new_ptr(void *buf, C *old) {62  C **p = new (buf) C*(old) + 1;63  // CHECK-MESSAGES-NOT: :[[@LINE-1]]:11: warning: arithmetic operation is applied to the result of operator new() instead of its size-like argument64}65