1// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t
2
3enum E {
4 ENUM_LITERAL = 1
5};
6
7typedef int* IntPtr;
8
9int i = 4;
10int j = 1;
11int *p = 0;
12int *q = 0;
13IntPtr ip = 0;
14
15void fail() {
16 q = p + 4;
17 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]
18 p = q + i;
19 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
20 p = q + ENUM_LITERAL;
21 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
22
23 q = p - 1;
24 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
25 p = q - i;
26 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
27 p = q - ENUM_LITERAL;
28 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
29
30 p += 4;
31 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
32 p += i;
33 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
34 p += ENUM_LITERAL;
35 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
36
37 q -= 1;
38 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
39 q -= i;
40 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
41 q -= ENUM_LITERAL;
42 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
43
44 p++;
45 // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
46 ++p;
47 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
48
49 p--;
50 // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
51 --p;
52 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
53
54 i = p[1];
55 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
56
57 p = ip + 1;
58 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: do not use pointer arithmetic
59 ip++;
60 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
61 i = ip[1];
62 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
63}
64
65template <typename T>
66void template_fail() {
67 T* p;
68 T* q;
69
70 p = q + 1;
71 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
72 q = p - 1;
73 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
74 p++;
75 // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
76 i = p[1];
77 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
78}
79
80void instantiate() {
81 template_fail<int>();
82}
83
84struct S {
85 operator int() const;
86};
87
88void f(S &s) {
89 int *i;
90 i = i + s;
91 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
92}
93
94void f2(int i[]) {
95 i[1] = 0;
96 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use pointer arithmetic
97}
98
99void okay() {
100 int a[3];
101 i = a[2]; // OK, access to array
102
103 p = q;
104 p = &i;
105
106 i++;
107 ++i;
108 i--;
109 --i;
110 i += 1;
111 i -= 1;
112 i = j + 1;
113 i = j - 1;
114
115 auto diff = p - q; // OK, result is arithmetic
116
117 for(int ii : a) ; // OK, pointer arithmetic generated by compiler
118}
119
120namespace gh126424 {
121
122namespace std {
123template <typename, typename>
124class pair {};
125
126template <typename Key, typename Value>
127class map {
128 public:
129 using value_type = pair<Key, Value>;
130 value_type& operator[](const Key& key);
131 value_type& operator[](Key&& key);
132 };
133}
134
135template <typename R>
136int f(std::map<R*, int>& map, R* r) {
137 return map[r]; // OK
138}
139
140}
141

source code of clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp