NoInternalDependenciesCheck.cpp 1.6 KB

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