Fix false positive on anonymous namespaces in headers.

Summary:
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.

Reviewers: djasper

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D8443

llvm-svn: 233087
This commit is contained in:
Samuel Benzaquen 2015-03-24 15:21:45 +00:00
parent c676f2a8bb
commit 3199b9a8b5
2 changed files with 14 additions and 0 deletions

View File

@ -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");
}

View File

@ -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