• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- NoNamespaceCheck.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 //===----------------------------------------------------------------------===//
8 
9 #include "NoNamespaceCheck.h"
10 #include "AbseilMatcher.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace abseil {
19 
registerMatchers(MatchFinder * Finder)20 void NoNamespaceCheck::registerMatchers(MatchFinder *Finder) {
21   Finder->addMatcher(
22       namespaceDecl(hasName("::absl"), unless(isInAbseilFile()))
23           .bind("abslNamespace"),
24       this);
25 }
26 
check(const MatchFinder::MatchResult & Result)27 void NoNamespaceCheck::check(const MatchFinder::MatchResult &Result) {
28   const auto *abslNamespaceDecl =
29       Result.Nodes.getNodeAs<NamespaceDecl>("abslNamespace");
30 
31   diag(abslNamespaceDecl->getLocation(),
32        "namespace 'absl' is reserved for implementation of the Abseil library "
33        "and should not be opened in user code");
34 }
35 
36 } // namespace abseil
37 } // namespace tidy
38 } // namespace clang
39