brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.1 KiB · 2ed1e93 Raw
170 lines · cpp
1// RUN: %check_clang_tidy -check-suffixes=,CLASSIC %s readability-container-data-pointer %t -- -- -isystem %clang_tidy_headers -fno-delayed-template-parsing2// RUN: %check_clang_tidy -check-suffixes=,WITH-CONFIG %s readability-container-data-pointer %t -- -config="{CheckOptions: {readability-container-data-pointer.IgnoredContainers: '::std::basic_string'}}" -- -isystem %clang_tidy_headers -fno-delayed-template-parsing3 4#include <string>5 6typedef __SIZE_TYPE__ size_t;7 8namespace std {9template <typename T>10struct vector {11  using size_type = size_t;12 13  vector();14  explicit vector(size_type);15 16  T *data();17  const T *data() const;18 19  T &operator[](size_type);20  const T &operator[](size_type) const;21};22 23template <typename T>24struct is_integral;25 26template <>27struct is_integral<size_t> {28  static const bool value = true;29};30 31template <bool, typename T = void>32struct enable_if { };33 34template <typename T>35struct enable_if<true, T> {36  typedef T type;37};38 39template <typename T>40struct unique_ptr {41  T &operator*() const;42  T *operator->() const;43};44}45 46template <typename T>47void f(const T *);48 49#define z (0)50 51void g(size_t s) {52  std::vector<unsigned char> b(s);53  f(&((b)[(z)]));54  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]55  // CHECK-FIXES: f(b.data());56}57 58void h() {59  std::string s;60  f(&((s).operator[]((z))));61  // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]62  // CHECK-FIXES-CLASSIC: f(s.data());63  // CHECK-MESSAGES-WITH-CONFIG-NOT: :[[@LINE-3]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]64 65  std::wstring w;66  f(&((&(w))->operator[]((z))));67  // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]68  // CHECK-FIXES-CLASSIC: f(w.data());69  // CHECK-MESSAGES-WITH-CONFIG-NOT: :[[@LINE-3]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]70}71 72template <typename T, typename U,73          typename = typename std::enable_if<std::is_integral<U>::value>::type>74void i(U s) {75  std::vector<T> b(s);76  f(&b[0]);77  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]78  // CHECK-FIXES: f(b.data());79}80 81template void i<unsigned char, size_t>(size_t);82 83void j(std::vector<unsigned char> * const v) {84  f(&(*v)[0]);85  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]86  // CHECK-FIXES: f(v->data());87}88 89void k(const std::vector<unsigned char> &v) {90  f(&v[0]);91  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]92  // CHECK-FIXES: f(v.data());93}94 95void l() {96  unsigned char b[32];97  f(&b[0]);98  // CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]99}100 101template <typename T>102void m(const std::vector<T> &v) {103  return &v[0];104  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]105  // CHECK-FIXES: return v.data();106}107 108template <typename T>109struct container_without_data {110  using size_type = size_t;111  T &operator[](size_type);112  const T &operator[](size_type) const;113};114 115template <typename T>116const T *n(const container_without_data<T> &c) {117  // c has no "data" member function, so there should not be a warning here:118  return &c[0];119}120 121const int *o(const std::vector<std::vector<std::vector<int>>> &v, const size_t idx1, const size_t idx2) {122  return &v[idx1][idx2][0];123  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]124  // CHECK-FIXES: return v[idx1][idx2].data();125}126 127std::vector<int> &select(std::vector<int> &u, std::vector<int> &v) {128  return v;129}130 131int *p(std::vector<int> &v1, std::vector<int> &v2) {132  return &select(*&v1, v2)[0];133  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]134  // CHECK-FIXES: return select(*&v1, v2).data();135}136 137int *q(std::vector<int> ***v) {138  return &(***v)[0];139  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]140  // CHECK-FIXES: return (**v)->data();141}142 143struct VectorHolder {144  std::vector<int> v;145};146 147int *r() {148  VectorHolder holder;149  return &holder.v[0];150  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]151  // CHECK-FIXES: return holder.v.data();152}153 154void s(std::unique_ptr<std::vector<unsigned char>> p) {155  f(&(*p)[0]);156  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]157  // CHECK-FIXES: f((*p).data());158}159 160void t(std::unique_ptr<container_without_data<unsigned char>> p) {161  // p has no "data" member function, so no warning162  f(&(*p)[0]);163}164 165template <typename T>166void u(std::unique_ptr<T> p) {167  // we don't know if 'T' will always have "data" member function, so no warning168  f(&(*p)[0]);169}170