[clang-tidy] Fix check for Abseil internal namespace access
authorEric Fiselier <eric@efcs.ca>
Tue, 21 Jan 2020 20:10:47 +0000 (15:10 -0500)
committerEric Fiselier <eric@efcs.ca>
Tue, 21 Jan 2020 20:21:53 +0000 (15:21 -0500)
This change makes following modifications:
  * If reference originated from macro expansion, we report location inside of
    the macro instead of location where macro is referenced.
  * If for any reason deduced location is not correct we silently ignore it.

Patch by Gennadiy Rozental (rogeeff@google.com)
Reviewed as https://reviews.llvm.org/D72484

clang-tools-extra/clang-tidy/abseil/NoInternalDependenciesCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/Inputs/absl/strings/internal-file.h
clang-tools-extra/test/clang-tidy/checkers/abseil-no-internal-dependencies.cpp

index dcb8585d55a973874aafc8a67ac8436d4c2ce889..3ce937a756494b3a1abca1c56c07e341a3126acb 100644 (file)
@@ -37,7 +37,13 @@ void NoInternalDependenciesCheck::check(const MatchFinder::MatchResult &Result)
   const auto *InternalDependency =
       Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("InternalDep");
 
-  diag(InternalDependency->getBeginLoc(),
+  SourceLocation LocAtFault =
+      Result.SourceManager->getSpellingLoc(InternalDependency->getBeginLoc());
+
+  if (!LocAtFault.isValid())
+    return;
+
+  diag(LocAtFault,
        "do not reference any 'internal' namespaces; those implementation "
        "details are reserved to Abseil");
 }
index 6014278e26060c7168b2ec411610721d5778edfe..31798661a80fc5ce3d33803cacb0aa884eeb8bc4 100644 (file)
@@ -15,6 +15,8 @@ template <class P> P InternalTemplateFunction(P a) {}
 
 namespace container_internal {
 struct InternalStruct {};
+
+template <typename T> struct InternalTemplate {};
 } // namespace container_internal
 } // namespace absl
 
index 272d0060bdb7c583ddce079dba9846bfd8cda8c1..2949d7fdd0274bf17acab6a3c85a3f8fc3bb53f5 100644 (file)
@@ -44,5 +44,18 @@ std::string Str = absl::StringsFunction("a");
 void MacroUse() {
   USE_INTERNAL(Function); // no-warning
   USE_EXTERNAL(Function);
-  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil
+  // CHECK-MESSAGES: :[[@LINE-5]]:25: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil
 }
+
+class A : absl::container_internal::InternalStruct {};
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil
+
+template <typename T>
+class B : absl::container_internal::InternalTemplate<T> {};
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil
+
+template <typename T> class C : absl::container_internal::InternalTemplate<T> {
+public:
+  template <typename U> static C Make(U *p) { return C{}; }
+};
+// CHECK-MESSAGES: :[[@LINE-4]]:33: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil