[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 07:03:48 PST 2022


carlosgalvezp updated this revision to Diff 479284.
carlosgalvezp added a comment.

Fix naming convention


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139113/new/

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 v8;
 };
 
 // OK
 void foo()
 {
-  static int x;
+  static int v9;
 }
+
+// OK
+static const int v10{123};
+static constexpr int v11{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 gv{123};
+static void gf(){}
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.479284.patch
Type: text/x-patch
Size: 2558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221201/60ec3ef4/attachment.bin>


More information about the cfe-commits mailing list