xref: /llvm-project/clang-tools-extra/clang-tidy/abseil/NoNamespaceCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
1d2f7b04dSHaojian Wu //===--- NoNamespaceCheck.cpp - clang-tidy---------------------------------===//
2d2f7b04dSHaojian Wu //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d2f7b04dSHaojian Wu //
7d2f7b04dSHaojian Wu //===----------------------------------------------------------------------===//
8d2f7b04dSHaojian Wu 
9d2f7b04dSHaojian Wu #include "NoNamespaceCheck.h"
10d2f7b04dSHaojian Wu #include "AbseilMatcher.h"
11d2f7b04dSHaojian Wu #include "clang/AST/ASTContext.h"
12d2f7b04dSHaojian Wu #include "clang/ASTMatchers/ASTMatchFinder.h"
13d2f7b04dSHaojian Wu 
14d2f7b04dSHaojian Wu using namespace clang::ast_matchers;
15d2f7b04dSHaojian Wu 
16*7d2ea6c4SCarlos Galvez namespace clang::tidy::abseil {
17d2f7b04dSHaojian Wu 
registerMatchers(MatchFinder * Finder)18d2f7b04dSHaojian Wu void NoNamespaceCheck::registerMatchers(MatchFinder *Finder) {
19d2f7b04dSHaojian Wu   Finder->addMatcher(
20d2f7b04dSHaojian Wu       namespaceDecl(hasName("::absl"), unless(isInAbseilFile()))
21d2f7b04dSHaojian Wu           .bind("abslNamespace"),
22d2f7b04dSHaojian Wu       this);
23d2f7b04dSHaojian Wu }
24d2f7b04dSHaojian Wu 
check(const MatchFinder::MatchResult & Result)25d2f7b04dSHaojian Wu void NoNamespaceCheck::check(const MatchFinder::MatchResult &Result) {
26ab2d3ce4SAlexander Kornienko   const auto *AbslNamespaceDecl =
27d2f7b04dSHaojian Wu       Result.Nodes.getNodeAs<NamespaceDecl>("abslNamespace");
28d2f7b04dSHaojian Wu 
29ab2d3ce4SAlexander Kornienko   diag(AbslNamespaceDecl->getLocation(),
30d2f7b04dSHaojian Wu        "namespace 'absl' is reserved for implementation of the Abseil library "
31d2f7b04dSHaojian Wu        "and should not be opened in user code");
32d2f7b04dSHaojian Wu }
33d2f7b04dSHaojian Wu 
34*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::abseil
35