clang-tools
22.0.0git
llvm-project
clang-tools-extra
clang-tidy
google
UnnamedNamespaceInHeaderCheck.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
9
#include "
UnnamedNamespaceInHeaderCheck.h
"
10
#include "clang/ASTMatchers/ASTMatchFinder.h"
11
#include "clang/ASTMatchers/ASTMatchers.h"
12
13
using namespace
clang::ast_matchers
;
14
15
namespace
clang::tidy::google::build
{
16
17
UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck
(
18
StringRef Name,
ClangTidyContext
*Context)
19
:
ClangTidyCheck
(Name, Context),
20
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
21
22
void
UnnamedNamespaceInHeaderCheck::registerMatchers
(
23
ast_matchers::MatchFinder *Finder) {
24
Finder->addMatcher(namespaceDecl(isAnonymous()).bind(
"anonymousNamespace"
),
25
this
);
26
}
27
28
void
UnnamedNamespaceInHeaderCheck::check
(
29
const
MatchFinder::MatchResult &Result) {
30
const
auto
*N = Result.Nodes.getNodeAs<NamespaceDecl>(
"anonymousNamespace"
);
31
SourceLocation Loc = N->getBeginLoc();
32
if
(!Loc.isValid())
33
return
;
34
35
if
(
utils::isPresumedLocInHeaderFile
(Loc, *Result.SourceManager,
36
HeaderFileExtensions))
37
diag(Loc,
"do not use unnamed namespaces in header files"
);
38
}
39
40
}
// namespace clang::tidy::google::build
UnnamedNamespaceInHeaderCheck.h
ClangTidyCheck
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition
ClangTidyDiagnosticConsumer.h:70
clang::tidy::google::build::UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck
UnnamedNamespaceInHeaderCheck(StringRef Name, ClangTidyContext *Context)
Definition
UnnamedNamespaceInHeaderCheck.cpp:17
clang::tidy::google::build::UnnamedNamespaceInHeaderCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
Definition
UnnamedNamespaceInHeaderCheck.cpp:28
clang::tidy::google::build::UnnamedNamespaceInHeaderCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Definition
UnnamedNamespaceInHeaderCheck.cpp:22
clang::ast_matchers
Definition
AbseilMatcher.h:13
clang::tidy::google::build
Definition
ExplicitMakePairCheck.cpp:23
clang::tidy::utils::isPresumedLocInHeaderFile
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
Definition
FileExtensionsUtils.cpp:22
Generated on
for clang-tools by
1.14.0