[PATCH] Fix false positive on anonymous namespaces in headers.

Samuel Benzaquen sbenza at google.com
Tue Mar 24 08:24:23 PDT 2015


REPOSITORY
  rL LLVM

http://reviews.llvm.org/D8443

Files:
  clang-tools-extra/trunk/clang-tidy/google/GlobalNamesInHeadersCheck.cpp
  clang-tools-extra/trunk/unittests/clang-tidy/GoogleModuleTest.cpp

Index: clang-tools-extra/trunk/clang-tidy/google/GlobalNamesInHeadersCheck.cpp
===================================================================
--- clang-tools-extra/trunk/clang-tidy/google/GlobalNamesInHeadersCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/google/GlobalNamesInHeadersCheck.cpp
@@ -45,6 +45,16 @@
       return;
   }
 
+  if (const auto* UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
+    if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
+      // Anynoumous namespaces inject a using directive into the AST to import
+      // the names into the containing namespace.
+      // We should not have them in headers, but there is another warning for
+      // that.
+      return;
+    }
+  }
+
   diag(D->getLocStart(),
        "using declarations in the global namespace in headers are prohibited");
 }
Index: clang-tools-extra/trunk/unittests/clang-tidy/GoogleModuleTest.cpp
===================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/GoogleModuleTest.cpp
+++ clang-tools-extra/trunk/unittests/clang-tidy/GoogleModuleTest.cpp
@@ -101,6 +101,10 @@
   EXPECT_FALSE(runCheckOnCode("SOME_MACRO(namespace std);", "foo.h"));
 }
 
+TEST_F(GlobalNamesInHeadersCheckTest, RegressionAnonymousNamespace) {
+  EXPECT_FALSE(runCheckOnCode("namespace {}", "foo.h"));
+}
+
 } // namespace test
 } // namespace tidy
 } // namespace clang

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8443.22577.patch
Type: text/x-patch
Size: 1429 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150324/dc67801b/attachment.bin>


More information about the cfe-commits mailing list