clang-tools 6.0.0

UnnamedNamespaceInHeaderCheck.cpp
Go to the documentation of this file.
1 //===--- UnnamedNamespaceInHeaderCheck.cpp - clang-tidy ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "UnnamedNamespaceInHeaderCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace google {
20 namespace build {
21
22 UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck(
23 StringRef Name, ClangTidyContext *Context)
24 : ClangTidyCheck(Name, Context),
25 RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
26 "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
27 if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
28 HeaderFileExtensions, ',')) {
29 llvm::errs() << "Invalid header file extension: "
30 << RawStringHeaderFileExtensions << "\n";
31 }
32 }
33
34 void UnnamedNamespaceInHeaderCheck::storeOptions(
35 ClangTidyOptions::OptionMap &Opts) {
36 Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
37 }
38
39 void UnnamedNamespaceInHeaderCheck::registerMatchers(
40 ast_matchers::MatchFinder *Finder) {
41 // Only register the matchers for C++; the functionality currently does not
42 // provide any benefit to other languages, despite being benign.
43 if (getLangOpts().CPlusPlus)
44 Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"),
45 this);
46 }
47
48 void UnnamedNamespaceInHeaderCheck::check(
49 const MatchFinder::MatchResult &Result) {
50 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace");
51 SourceLocation Loc = N->getLocStart();
52 if (!Loc.isValid())
53 return;
54
55 if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
56 HeaderFileExtensions))
57 diag(Loc, "do not use unnamed namespaces in header files");
58 }
59
60 } // namespace build
61 } // namespace google
62 } // namespace tidy
63 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:36
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:34
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
clang::tidy::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: ClangTidy.cpp:449
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::tidy::ClangTidyCheck::getLangOpts
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:42
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:26
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:48
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: HeaderFileExtensionsUtils.h:41
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
clang::tidy::google::build::UnnamedNamespaceInHeaderCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UnnamedNamespaceInHeaderCheck.cpp:39
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:99
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:104
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: ClangTidy.cpp:416