summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp10
-rw-r--r--clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp4
2 files changed, 14 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp
index 09493ff6f18..5f7c1cd49e1 100644
--- a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp
@@ -45,6 +45,16 @@ void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
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");
}
diff --git a/clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp b/clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp
index 806eecd7985..feb7ae913f9 100644
--- a/clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp
@@ -101,6 +101,10 @@ TEST_F(GlobalNamesInHeadersCheckTest, UsingDirectives) {
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
OpenPOWER on IntegriCloud