1 // RUN: %check_clang_tidy %s cppcoreguidelines-avoid-non-const-global-variables %t
2 
3 int 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 
6 int &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 
9 int *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 
13 int *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 
16 namespace namespace_name {
17 int 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 
20 const int constNamespaceInt = 0;
21 } // namespace namespace_name
22 
23 const int constInt = 0;
24 
25 const 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 
28 const int *const constPointerToConstInt = &constInt;
29 
30 const int &constReferenceToConstInt = constInt;
31 
32 constexpr int constexprInt = 0;
33 
function()34 int function() {
35   int nonConstReturnValue = 0;
36   return nonConstReturnValue;
37 }
38 
39 namespace {
40 int 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 
44 class DummyClass {
45 public:
46   int nonConstPublicMemberVariable = 0;
47   const int constPublicMemberVariable = 0;
48 
49 private:
50   int nonConstPrivateMemberVariable = 0;
51   const int constPrivateMemberVariable = 0;
52 };
53 
54 DummyClass 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 
57 DummyClass *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 
61 DummyClass &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 
64 int *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]
67 int *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 
70 const DummyClass constClassInstance;
71 
72 DummyClass *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 
75 const 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 
78 const DummyClass *const constPointerToConstDummyClass = &constClassInstance;
79 
80 const int *const constPointerToConstMember = &constClassInstance.nonConstPublicMemberVariable;
81 
82 const DummyClass &constReferenceToDummyClass = constClassInstance;
83 
84 namespace namespace_name {
85 DummyClass 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 
88 const DummyClass constDummyClassInstance;
89 } // namespace namespace_name
90 
91 // CHECKING FOR NON-CONST GLOBAL ENUM /////////////////////////////////////////
92 enum DummyEnum {
93   first,
94   second
95 };
96 
97 DummyEnum 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 
100 DummyEnum *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 
104 DummyEnum &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 
107 DummyEnum *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 
110 const DummyEnum constDummyEnumInstance = DummyEnum::first;
111 
112 const 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 
115 const DummyEnum *const constPointerToConstDummyEnum = &constDummyEnumInstance;
116 
117 const DummyEnum &referenceToConstDummyEnum = constDummyEnumInstance;
118 
119 namespace namespace_name {
120 DummyEnum 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 
123 const DummyEnum constNamespaceEnumInstance = DummyEnum::first;
124 } // namespace namespace_name
125 
126 namespace {
127 DummyEnum 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 ///////////////////////////////////////
132 struct DummyStruct {
133 public:
134   int structIntElement = 0;
135   const int constStructIntElement = 0;
136 
137 private:
138   int privateStructIntElement = 0;
139 };
140 
141 DummyStruct 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 
144 DummyStruct *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 
148 DummyStruct &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]
150 DummyStruct *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 
153 const DummyStruct constDummyStructInstance;
154 
155 const 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 
158 const DummyStruct *const constPointerToConstDummyStruct = &constDummyStructInstance;
159 
160 const DummyStruct &referenceToConstDummyStruct = constDummyStructInstance;
161 
162 namespace namespace_name {
163 DummyStruct 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 
166 const DummyStruct constNamespaceDummyStructInstance;
167 } // namespace namespace_name
168 
169 namespace {
170 DummyStruct 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 ////////////////////////////////////////
175 union DummyUnion {
176   int unionInteger;
177   char unionChar;
178 };
179 
180 DummyUnion nonConstUnionIntInstance = {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 
183 DummyUnion *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 
187 DummyUnion *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 
190 DummyUnion &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 
193 const DummyUnion constUnionIntInstance = {0x0};
194 
195 const 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 
198 const DummyUnion *const constPointerToConstUnionInt = &constUnionIntInstance;
199 
200 const DummyUnion &referenceToConstUnionInt = constUnionIntInstance;
201 
202 namespace namespace_name {
203 DummyUnion 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 
206 const DummyUnion constNamespaceDummyUnionInstance = {0x0};
207 } // namespace namespace_name
208 
209 namespace {
210 DummyUnion nonConstAnonymousNamespaceUnionInstance = {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 /////////////////////////////
dummyFunction()215 int dummyFunction() {
216   return 0;
217 }
218 
219 typedef int (*functionPointer)();
220 functionPointer 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 
223 typedef int (*const functionConstPointer)();
224 functionPointer 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 ////////////////////////////
228 template <class T>
229 constexpr T templateVariable = T(0L);
230 
231 // CHECKING AGAINST FALSE POSITIVES INSIDE FUNCTION SCOPE /////////////////////
main()232 int 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