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