aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/llvmlibc/ImplementationInNamespaceCheck.cpp
blob: c2fbc4422e5d2acbaf5824961dd29dfdd501a60a (plain)
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
//===--- ImplementationInNamespaceCheck.cpp - clang-tidy ------------------===//
//
// 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 "ImplementationInNamespaceCheck.h"
#include "NamespaceConstants.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

using namespace clang::ast_matchers;

namespace clang::tidy::llvm_libc {

void ImplementationInNamespaceCheck::registerMatchers(MatchFinder *Finder) {
  Finder->addMatcher(
      translationUnitDecl(
          forEach(decl(isExpansionInMainFile(), unless(linkageSpecDecl()),
                       // anonymous namespaces generate usingDirective
                       unless(usingDirectiveDecl(isImplicit())))
                      .bind("child_of_translation_unit"))),
      this);
}

void ImplementationInNamespaceCheck::check(
    const MatchFinder::MatchResult &Result) {
  const auto *MatchedDecl =
      Result.Nodes.getNodeAs<Decl>("child_of_translation_unit");
  const auto *NS = dyn_cast<NamespaceDecl>(MatchedDecl);

  // LLVM libc declarations should be inside of a non-anonymous namespace.
  if (NS == nullptr || NS->isAnonymousNamespace()) {
    diag(MatchedDecl->getLocation(),
         "declaration must be enclosed within the '%0' namespace")
        << RequiredNamespaceDeclMacroName;
    return;
  }

  // Enforce that the namespace is the result of macro expansion
  if (Result.SourceManager->isMacroBodyExpansion(NS->getLocation()) == false) {
    diag(NS->getLocation(), "the outermost namespace should be the '%0' macro")
        << RequiredNamespaceDeclMacroName;
    return;
  }

  // We want the macro to have [[gnu::visibility("hidden")]] as a prefix, but
  // visibility is just an attribute in the AST construct, so we check that
  // instead.
  if (NS->getVisibility() != Visibility::HiddenVisibility) {
    diag(NS->getLocation(), "the '%0' macro should start with '%1'")
        << RequiredNamespaceDeclMacroName << RequiredNamespaceDeclStart;
    return;
  }

  // Lastly, make sure the namespace name actually has the __llvm_libc prefix
  if (NS->getName().starts_with(RequiredNamespaceRefStart) == false) {
    diag(NS->getLocation(), "the '%0' macro expansion should start with '%1'")
        << RequiredNamespaceDeclMacroName << RequiredNamespaceRefStart;
    return;
  }
}

} // namespace clang::tidy::llvm_libc