| 1 | //===--- NoInternalDependenciesCheck.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 "NoInternalDependenciesCheck.h" |
| 10 | #include "AbseilMatcher.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang::tidy::abseil { |
| 16 | |
| 17 | void NoInternalDependenciesCheck::registerMatchers(MatchFinder *Finder) { |
| 18 | // TODO: refactor matcher to be configurable or just match on any internal |
| 19 | // access from outside the enclosing namespace. |
| 20 | |
| 21 | Finder->addMatcher( |
| 22 | NodeMatch: nestedNameSpecifierLoc(loc(InnerMatcher: specifiesNamespace(InnerMatcher: namespaceDecl( |
| 23 | matchesName(RegExp: "internal"), |
| 24 | hasParent(namespaceDecl(hasName(Name: "absl")))))), |
| 25 | unless(isInAbseilFile())) |
| 26 | .bind(ID: "InternalDep"), |
| 27 | Action: this); |
| 28 | } |
| 29 | |
| 30 | void NoInternalDependenciesCheck::check( |
| 31 | const MatchFinder::MatchResult &Result) { |
| 32 | const auto *InternalDependency = |
| 33 | Result.Nodes.getNodeAs<NestedNameSpecifierLoc>(ID: "InternalDep"); |
| 34 | |
| 35 | SourceLocation LocAtFault = |
| 36 | Result.SourceManager->getSpellingLoc(Loc: InternalDependency->getBeginLoc()); |
| 37 | |
| 38 | if (!LocAtFault.isValid()) |
| 39 | return; |
| 40 | |
| 41 | diag(Loc: LocAtFault, |
| 42 | Description: "do not reference any 'internal' namespaces; those implementation " |
| 43 | "details are reserved to Abseil"); |
| 44 | } |
| 45 | |
| 46 | } // namespace clang::tidy::abseil |
| 47 |
