[PATCH] D139113: Fix a couple additional cases in misc-use-anonymous-namespace only
Carlos Galvez via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 1 06:59:34 PST 2022
carlosgalvezp created this revision.
Herald added a reviewer: njames93.
Herald added a project: All.
carlosgalvezp requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
- Do not analyze header files, since we don't want to promote using anonymous namespaces there.
- Do not warn about const/constexpr variables, those are implicitly static and they don't need to be moved to an anonymous namespace. Warning about redundant static in general could be implemented as a standalone check, moving away some of the functionality from this check.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D139113
Files:
clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/use-anonymous-namespace.h
clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp
@@ -1,4 +1,5 @@
-// RUN: %check_clang_tidy %s misc-use-anonymous-namespace %t
+// RUN: %check_clang_tidy %s misc-use-anonymous-namespace %t -- -header-filter=.* -- -I%S/Inputs
+#include "use-anonymous-namespace.h"
static void f1();
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'f1' declared 'static', move to anonymous namespace instead [misc-use-anonymous-namespace]
@@ -48,12 +49,16 @@
// OK
struct Foo {
- static void f();
- static int x;
+ static void f8();
+ static int x8;
};
// OK
void foo()
{
- static int x;
+ static int x9;
}
+
+// OK
+static const int x10{123};
+static constexpr int x11{123};
Index: clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/use-anonymous-namespace.h
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/use-anonymous-namespace.h
@@ -0,0 +1,3 @@
+// Should not warn here, require anonymous namespaces only in source files
+static int g1{123};
+static void g2(){}
Index: clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
@@ -39,6 +39,11 @@
template <typename T>
void UseAnonymousNamespaceCheck::processMatch(const T *MatchedDecl) {
+ // Enforce anonymous namespaces only in source files, not headers
+ const SourceManager &SM = MatchedDecl->getASTContext().getSourceManager();
+ if (!SM.isWrittenInMainFile(MatchedDecl->getLocation()))
+ return;
+
StringRef Type = llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function";
if (isInAnonymousNamespace(MatchedDecl))
diag(MatchedDecl->getLocation(), "%0 %1 declared 'static' in "
@@ -54,7 +59,8 @@
Finder->addMatcher(
functionDecl(isStatic(), unless(isMemberFunction())).bind("func"), this);
Finder->addMatcher(
- varDecl(isStatic(), unless(anyOf(isStaticLocal(), isStaticDataMember())))
+ varDecl(isStatic(), unless(anyOf(isStaticLocal(), isStaticDataMember(),
+ hasType(isConstQualified()))))
.bind("var"),
this);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139113.479280.patch
Type: text/x-patch
Size: 2558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221201/79a8e5c0/attachment-0001.bin>
More information about the cfe-commits
mailing list