clang-tools 23.0.0git
AnonymousNamespaceInHeaderCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13
14using namespace clang::ast_matchers;
15
17
21
23 ast_matchers::MatchFinder *Finder) {
24 Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"),
25 this);
26}
27
29 const MatchFinder::MatchResult &Result) {
30 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace");
31 const SourceLocation Loc = N->getBeginLoc();
32 if (!Loc.isValid())
33 return;
34
35 if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
36 getHeaderFileExtensions()))
37 diag(Loc, "do not use unnamed namespaces in header files");
38}
39
40} // namespace clang::tidy::misc
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
AnonymousNamespaceInHeaderCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.