1// RUN: %check_clang_tidy %s cppcoreguidelines-avoid-non-const-global-variables %t
2
3int nonConstInt = 0;
4// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'nonConstInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
5
6int &nonConstIntReference = nonConstInt;
7// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: variable 'nonConstIntReference' provides global access to a non-const object; consider making the referenced data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
8
9int *pointerToNonConstInt = &nonConstInt;
10// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: variable 'pointerToNonConstInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
11// CHECK-MESSAGES: :[[@LINE-2]]:6: warning: variable 'pointerToNonConstInt' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
12
13int *const constPointerToNonConstInt = &nonConstInt;
14// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'constPointerToNonConstInt' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
15
16namespace namespace_name {
17int nonConstNamespaceInt = 0;
18// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'nonConstNamespaceInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
19
20const int constNamespaceInt = 0;
21} // namespace namespace_name
22
23const int constInt = 0;
24
25const int *pointerToConstInt = &constInt;
26// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'pointerToConstInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
27
28const int *const constPointerToConstInt = &constInt;
29
30const int &constReferenceToConstInt = constInt;
31
32constexpr int constexprInt = 0;
33
34int function() {
35 int nonConstReturnValue = 0;
36 return nonConstReturnValue;
37}
38
39namespace {
40int nonConstAnonymousNamespaceInt = 0;
41// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'nonConstAnonymousNamespaceInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
42} // namespace
43
44class DummyClass {
45public:
46 int nonConstPublicMemberVariable = 0;
47 const int constPublicMemberVariable = 0;
48
49private:
50 int nonConstPrivateMemberVariable = 0;
51 const int constPrivateMemberVariable = 0;
52};
53
54DummyClass nonConstClassInstance;
55// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'nonConstClassInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
56
57DummyClass *pointerToNonConstDummyClass = &nonConstClassInstance;
58// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'pointerToNonConstDummyClass' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
59// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: variable 'pointerToNonConstDummyClass' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
60
61DummyClass &referenceToNonConstDummyClass = nonConstClassInstance;
62// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'referenceToNonConstDummyClass' provides global access to a non-const object; consider making the referenced data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
63
64int *nonConstPointerToMember = &nonConstClassInstance.nonConstPublicMemberVariable;
65// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: variable 'nonConstPointerToMember' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
66// CHECK-MESSAGES: :[[@LINE-2]]:6: warning: variable 'nonConstPointerToMember' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
67int *const constPointerToNonConstMember = &nonConstClassInstance.nonConstPublicMemberVariable;
68// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'constPointerToNonConstMember' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
69
70const DummyClass constClassInstance;
71
72DummyClass *const constPointerToNonConstDummyClass = &nonConstClassInstance;
73// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: variable 'constPointerToNonConstDummyClass' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
74
75const DummyClass *nonConstPointerToConstDummyClass = &constClassInstance;
76// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: variable 'nonConstPointerToConstDummyClass' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
77
78const DummyClass *const constPointerToConstDummyClass = &constClassInstance;
79
80const int *const constPointerToConstMember = &constClassInstance.nonConstPublicMemberVariable;
81
82const DummyClass &constReferenceToDummyClass = constClassInstance;
83
84namespace namespace_name {
85DummyClass nonConstNamespaceClassInstance;
86// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'nonConstNamespaceClassInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
87
88const DummyClass constDummyClassInstance;
89} // namespace namespace_name
90
91// CHECKING FOR NON-CONST GLOBAL ENUM /////////////////////////////////////////
92enum DummyEnum {
93 first,
94 second
95};
96
97DummyEnum nonConstDummyEnumInstance = DummyEnum::first;
98// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: variable 'nonConstDummyEnumInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
99
100DummyEnum *pointerToNonConstDummyEnum = &nonConstDummyEnumInstance;
101// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'pointerToNonConstDummyEnum' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
102// CHECK-MESSAGES: :[[@LINE-2]]:12: warning: variable 'pointerToNonConstDummyEnum' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
103
104DummyEnum &referenceToNonConstDummyEnum = nonConstDummyEnumInstance;
105// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'referenceToNonConstDummyEnum' provides global access to a non-const object; consider making the referenced data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
106
107DummyEnum *const constPointerToNonConstDummyEnum = &nonConstDummyEnumInstance;
108// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: variable 'constPointerToNonConstDummyEnum' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
109
110const DummyEnum constDummyEnumInstance = DummyEnum::first;
111
112const DummyEnum *nonConstPointerToConstDummyEnum = &constDummyEnumInstance;
113// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: variable 'nonConstPointerToConstDummyEnum' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
114
115const DummyEnum *const constPointerToConstDummyEnum = &constDummyEnumInstance;
116
117const DummyEnum &referenceToConstDummyEnum = constDummyEnumInstance;
118
119namespace namespace_name {
120DummyEnum nonConstNamespaceEnumInstance = DummyEnum::first;
121// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: variable 'nonConstNamespaceEnumInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
122
123const DummyEnum constNamespaceEnumInstance = DummyEnum::first;
124} // namespace namespace_name
125
126namespace {
127DummyEnum nonConstAnonymousNamespaceEnumInstance = DummyEnum::first;
128}
129// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: variable 'nonConstAnonymousNamespaceEnumInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
130
131// CHECKING FOR NON-CONST GLOBAL STRUCT ///////////////////////////////////////
132struct DummyStruct {
133public:
134 int structIntElement = 0;
135 const int constStructIntElement = 0;
136
137private:
138 int privateStructIntElement = 0;
139};
140
141DummyStruct nonConstDummyStructInstance;
142// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'nonConstDummyStructInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
143
144DummyStruct *pointerToNonConstDummyStruct = &nonConstDummyStructInstance;
145// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'pointerToNonConstDummyStruct' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
146// CHECK-MESSAGES: :[[@LINE-2]]:14: warning: variable 'pointerToNonConstDummyStruct' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
147
148DummyStruct &referenceToNonConstDummyStruct = nonConstDummyStructInstance;
149// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'referenceToNonConstDummyStruct' provides global access to a non-const object; consider making the referenced data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
150DummyStruct *const constPointerToNonConstDummyStruct = &nonConstDummyStructInstance;
151// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: variable 'constPointerToNonConstDummyStruct' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
152
153const DummyStruct constDummyStructInstance;
154
155const DummyStruct *nonConstPointerToConstDummyStruct = &constDummyStructInstance;
156// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: variable 'nonConstPointerToConstDummyStruct' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
157
158const DummyStruct *const constPointerToConstDummyStruct = &constDummyStructInstance;
159
160const DummyStruct &referenceToConstDummyStruct = constDummyStructInstance;
161
162namespace namespace_name {
163DummyStruct nonConstNamespaceDummyStructInstance;
164// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'nonConstNamespaceDummyStructInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
165
166const DummyStruct constNamespaceDummyStructInstance;
167} // namespace namespace_name
168
169namespace {
170DummyStruct nonConstAnonymousNamespaceStructInstance;
171}
172// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: variable 'nonConstAnonymousNamespaceStructInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
173
174// CHECKING FOR NON-CONST GLOBAL UNION ////////////////////////////////////////
175union DummyUnion {
176 int unionInteger;
177 char unionChar;
178};
179
180DummyUnion nonConstUnionIntInstance = {.unionInteger: 0x0};
181// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'nonConstUnionIntInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
182
183DummyUnion *nonConstPointerToNonConstUnionInt = &nonConstUnionIntInstance;
184// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'nonConstPointerToNonConstUnionInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
185// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: variable 'nonConstPointerToNonConstUnionInt' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
186
187DummyUnion *const constPointerToNonConstUnionInt = &nonConstUnionIntInstance;
188// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: variable 'constPointerToNonConstUnionInt' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
189
190DummyUnion &referenceToNonConstUnionInt = nonConstUnionIntInstance;
191// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'referenceToNonConstUnionInt' provides global access to a non-const object; consider making the referenced data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
192
193const DummyUnion constUnionIntInstance = {.unionInteger: 0x0};
194
195const DummyUnion *nonConstPointerToConstUnionInt = &constUnionIntInstance;
196// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: variable 'nonConstPointerToConstUnionInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
197
198const DummyUnion *const constPointerToConstUnionInt = &constUnionIntInstance;
199
200const DummyUnion &referenceToConstUnionInt = constUnionIntInstance;
201
202namespace namespace_name {
203DummyUnion nonConstNamespaceDummyUnionInstance;
204// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'nonConstNamespaceDummyUnionInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
205
206const DummyUnion constNamespaceDummyUnionInstance = {.unionInteger: 0x0};
207} // namespace namespace_name
208
209namespace {
210DummyUnion nonConstAnonymousNamespaceUnionInstance = {.unionInteger: 0x0};
211}
212// CHECK-MESSAGES: :[[@LINE-2]]:12: warning: variable 'nonConstAnonymousNamespaceUnionInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
213
214// CHECKING FOR NON-CONST GLOBAL FUNCTION POINTER /////////////////////////////
215int dummyFunction() {
216 return 0;
217}
218
219typedef int (*functionPointer)();
220functionPointer fp1 = &dummyFunction;
221// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: variable 'fp1' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
222
223typedef int (*const functionConstPointer)();
224functionPointer fp2 = &dummyFunction;
225// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: variable 'fp2' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
226
227// CHECKING FOR NON-CONST GLOBAL TEMPLATE VARIABLE ////////////////////////////
228template <class T>
229constexpr T templateVariable = T(0L);
230
231// CHECKING AGAINST FALSE POSITIVES INSIDE FUNCTION SCOPE /////////////////////
232int main() {
233 for (int i = 0; i < 3; ++i) {
234 static int staticNonConstLoopVariable = 42;
235 int nonConstLoopVariable = 42;
236 nonConstInt = nonConstLoopVariable + i + staticNonConstLoopVariable;
237 }
238}
239
240// CHECKING AGAINST FALSE POSITIVES INSIDE STRUCT SCOPE /////////////////////
241struct StructWithStatic {
242 static DummyStruct nonConstDummyStructInstance;
243 static int value;
244 static int* valuePtr;
245 static int& valueRef;
246};
247
248DummyStruct StructWithStatic::nonConstDummyStructInstance;
249int StructWithStatic::value = 0;
250int* StructWithStatic::valuePtr = &StructWithStatic::value;
251int& StructWithStatic::valueRef = StructWithStatic::value;
252
253

source code of clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp