10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
20 void ImplementationInNamespaceCheck::registerMatchers(MatchFinder *Finder) {
22 decl(hasParent(translationUnitDecl()), unless(linkageSpecDecl()))
23 .bind(
"child_of_translation_unit"),
27 void ImplementationInNamespaceCheck::check(
28 const MatchFinder::MatchResult &Result) {
29 const auto *MatchedDecl =
30 Result.Nodes.getNodeAs<
Decl>(
"child_of_translation_unit");
31 if (!Result.SourceManager->isInMainFile(MatchedDecl->getLocation()))
34 if (
const auto *NS = dyn_cast<NamespaceDecl>(MatchedDecl)) {
36 diag(NS->getLocation(),
"'%0' needs to be the outermost namespace")
41 diag(MatchedDecl->getLocation(),
42 "declaration must be declared within the '%0' namespace")