@@ -879,11 +879,8 @@ class ReferenceFinder : public index::IndexDataConsumer {
879
879
};
880
880
881
881
ReferenceFinder (const ParsedAST &AST,
882
- const std::vector<const NamedDecl *> &TargetDecls)
883
- : AST(AST) {
884
- for (const NamedDecl *D : TargetDecls)
885
- CanonicalTargets.insert (D->getCanonicalDecl ());
886
- }
882
+ const llvm::DenseSet<SymbolID> &TargetIDs)
883
+ : AST(AST), TargetIDs(TargetIDs) {}
887
884
888
885
std::vector<Reference> take () && {
889
886
llvm::sort (References, [](const Reference &L, const Reference &R) {
@@ -908,9 +905,9 @@ class ReferenceFinder : public index::IndexDataConsumer {
908
905
llvm::ArrayRef<index::SymbolRelation> Relations,
909
906
SourceLocation Loc,
910
907
index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
911
- assert (D->isCanonicalDecl () && " expect D to be a canonical declaration" );
912
908
const SourceManager &SM = AST.getSourceManager ();
913
- if (!CanonicalTargets.count (D) || !isInsideMainFile (Loc, SM))
909
+ if (!isInsideMainFile (Loc, SM) ||
910
+ TargetIDs.find (getSymbolID (D)) == TargetIDs.end ())
914
911
return true ;
915
912
const auto &TB = AST.getTokens ();
916
913
Loc = SM.getFileLoc (Loc);
@@ -920,14 +917,14 @@ class ReferenceFinder : public index::IndexDataConsumer {
920
917
}
921
918
922
919
private:
923
- llvm::SmallSet<const Decl *, 4 > CanonicalTargets;
924
920
std::vector<Reference> References;
925
921
const ParsedAST &AST;
922
+ const llvm::DenseSet<SymbolID> &TargetIDs;
926
923
};
927
924
928
925
std::vector<ReferenceFinder::Reference>
929
- findRefs (const std::vector< const NamedDecl * > &Decls , ParsedAST &AST) {
930
- ReferenceFinder RefFinder (AST, Decls );
926
+ findRefs (const llvm::DenseSet<SymbolID > &IDs , ParsedAST &AST) {
927
+ ReferenceFinder RefFinder (AST, IDs );
931
928
index::IndexingOptions IndexOpts;
932
929
IndexOpts.SystemSymbolFilter =
933
930
index::IndexingOptions::SystemSymbolFilterKind::All;
@@ -1217,7 +1214,11 @@ std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
1217
1214
if (!Decls.empty ()) {
1218
1215
// FIXME: we may get multiple DocumentHighlights with the same location
1219
1216
// and different kinds, deduplicate them.
1220
- for (const auto &Ref : findRefs ({Decls.begin (), Decls.end ()}, AST))
1217
+ llvm::DenseSet<SymbolID> Targets;
1218
+ for (const NamedDecl *ND : Decls)
1219
+ if (auto ID = getSymbolID (ND))
1220
+ Targets.insert (ID);
1221
+ for (const auto &Ref : findRefs (Targets, AST))
1221
1222
Result.push_back (toHighlight (Ref, SM));
1222
1223
return true ;
1223
1224
}
@@ -1295,13 +1296,14 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
1295
1296
llvm::consumeError (CurLoc.takeError ());
1296
1297
return {};
1297
1298
}
1298
- llvm::Optional<DefinedMacro> Macro;
1299
- if (const auto *IdentifierAtCursor =
1300
- syntax::spelledIdentifierTouching (*CurLoc, AST.getTokens ())) {
1301
- Macro = locateMacroAt (*IdentifierAtCursor, AST.getPreprocessor ());
1302
- }
1303
1299
1304
1300
RefsRequest Req;
1301
+
1302
+ const auto *IdentifierAtCursor =
1303
+ syntax::spelledIdentifierTouching (*CurLoc, AST.getTokens ());
1304
+ llvm::Optional<DefinedMacro> Macro;
1305
+ if (IdentifierAtCursor)
1306
+ Macro = locateMacroAt (*IdentifierAtCursor, AST.getPreprocessor ());
1305
1307
if (Macro) {
1306
1308
// Handle references to macro.
1307
1309
if (auto MacroSID = getSymbolID (Macro->Name , Macro->Info , SM)) {
@@ -1325,9 +1327,35 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
1325
1327
DeclRelation::TemplatePattern | DeclRelation::Alias;
1326
1328
std::vector<const NamedDecl *> Decls =
1327
1329
getDeclAtPosition (AST, *CurLoc, Relations);
1330
+ llvm::DenseSet<SymbolID> Targets;
1331
+ for (const NamedDecl *D : Decls)
1332
+ if (auto ID = getSymbolID (D))
1333
+ Targets.insert (ID);
1334
+
1335
+ llvm::DenseSet<SymbolID> Overrides;
1336
+ if (Index) {
1337
+ RelationsRequest FindOverrides;
1338
+ FindOverrides.Predicate = RelationKind::OverriddenBy;
1339
+ for (const NamedDecl *ND : Decls) {
1340
+ // Special case: virtual void meth^od() = 0 includes refs of overrides.
1341
+ if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(ND)) {
1342
+ if (CMD->isPure ())
1343
+ if (IdentifierAtCursor && SM.getSpellingLoc (CMD->getLocation ()) ==
1344
+ IdentifierAtCursor->location ())
1345
+ if (auto ID = getSymbolID (CMD))
1346
+ FindOverrides.Subjects .insert (ID);
1347
+ }
1348
+ }
1349
+ if (!FindOverrides.Subjects .empty ())
1350
+ Index->relations (FindOverrides,
1351
+ [&](const SymbolID &Subject, const Symbol &Object) {
1352
+ Overrides.insert (Object.ID );
1353
+ });
1354
+ Targets.insert (Overrides.begin (), Overrides.end ());
1355
+ }
1328
1356
1329
1357
// We traverse the AST to find references in the main file.
1330
- auto MainFileRefs = findRefs (Decls , AST);
1358
+ auto MainFileRefs = findRefs (Targets , AST);
1331
1359
// We may get multiple refs with the same location and different Roles, as
1332
1360
// cross-reference is only interested in locations, we deduplicate them
1333
1361
// by the location to avoid emitting duplicated locations.
@@ -1345,6 +1373,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
1345
1373
Results.References .push_back (std::move (Result));
1346
1374
}
1347
1375
if (Index && Results.References .size () <= Limit) {
1376
+ Req.IDs = std::move (Overrides);
1348
1377
for (const Decl *D : Decls) {
1349
1378
// Not all symbols can be referenced from outside (e.g.
1350
1379
// function-locals).
0 commit comments