clang-tools  14.0.0git
NoInternalDependenciesCheck.cpp
Go to the documentation of this file.
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 
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 
20 void NoInternalDependenciesCheck::registerMatchers(MatchFinder *Finder) {
21  // TODO: refactor matcher to be configurable or just match on any internal
22  // access from outside the enclosing namespace.
23 
24  Finder->addMatcher(
25  nestedNameSpecifierLoc(loc(specifiesNamespace(namespaceDecl(
26  matchesName("internal"),
27  hasParent(namespaceDecl(hasName("absl")))))),
28  unless(isInAbseilFile()))
29  .bind("InternalDep"),
30  this);
31 }
32 
33 void NoInternalDependenciesCheck::check(const MatchFinder::MatchResult &Result) {
34  const auto *InternalDependency =
35  Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("InternalDep");
36 
37  SourceLocation LocAtFault =
38  Result.SourceManager->getSpellingLoc(InternalDependency->getBeginLoc());
39 
40  if (!LocAtFault.isValid())
41  return;
42 
43  diag(LocAtFault,
44  "do not reference any 'internal' namespaces; those implementation "
45  "details are reserved to Abseil");
46 }
47 
48 } // namespace abseil
49 } // namespace tidy
50 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:258
AbseilMatcher.h
NoInternalDependenciesCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27