Skip to content

Commit 020ed67

Browse files
committed
[clang-tidy] Fix check for Abseil internal namespace access
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 ([email protected]) Reviewed as https://reviews.llvm.org/D72484
1 parent 41fcd17 commit 020ed67

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

clang-tools-extra/clang-tidy/abseil/NoInternalDependenciesCheck.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ void NoInternalDependenciesCheck::check(const MatchFinder::MatchResult &Result)
3737
const auto *InternalDependency =
3838
Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("InternalDep");
3939

40-
diag(InternalDependency->getBeginLoc(),
40+
SourceLocation LocAtFault =
41+
Result.SourceManager->getSpellingLoc(InternalDependency->getBeginLoc());
42+
43+
if (!LocAtFault.isValid())
44+
return;
45+
46+
diag(LocAtFault,
4147
"do not reference any 'internal' namespaces; those implementation "
4248
"details are reserved to Abseil");
4349
}

clang-tools-extra/test/clang-tidy/checkers/Inputs/absl/strings/internal-file.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ template <class P> P InternalTemplateFunction(P a) {}
1515

1616
namespace container_internal {
1717
struct InternalStruct {};
18+
19+
template <typename T> struct InternalTemplate {};
1820
} // namespace container_internal
1921
} // namespace absl
2022

clang-tools-extra/test/clang-tidy/checkers/abseil-no-internal-dependencies.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,18 @@ std::string Str = absl::StringsFunction("a");
4444
void MacroUse() {
4545
USE_INTERNAL(Function); // no-warning
4646
USE_EXTERNAL(Function);
47-
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil
47+
// CHECK-MESSAGES: :[[@LINE-5]]:25: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil
4848
}
49+
50+
class A : absl::container_internal::InternalStruct {};
51+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil
52+
53+
template <typename T>
54+
class B : absl::container_internal::InternalTemplate<T> {};
55+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil
56+
57+
template <typename T> class C : absl::container_internal::InternalTemplate<T> {
58+
public:
59+
template <typename U> static C Make(U *p) { return C{}; }
60+
};
61+
// CHECK-MESSAGES: :[[@LINE-4]]:33: warning: do not reference any 'internal' namespaces; those implementation details are reserved to Abseil

0 commit comments

Comments
 (0)