Skip to content

Commit 7556abf

Browse files
committed
[clangd] findExplicitReferences impl filters nulls centrally. NFC
1 parent a054e94 commit 7556abf

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

clang-tools-extra/clangd/FindTarget.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -716,20 +716,18 @@ llvm::SmallVector<ReferenceLoc> refInStmt(const Stmt *S,
716716
if (!D.isFieldDesignator())
717717
continue;
718718

719-
llvm::SmallVector<const NamedDecl *, 1> Targets;
720-
if (D.getField())
721-
Targets.push_back(D.getField());
722-
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), D.getFieldLoc(),
723-
/*IsDecl=*/false, std::move(Targets)});
719+
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
720+
D.getFieldLoc(),
721+
/*IsDecl=*/false,
722+
{D.getField()}});
724723
}
725724
}
726725

727726
void VisitGotoStmt(const GotoStmt *GS) {
728-
llvm::SmallVector<const NamedDecl *, 1> Targets;
729-
if (const auto *L = GS->getLabel())
730-
Targets.push_back(L);
731-
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), GS->getLabelLoc(),
732-
/*IsDecl=*/false, std::move(Targets)});
727+
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
728+
GS->getLabelLoc(),
729+
/*IsDecl=*/false,
730+
{GS->getLabel()}});
733731
}
734732

735733
void VisitLabelStmt(const LabelStmt *LS) {
@@ -882,17 +880,15 @@ class ExplicitReferenceCollector
882880
// TemplateArgumentLoc is the only way to get locations for references to
883881
// template template parameters.
884882
bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
885-
llvm::SmallVector<const NamedDecl *, 1> Targets;
886883
switch (A.getArgument().getKind()) {
887884
case TemplateArgument::Template:
888885
case TemplateArgument::TemplateExpansion:
889-
if (const auto *D = A.getArgument()
890-
.getAsTemplateOrTemplatePattern()
891-
.getAsTemplateDecl())
892-
Targets.push_back(D);
893886
reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
894887
A.getTemplateNameLoc(),
895-
/*IsDecl=*/false, Targets},
888+
/*IsDecl=*/false,
889+
{A.getArgument()
890+
.getAsTemplateOrTemplatePattern()
891+
.getAsTemplateDecl()}},
896892
DynTypedNode::create(A.getArgument()));
897893
break;
898894
case TemplateArgument::Declaration:
@@ -975,11 +971,14 @@ class ExplicitReferenceCollector
975971
}
976972

977973
void visitNode(DynTypedNode N) {
978-
for (const auto &R : explicitReference(N))
979-
reportReference(R, N);
974+
for (auto &R : explicitReference(N))
975+
reportReference(std::move(R), N);
980976
}
981977

982-
void reportReference(const ReferenceLoc &Ref, DynTypedNode N) {
978+
void reportReference(ReferenceLoc &&Ref, DynTypedNode N) {
979+
// Strip null targets that can arise from invalid code.
980+
// (This avoids having to check for null everywhere we insert)
981+
llvm::erase_value(Ref.Targets, nullptr);
983982
// Our promise is to return only references from the source code. If we lack
984983
// location information, skip these nodes.
985984
// Normally this should not happen in practice, unless there are bugs in the

0 commit comments

Comments
 (0)