[clang-tools-extra] 3e2ed57 - [clang-tidy] Exclude class/struct scope variables from cppcoreguidelines-avoid-non-const-global-variables

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 5 09:49:01 PDT 2023


Author: Piotr Zegar
Date: 2023-08-05T16:47:49Z
New Revision: 3e2ed5701b7e0a5f5b2f0d248fe82f78e0e07267

URL: https://github.com/llvm/llvm-project/commit/3e2ed5701b7e0a5f5b2f0d248fe82f78e0e07267
DIFF: https://github.com/llvm/llvm-project/commit/3e2ed5701b7e0a5f5b2f0d248fe82f78e0e07267.diff

LOG: [clang-tidy] Exclude class/struct scope variables from cppcoreguidelines-avoid-non-const-global-variables

Ignore static variables declared within the scope of class/struct.
Those variables should be covered by I.3 rule.

Fixes: #47384

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D157180

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
index db723b144c140e..ee17b0e0142882 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
@@ -15,25 +15,24 @@ using namespace clang::ast_matchers;
 
 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"),

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a88df4d9f8909b..59ec57b6db4ade 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -163,6 +163,11 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/reserved-identifier>`, so that it does not warn
   on macros starting with underscore and lowercase letter.
 
+- Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables
+  <clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables>` check
+  to ignore ``static`` variables declared within the scope of
+  ``class``/``struct``.
+
 - Improved :doc:`llvm-namespace-comment
   <clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
   ``inline`` namespaces in the same format as :program:`clang-format`.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp
index 5b1c004837e7fd..3ca1029433d229 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp
@@ -236,3 +236,17 @@ int main() {
     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 = &StructWithStatic::value;
+int& StructWithStatic::valueRef = StructWithStatic::value;
+


        


More information about the cfe-commits mailing list