clang-tools 10.0.0

UnnamedNamespaceInHeaderCheck.cpp
Go to the documentation of this file.
1 //===--- UnnamedNamespaceInHeaderCheck.cpp - clang-tidy ---------*- C++ -*-===//
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/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace google {
19 namespace build {
20
21 UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck(
22 StringRef Name, ClangTidyContext *Context)
23 : ClangTidyCheck(Name, Context),
24 RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
25 "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
26 if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
27 HeaderFileExtensions, ',')) {
28 llvm::errs() << "Invalid header file extension: "
29 << RawStringHeaderFileExtensions << "\n";
30 }
31 }
32
33 void UnnamedNamespaceInHeaderCheck::storeOptions(
34 ClangTidyOptions::OptionMap &Opts) {
35 Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
36 }
37
38 void UnnamedNamespaceInHeaderCheck::registerMatchers(
39 ast_matchers::MatchFinder *Finder) {
40 // Only register the matchers for C++; the functionality currently does not
41 // provide any benefit to other languages, despite being benign.
42 if (getLangOpts().CPlusPlus)
43 Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"),
44 this);
45 }
46
47 void UnnamedNamespaceInHeaderCheck::check(
48 const MatchFinder::MatchResult &Result) {
49 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace");
50 SourceLocation Loc = N->getBeginLoc();
51 if (!Loc.isValid())
52 return;
53
54 if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
55 HeaderFileExtensions))
56 diag(Loc, "do not use unnamed namespaces in header files");
57 }
58
59 } // namespace build
60 } // namespace google
61 } // namespace tidy
62 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::tidy::google::build::UnnamedNamespaceInHeaderCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: UnnamedNamespaceInHeaderCheck.cpp:33
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:41
clang::tidy::utils::isPresumedLocInHeaderFile
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
Definition: HeaderFileExtensionsUtils.cpp:25
clang::tidy::google::build::UnnamedNamespaceInHeaderCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UnnamedNamespaceInHeaderCheck.cpp:47
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: HeaderFileExtensionsUtils.h:40
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::google::build::UnnamedNamespaceInHeaderCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UnnamedNamespaceInHeaderCheck.cpp:38
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::isAnonymous
static bool isAnonymous(const DeclarationName &N)
Definition: AST.cpp:184
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
UnnamedNamespaceInHeaderCheck.h
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21