[clang-tools-extra] 7d1ee63 - [clangd] Fix a crash for accessing a null template decl returned by findExplicitReferences.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 22 12:00:39 PDT 2020
Author: Haojian Wu
Date: 2020-04-22T21:00:20+02:00
New Revision: 7d1ee639cb9efea364bec90afe4d1161ec624a7f
URL: https://github.com/llvm/llvm-project/commit/7d1ee639cb9efea364bec90afe4d1161ec624a7f
DIFF: https://github.com/llvm/llvm-project/commit/7d1ee639cb9efea364bec90afe4d1161ec624a7f.diff
LOG: [clangd] Fix a crash for accessing a null template decl returned by findExplicitReferences.
Summary: Fixes https://github.com/clangd/clangd/issues/347.
Reviewers: kadircet
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D78626
Added:
Modified:
clang-tools-extra/clangd/FindTarget.cpp
clang-tools-extra/clangd/unittests/FindTargetTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp
index 1ff20e6e4bc1..be3306f3fc78 100644
--- a/clang-tools-extra/clangd/FindTarget.cpp
+++ b/clang-tools-extra/clangd/FindTarget.cpp
@@ -860,15 +860,17 @@ class ExplicitReferenceCollector
// TemplateArgumentLoc is the only way to get locations for references to
// template template parameters.
bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
+ llvm::SmallVector<const NamedDecl *, 1> Targets;
switch (A.getArgument().getKind()) {
case TemplateArgument::Template:
case TemplateArgument::TemplateExpansion:
+ if (const auto *D = A.getArgument()
+ .getAsTemplateOrTemplatePattern()
+ .getAsTemplateDecl())
+ Targets.push_back(D);
reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
A.getTemplateNameLoc(),
- /*IsDecl=*/false,
- {A.getArgument()
- .getAsTemplateOrTemplatePattern()
- .getAsTemplateDecl()}},
+ /*IsDecl=*/false, Targets},
DynTypedNode::create(A.getArgument()));
break;
case TemplateArgument::Declaration:
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index f943772e052e..03cdc02fbb18 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -1286,6 +1286,20 @@ TEST_F(FindExplicitReferencesTest, All) {
"1: targets = {}\n"
"2: targets = {T}\n"
},
+ // unknown template name should not crash.
+ {R"cpp(
+ template <template <typename> typename T>
+ struct Base {};
+ namespace foo {
+ template <typename $0^T>
+ struct $1^Derive : $2^Base<$3^T::template $4^Unknown> {};
+ }
+ )cpp",
+ "0: targets = {foo::Derive::T}, decl\n"
+ "1: targets = {foo::Derive}, decl\n"
+ "2: targets = {Base}\n"
+ "3: targets = {foo::Derive::T}\n"
+ "4: targets = {}, qualifier = 'T::'\n"},
};
for (const auto &C : Cases) {
More information about the cfe-commits
mailing list