[PATCH] D157180: [clang-tidy] Exclude class/struct scope variables from cppcoreguidelines-avoid-non-const-global-variables
Piotr Zegar via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 5 00:24:20 PDT 2023
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 = &StructWithStatic::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 @@
<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``.
+
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"),
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157180.547457.patch
Type: text/x-patch
Size: 3351 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230805/2a8d31b0/attachment.bin>
More information about the cfe-commits
mailing list