[PATCH] D157180: [clang-tidy] Exclude class/struct scope variables from cppcoreguidelines-avoid-non-const-global-variables
This revision was automatically updated to reflect the committed changes. Closed by commit rG3e2ed5701b7e: [clang-tidy] Exclude class/struct scope variables from cppcoreguidelines-avoid… (authored by PiotrZSL). Changed prior to commit: https://reviews.llvm.org/D157180?vs=547457=547499#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157180/new/ https://reviews.llvm.org/D157180 Files: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp === --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp @@ -236,3 +236,17 @@ nonConstInt = nonConstLoopVariable + i + staticNonConstLoopVariable; } } + +// CHECKING AGAINST FALSE POSITIVES INSIDE STRUCT SCOPE / +struct StructWithStatic { + static DummyStruct nonConstDummyStructInstance; + static int value; + static int* valuePtr; + static int& valueRef; +}; + +DummyStruct StructWithStatic::nonConstDummyStructInstance; +int StructWithStatic::value = 0; +int* StructWithStatic::valuePtr = ::value; +int& StructWithStatic::valueRef = StructWithStatic::value; + Index: clang-tools-extra/docs/ReleaseNotes.rst === --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -163,6 +163,11 @@ `, so that it does not warn on macros starting with underscore and lowercase letter. +- Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables + ` check + to ignore ``static`` variables declared within the scope of + ``class``/``struct``. + - Improved :doc:`llvm-namespace-comment ` check to provide fixes for ``inline`` namespaces in the same format as :program:`clang-format`. Index: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp === --- clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp +++ clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp @@ -15,25 +15,24 @@ namespace clang::tidy::cppcoreguidelines { -namespace { -AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); } -} // namespace - void AvoidNonConstGlobalVariablesCheck::registerMatchers(MatchFinder *Finder) { + auto GlobalContext = + varDecl(hasGlobalStorage(), + hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl(; + auto GlobalVariable = varDecl( - hasGlobalStorage(), + GlobalContext, unless(anyOf( - isLocalVarDecl(), isConstexpr(), hasType(isConstQualified()), + isConstexpr(), hasType(isConstQualified()), hasType(referenceType(); // References can't be changed, only the // data they reference can be changed. auto GlobalReferenceToNonConst = - varDecl(hasGlobalStorage(), hasType(referenceType()), + varDecl(GlobalContext, hasType(referenceType()), unless(hasType(references(qualType(isConstQualified()); - auto GlobalPointerToNonConst = - varDecl(hasGlobalStorage(), - hasType(pointerType(pointee(unless(isConstQualified()); + auto GlobalPointerToNonConst = varDecl( + GlobalContext, hasType(pointerType(pointee(unless(isConstQualified()); Finder->addMatcher(GlobalVariable.bind("non-const_variable"), this); Finder->addMatcher(GlobalReferenceToNonConst.bind("indirection_to_non-const"), Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp === --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp @@ -236,3 +236,17 @@ nonConstInt = nonConstLoopVariable + i + staticNonConstLoopVariable; } } + +// CHECKING AGAINST FALSE POSITIVES INSIDE STRUCT SCOPE / +struct StructWithStatic { + static DummyStruct nonConstDummyStructInstance; + static int value; + static int* valuePtr; + static int& valueRef; +}; + +DummyStruct StructWithStatic::nonConstDummyStructInstance; +int StructWithStatic::value = 0; +int* StructWithStatic::valuePtr = ::value; +int& StructWithStatic::valueRef = StructWithStatic::value; + Index: clang-tools-extra/docs/ReleaseNotes.rst
[PATCH] D157180: [clang-tidy] Exclude class/struct scope variables from cppcoreguidelines-avoid-non-const-global-variables
carlosgalvezp accepted this revision. carlosgalvezp added a comment. This revision is now accepted and ready to land. LGTM! Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157180/new/ https://reviews.llvm.org/D157180 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D157180: [clang-tidy] Exclude class/struct scope variables from cppcoreguidelines-avoid-non-const-global-variables
PiotrZSL created this revision. PiotrZSL added reviewers: njames93, carlosgalvezp, ccotter. Herald added subscribers: shchenz, kbarton, xazax.hun, nemanjai. Herald added a project: All. PiotrZSL requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits. Ignore static variables declared within the scope of class/struct. Those variables should be covered by I.3 rule. Fixes: #47384 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D157180 Files: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp === --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp @@ -236,3 +236,17 @@ nonConstInt = nonConstLoopVariable + i + staticNonConstLoopVariable; } } + +// CHECKING AGAINST FALSE POSITIVES INSIDE STRUCT SCOPE / +struct StructWithStatic { + static DummyStruct nonConstDummyStructInstance; + static int value; + static int* valuePtr; + static int& valueRef; +}; + +DummyStruct StructWithStatic::nonConstDummyStructInstance; +int StructWithStatic::value = 0; +int* StructWithStatic::valuePtr = ::value; +int& StructWithStatic::valueRef = StructWithStatic::value; + Index: clang-tools-extra/docs/ReleaseNotes.rst === --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -163,6 +163,11 @@ `, so that it does not warn on macros starting with underscore and lowercase letter. +- Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables + ` check + to ignore ``static`` variables declared within the scope of + ``class``/``struct``. + Removed checks ^^ Index: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp === --- clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp +++ clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp @@ -15,25 +15,24 @@ namespace clang::tidy::cppcoreguidelines { -namespace { -AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); } -} // namespace - void AvoidNonConstGlobalVariablesCheck::registerMatchers(MatchFinder *Finder) { + auto GlobalContext = + varDecl(hasGlobalStorage(), + hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl(; + auto GlobalVariable = varDecl( - hasGlobalStorage(), + GlobalContext, unless(anyOf( - isLocalVarDecl(), isConstexpr(), hasType(isConstQualified()), + isConstexpr(), hasType(isConstQualified()), hasType(referenceType(); // References can't be changed, only the // data they reference can be changed. auto GlobalReferenceToNonConst = - varDecl(hasGlobalStorage(), hasType(referenceType()), + varDecl(GlobalContext, hasType(referenceType()), unless(hasType(references(qualType(isConstQualified()); - auto GlobalPointerToNonConst = - varDecl(hasGlobalStorage(), - hasType(pointerType(pointee(unless(isConstQualified()); + auto GlobalPointerToNonConst = varDecl( + GlobalContext, hasType(pointerType(pointee(unless(isConstQualified()); Finder->addMatcher(GlobalVariable.bind("non-const_variable"), this); Finder->addMatcher(GlobalReferenceToNonConst.bind("indirection_to_non-const"), Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp === --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp @@ -236,3 +236,17 @@ nonConstInt = nonConstLoopVariable + i + staticNonConstLoopVariable; } } + +// CHECKING AGAINST FALSE POSITIVES INSIDE STRUCT SCOPE / +struct StructWithStatic { + static DummyStruct nonConstDummyStructInstance; + static int value; + static int* valuePtr; + static int& valueRef; +}; + +DummyStruct StructWithStatic::nonConstDummyStructInstance; +int StructWithStatic::value = 0; +int* StructWithStatic::valuePtr = ::value; +int& StructWithStatic::valueRef = StructWithStatic::value; + Index: clang-tools-extra/docs/ReleaseNotes.rst