Author: Carlos Galvez
Date: 2022-12-08T11:40:50Z
New Revision: 7fd8387917167d9cb4bab14a8548f0f78b0eaa79

URL: 
https://github.com/llvm/llvm-project/commit/7fd8387917167d9cb4bab14a8548f0f78b0eaa79
DIFF: 
https://github.com/llvm/llvm-project/commit/7fd8387917167d9cb4bab14a8548f0f78b0eaa79.diff

LOG: [clang-tidy] Do not warn about redundant static in 
misc-use-anonymous-namespace

The same functionality is already implemented in the
readability-static-definition-in-anonymous-namespace
check, including automatic fixes.

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
    clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h
    clang-tools-extra/docs/clang-tidy/checks/misc/use-anonymous-namespace.rst
    clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
index 5632347f99948..878cba31eb2ab 100644
--- a/clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
@@ -25,36 +25,29 @@ AST_MATCHER(FunctionDecl, isMemberFunction) {
   return llvm::isa<CXXMethodDecl>(&Node);
 }
 AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); }
-} // namespace
 
-static bool isInAnonymousNamespace(const Decl *Decl) {
-  const DeclContext *DC = Decl->getDeclContext();
-  if (DC && DC->isNamespace()) {
-    const auto *ND = llvm::cast<NamespaceDecl>(DC);
-    if (ND && ND->isAnonymousNamespace())
-      return true;
-  }
-  return false;
+AST_MATCHER(Decl, isInAnonymousNamespace) {
+  return Node.isInAnonymousNamespace();
 }
+} // namespace
 
 template <typename T>
 void UseAnonymousNamespaceCheck::processMatch(const T *MatchedDecl) {
   StringRef Type = llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function";
-  if (isInAnonymousNamespace(MatchedDecl))
-    diag(MatchedDecl->getLocation(), "%0 %1 declared 'static' in "
-                                     "anonymous namespace, remove 'static'")
-        << Type << MatchedDecl;
-  else
-    diag(MatchedDecl->getLocation(),
-         "%0 %1 declared 'static', move to anonymous namespace instead")
-        << Type << MatchedDecl;
+  diag(MatchedDecl->getLocation(),
+       "%0 %1 declared 'static', move to anonymous namespace instead")
+      << Type << MatchedDecl;
 }
 
 void UseAnonymousNamespaceCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
-      functionDecl(isStatic(), unless(isMemberFunction())).bind("func"), this);
+      functionDecl(isStatic(),
+                   unless(anyOf(isInAnonymousNamespace(), isMemberFunction())))
+          .bind("func"),
+      this);
   Finder->addMatcher(
-      varDecl(isStatic(), unless(anyOf(isStaticLocal(), isStaticDataMember())))
+      varDecl(isStatic(), unless(anyOf(isInAnonymousNamespace(),
+                                       isStaticLocal(), isStaticDataMember())))
           .bind("var"),
       this);
 }

diff  --git a/clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h 
b/clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h
index 8e934080592e0..59c48029711e0 100644
--- a/clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h
@@ -16,8 +16,7 @@ namespace tidy {
 namespace misc {
 
 /// Warns when using 'static' functions or variables at global scope, and
-/// suggests moving them to an anonymous namespace. It also suggests removing
-/// 'static' if they are already inside an anonymous namespace.
+/// suggests moving them to an anonymous namespace.
 ///
 /// For the user-facing documentation see:
 /// 
http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-anonymous-namespace.html

diff  --git 
a/clang-tools-extra/docs/clang-tidy/checks/misc/use-anonymous-namespace.rst 
b/clang-tools-extra/docs/clang-tidy/checks/misc/use-anonymous-namespace.rst
index 5b6790b1e1b81..c116bfbdd3325 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/misc/use-anonymous-namespace.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/misc/use-anonymous-namespace.rst
@@ -4,9 +4,7 @@ misc-use-anonymous-namespace
 ============================
 
 Finds instances of ``static`` functions or variables declared at global scope
-that could instead be moved into an anonymous namespace. It also detects
-instances moved to an anonymous namespace that still keep the redundant
-``static``.
+that could instead be moved into an anonymous namespace.
 
 Anonymous namespaces are the "superior alternative" according to the C++
 Standard. ``static`` was proposed for deprecation, but later un-deprecated to
@@ -27,18 +25,4 @@ Examples:
     int x;
   } // namespace
 
-.. code-block:: c++
-
-  // Bad
-  namespace {
-    static void foo();
-    static int x;
-  }
-
-  // Good
-  namespace {
-    void foo();
-    int x;
-  }  // namespace
-
 [1] `Undeprecating static 
<https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1012>`_

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp
index a89d7823eb950..966310eef7f7c 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp
@@ -5,13 +5,6 @@ static void f1();
 static int v1;
 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'v1' declared 'static', 
move to anonymous namespace instead
 
-namespace {
-  static void f2();
-  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function 'f2' declared 'static' 
in anonymous namespace, remove 'static'
-  static int v2;
-  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'v2' declared 'static' 
in anonymous namespace, remove 'static'
-}
-
 namespace a {
   static void f3();
   // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function 'f3' declared 
'static', move to anonymous namespace instead
@@ -19,15 +12,6 @@ namespace a {
   // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'v3' declared 
'static', move to anonymous namespace instead
 }
 
-namespace a {
-namespace {
-  static void f4();
-  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: function 'f4' declared 'static' 
in anonymous namespace, remove 'static'
-  static int v4;
-  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'v4' declared 'static' 
in anonymous namespace, remove 'static'
-}
-}
-
 // OK
 void f5();
 int v5;


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to