aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang14/tools/extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp
blob: ae3738aa691e440d91a4f84a11d1537195da8c69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//===--- GlobalNamesInHeadersCheck.cpp - clang-tidy -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "GlobalNamesInHeadersCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace google {
namespace readability {

GlobalNamesInHeadersCheck::GlobalNamesInHeadersCheck(StringRef Name,
                                                     ClangTidyContext *Context)
    : ClangTidyCheck(Name, Context),
      RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
          "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
  if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
                                  HeaderFileExtensions,
                                  utils::defaultFileExtensionDelimiters())) {
    this->configurationDiag("Invalid header file extension: '%0'")
        << RawStringHeaderFileExtensions;
  }
}

void GlobalNamesInHeadersCheck::storeOptions(
    ClangTidyOptions::OptionMap &Opts) {
  Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
}

void GlobalNamesInHeadersCheck::registerMatchers(
    ast_matchers::MatchFinder *Finder) {
  Finder->addMatcher(decl(anyOf(usingDecl(), usingDirectiveDecl()),
                          hasDeclContext(translationUnitDecl()))
                         .bind("using_decl"),
                     this);
}

void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
  const auto *D = Result.Nodes.getNodeAs<Decl>("using_decl");
  // If it comes from a macro, we'll assume it is fine.
  if (D->getBeginLoc().isMacroID())
    return;

  // Ignore if it comes from the "main" file ...
  if (Result.SourceManager->isInMainFile(
          Result.SourceManager->getExpansionLoc(D->getBeginLoc()))) {
    // unless that file is a header.
    if (!utils::isSpellingLocInHeaderFile(
            D->getBeginLoc(), *Result.SourceManager, HeaderFileExtensions))
      return;
  }

  if (const auto *UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
    if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
      // Anonymous namespaces inject a using directive into the AST to import
      // the names into the containing namespace.
      // We should not have them in headers, but there is another warning for
      // that.
      return;
    }
  }

  diag(D->getBeginLoc(),
       "using declarations in the global namespace in headers are prohibited");
}

} // namespace readability
} // namespace google
} // namespace tidy
} // namespace clang