From: Haojian Wu Date: Tue, 14 Apr 2020 22:16:10 +0000 (+0200) Subject: [clangd] Fix a crash for accessing a null field decl returned by findExplicitReferences. X-Git-Tag: llvmorg-12-init~8923 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6a78c55e3af0fc98513bf7e0b76b3b88719ca2c3;p=platform%2Fupstream%2Fllvm.git [clangd] Fix a crash for accessing a null field decl returned by findExplicitReferences. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D78181 --- diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp index 27f6532..1ff20e6 100644 --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -714,10 +714,12 @@ llvm::SmallVector refInExpr(const Expr *E) { for (const DesignatedInitExpr::Designator &D : DIE->designators()) { if (!D.isFieldDesignator()) continue; - Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), - D.getFieldLoc(), - /*IsDecl=*/false, - {D.getField()}}); + + llvm::SmallVector Targets; + if (D.getField()) + Targets.push_back(D.getField()); + Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), D.getFieldLoc(), + /*IsDecl=*/false, std::move(Targets)}); } } }; diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp index beb81a7..f943772 100644 --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -1273,7 +1273,20 @@ TEST_F(FindExplicitReferencesTest, All) { "5: targets = {Bar}\n" "6: targets = {bar}, decl\n" "7: targets = {foo()::Bar::Foo}\n" - "8: targets = {foo()::Baz::Field}\n"}}; + "8: targets = {foo()::Baz::Field}\n"}, + {R"cpp( + template + void crash(T); + template + void foo() { + $0^crash({.$1^x = $2^T()}); + } + )cpp", + "0: targets = {crash}\n" + "1: targets = {}\n" + "2: targets = {T}\n" + }, + }; for (const auto &C : Cases) { llvm::StringRef ExpectedCode = C.first;