brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.0 KiB · 02d32c0 Raw
75 lines · cpp
1// RUN: %check_clang_tidy %s misc-const-correctness %t \2// RUN: -config='{CheckOptions: \3// RUN:  {misc-const-correctness.AnalyzeValues: true,\4// RUN:   misc-const-correctness.WarnPointersAsValues: true,\5// RUN:   misc-const-correctness.WarnPointersAsPointers: false,\6// RUN:   misc-const-correctness.TransformPointersAsValues: true}}' \7// RUN: -- -fno-delayed-template-parsing8 9void potential_const_pointer() {10  double np_local0[10] = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};11  double *p_local0 = &np_local0[1];12  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'double *' can be declared 'const'13  // CHECK-FIXES: double *const p_local0 = &np_local0[1];14 15  using doublePtr = double*;16  using doubleArray = double[15];17  doubleArray np_local1;18  doublePtr p_local1 = &np_local1[0];19  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local1' of type 'doublePtr' (aka 'double *') can be declared 'const'20  // CHECK-FIXES: doublePtr const p_local1 = &np_local1[0];21}22 23void range_for() {24  int np_local0[2] = {1, 2};25  int *p_local0[2] = {&np_local0[0], &np_local0[1]};26  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *[2]' can be declared 'const'27  // CHECK-FIXES: int *const p_local0[2] = {&np_local0[0], &np_local0[1]};28  for (const int *p_local1 : p_local0) {29  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: variable 'p_local1' of type 'const int *' can be declared 'const'30  // CHECK-FIXES: for (const int *const p_local1 : p_local0) {31  }32 33  int *p_local2[2] = {nullptr, nullptr};34  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local2' of type 'int *[2]' can be declared 'const'35  // CHECK-FIXES: int *const p_local2[2] = {nullptr, nullptr};36  for (const auto *con_ptr : p_local2) {37  }38}39 40template <typename T>41struct SmallVectorBase {42  T data[4];43  void push_back(const T &el) {}44  int size() const { return 4; }45  T *begin() { return data; }46  const T *begin() const { return data; }47  T *end() { return data + 4; }48  const T *end() const { return data + 4; }49};50 51template <typename T>52struct SmallVector : SmallVectorBase<T> {};53 54template <class T>55void EmitProtocolMethodList(T &&Methods) {56  // Note: If the template is uninstantiated the analysis does not figure out,57  // that p_local0 could be const. Not sure why, but probably bails because58  // some expressions are type-dependent.59  SmallVector<const int *> p_local0;60  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'SmallVector<const int *>' can be declared 'const'61  // CHECK-FIXES: SmallVector<const int *> const p_local0;62  SmallVector<const int *> np_local0;63  for (const auto *I : Methods) {64    if (I == nullptr)65      np_local0.push_back(I);66  }67  p_local0.size();68}69void instantiate() {70  int *p_local0[4] = {nullptr, nullptr, nullptr, nullptr};71  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int *[4]' can be declared 'const'72  // CHECK-FIXES: int *const p_local0[4] = {nullptr, nullptr, nullptr, nullptr};73  EmitProtocolMethodList(p_local0);74}75