[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clang-tidy / misc / UseAnonymousNamespaceCheck.cpp
bloba6337f5bab834dd666d6ea5e5d2e0f2d46f49c10
1 //===--- UseAnonymousNamespaceCheck.cpp - clang-tidy ----------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "UseAnonymousNamespaceCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers;
15 namespace clang::tidy::misc {
16 namespace {
17 AST_POLYMORPHIC_MATCHER_P(isInHeaderFile,
18 AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
19 VarDecl),
20 FileExtensionsSet, HeaderFileExtensions) {
21 return utils::isExpansionLocInHeaderFile(
22 Node.getBeginLoc(), Finder->getASTContext().getSourceManager(),
23 HeaderFileExtensions);
26 AST_MATCHER(FunctionDecl, isMemberFunction) {
27 return llvm::isa<CXXMethodDecl>(&Node);
29 AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); }
30 } // namespace
32 UseAnonymousNamespaceCheck::UseAnonymousNamespaceCheck(
33 StringRef Name, ClangTidyContext *Context)
34 : ClangTidyCheck(Name, Context) {
35 std::optional<StringRef> HeaderFileExtensionsOption =
36 Options.get("HeaderFileExtensions");
37 RawStringHeaderFileExtensions =
38 HeaderFileExtensionsOption.value_or(utils::defaultHeaderFileExtensions());
39 if (HeaderFileExtensionsOption) {
40 if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
41 HeaderFileExtensions,
42 utils::defaultFileExtensionDelimiters())) {
43 this->configurationDiag("Invalid header file extension: '%0'")
44 << RawStringHeaderFileExtensions;
46 } else
47 HeaderFileExtensions = Context->getHeaderFileExtensions();
50 void UseAnonymousNamespaceCheck::storeOptions(
51 ClangTidyOptions::OptionMap &Opts) {
52 Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
55 void UseAnonymousNamespaceCheck::registerMatchers(MatchFinder *Finder) {
56 Finder->addMatcher(
57 functionDecl(isStaticStorageClass(),
58 unless(anyOf(isInHeaderFile(HeaderFileExtensions),
59 isInAnonymousNamespace(), isMemberFunction())))
60 .bind("x"),
61 this);
62 Finder->addMatcher(
63 varDecl(isStaticStorageClass(),
64 unless(anyOf(isInHeaderFile(HeaderFileExtensions),
65 isInAnonymousNamespace(), isStaticLocal(),
66 isStaticDataMember(), hasType(isConstQualified()))))
67 .bind("x"),
68 this);
71 void UseAnonymousNamespaceCheck::check(const MatchFinder::MatchResult &Result) {
72 if (const auto *MatchedDecl = Result.Nodes.getNodeAs<NamedDecl>("x")) {
73 StringRef Type = llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function";
74 diag(MatchedDecl->getLocation(),
75 "%0 %1 declared 'static', move to anonymous namespace instead")
76 << Type << MatchedDecl;
80 } // namespace clang::tidy::misc