clang-tools 17.0.0git
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13
14using namespace clang::ast_matchers;
15
17
19 StringRef Name, ClangTidyContext *Context)
20 : ClangTidyCheck(Name, Context) {
21 std::optional<StringRef> HeaderFileExtensionsOption =
22 Options.get("HeaderFileExtensions");
23 RawStringHeaderFileExtensions =
24 HeaderFileExtensionsOption.value_or(utils::defaultHeaderFileExtensions());
25 if (HeaderFileExtensionsOption) {
26 if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
27 HeaderFileExtensions,
29 this->configurationDiag("Invalid header file extension: '%0'")
30 << RawStringHeaderFileExtensions;
31 }
32 } else
33 HeaderFileExtensions = Context->getHeaderFileExtensions();
34}
35
38 Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
39}
40
42 ast_matchers::MatchFinder *Finder) {
43 Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"),
44 this);
45}
46
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 clang::tidy::google::build
SourceLocation Loc
Token Name
std::optional< StringRef > get(StringRef LocalName) const
Read a named option from the Context.
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
UnnamedNamespaceInHeaderCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
llvm::StringMap< ClangTidyValue > OptionMap