File: container-data-pointer.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (146 lines) | stat: -rw-r--r-- 5,689 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// RUN: %check_clang_tidy -check-suffixes=,CLASSIC %s readability-container-data-pointer %t -- -- -isystem %clang_tidy_headers -fno-delayed-template-parsing
// RUN: %check_clang_tidy -check-suffixes=,WITH-CONFIG %s readability-container-data-pointer %t -- -config="{CheckOptions: [{key: readability-container-data-pointer.IgnoredContainers, value: '::std::basic_string'}]}" -- -isystem %clang_tidy_headers -fno-delayed-template-parsing

#include <string>

typedef __SIZE_TYPE__ size_t;

namespace std {
template <typename T>
struct vector {
  using size_type = size_t;

  vector();
  explicit vector(size_type);

  T *data();
  const T *data() const;

  T &operator[](size_type);
  const T &operator[](size_type) const;
};

template <typename T>
struct is_integral;

template <>
struct is_integral<size_t> {
  static const bool value = true;
};

template <bool, typename T = void>
struct enable_if { };

template <typename T>
struct enable_if<true, T> {
  typedef T type;
};
}

template <typename T>
void f(const T *);

#define z (0)

void g(size_t s) {
  std::vector<unsigned char> b(s);
  f(&((b)[(z)]));
  // 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]
  // CHECK-FIXES: {{^  }}f(b.data());{{$}}
}

void h() {
  std::string s;
  f(&((s).operator[]((z))));
  // 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]
  // CHECK-FIXES-CLASSIC: {{^  }}f(s.data());{{$}}
  // 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]

  std::wstring w;
  f(&((&(w))->operator[]((z))));
  // 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]
  // CHECK-FIXES-CLASSIC: {{^  }}f(w.data());{{$}}
  // 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]
}

template <typename T, typename U,
          typename = typename std::enable_if<std::is_integral<U>::value>::type>
void i(U s) {
  std::vector<T> b(s);
  f(&b[0]);
  // 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]
  // CHECK-FIXES: {{^  }}f(b.data());{{$}}
}

template void i<unsigned char, size_t>(size_t);

void j(std::vector<unsigned char> * const v) {
  f(&(*v)[0]);
  // 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]
  // CHECK-FIXES: {{^  }}f(v->data());{{$}}
}

void k(const std::vector<unsigned char> &v) {
  f(&v[0]);
  // 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]
  // CHECK-FIXES: {{^  }}f(v.data());{{$}}
}

void l() {
  unsigned char b[32];
  f(&b[0]);
  // 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]
}

template <typename T>
void m(const std::vector<T> &v) {
  return &v[0];
  // 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]
  // CHECK-FIXES: {{^  }}return v.data();{{$}}
}

template <typename T>
struct container_without_data {
  using size_type = size_t;
  T &operator[](size_type);
  const T &operator[](size_type) const;
};

template <typename T>
const T *n(const container_without_data<T> &c) {
  // c has no "data" member function, so there should not be a warning here:
  return &c[0];
}

const int *o(const std::vector<std::vector<std::vector<int>>> &v, const size_t idx1, const size_t idx2) {
  return &v[idx1][idx2][0];
  // 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]
  // CHECK-FIXES: {{^  }}return v[idx1][idx2].data();{{$}}
}

std::vector<int> &select(std::vector<int> &u, std::vector<int> &v) {
  return v;
}

int *p(std::vector<int> &v1, std::vector<int> &v2) {
  return &select(*&v1, v2)[0];
  // 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]
  // CHECK-FIXES: {{^  }}return select(*&v1, v2).data();{{$}}
}

int *q(std::vector<int> ***v) {
  return &(***v)[0];
  // 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]
  // CHECK-FIXES: {{^  }}return (**v)->data();{{$}}
}

struct VectorHolder {
  std::vector<int> v;
};

int *r() {
  VectorHolder holder;
  return &holder.v[0];
  // 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]
  // CHECK-FIXES: {{^  }}return holder.v.data();{{$}}
}