43
43
#include " llvm/ADT/StringExtras.h"
44
44
#include " llvm/ADT/StringRef.h"
45
45
#include " llvm/Support/Casting.h"
46
+ #include " llvm/Support/Error.h"
46
47
#include " llvm/Support/Path.h"
47
48
#include " llvm/Support/raw_ostream.h"
48
49
@@ -214,24 +215,34 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
214
215
}
215
216
}
216
217
218
+ auto CurLoc = sourceLocationInMainFile (SM, Pos);
219
+ if (!CurLoc) {
220
+ elog (" locateSymbolAt failed to convert position to source location: {0}" ,
221
+ CurLoc.takeError ());
222
+ return {};
223
+ }
224
+
217
225
// Macros are simple: there's no declaration/definition distinction.
218
226
// As a consequence, there's no need to look them up in the index either.
219
- SourceLocation IdentStartLoc = SM.getMacroArgExpandedLocation (
220
- getBeginningOfIdentifier (Pos, AST.getSourceManager (), AST.getLangOpts ()));
221
227
std::vector<LocatedSymbol> Result;
222
- if (auto M = locateMacroAt (IdentStartLoc, AST.getPreprocessor ())) {
223
- if (auto Loc = makeLocation (AST.getASTContext (),
224
- M->Info ->getDefinitionLoc (), *MainFilePath)) {
225
- LocatedSymbol Macro;
226
- Macro.Name = std::string (M->Name );
227
- Macro.PreferredDeclaration = *Loc;
228
- Macro.Definition = Loc;
229
- Result.push_back (std::move (Macro));
230
-
231
- // Don't look at the AST or index if we have a macro result.
232
- // (We'd just return declarations referenced from the macro's
233
- // expansion.)
234
- return Result;
228
+ const auto *TouchedIdentifier =
229
+ syntax::spelledIdentifierTouching (*CurLoc, AST.getTokens ());
230
+ if (TouchedIdentifier) {
231
+ if (auto M = locateMacroAt (TouchedIdentifier->location (),
232
+ AST.getPreprocessor ())) {
233
+ if (auto Loc = makeLocation (AST.getASTContext (),
234
+ M->Info ->getDefinitionLoc (), *MainFilePath)) {
235
+ LocatedSymbol Macro;
236
+ Macro.Name = std::string (M->Name );
237
+ Macro.PreferredDeclaration = *Loc;
238
+ Macro.Definition = Loc;
239
+ Result.push_back (std::move (Macro));
240
+
241
+ // Don't look at the AST or index if we have a macro result.
242
+ // (We'd just return declarations referenced from the macro's
243
+ // expansion.)
244
+ return Result;
245
+ }
235
246
}
236
247
}
237
248
@@ -244,15 +255,6 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
244
255
// Keep track of SymbolID -> index mapping, to fill in index data later.
245
256
llvm::DenseMap<SymbolID, size_t > ResultIndex;
246
257
247
- SourceLocation SourceLoc;
248
- if (auto L = sourceLocationInMainFile (SM, Pos)) {
249
- SourceLoc = *L;
250
- } else {
251
- elog (" locateSymbolAt failed to convert position to source location: {0}" ,
252
- L.takeError ());
253
- return Result;
254
- }
255
-
256
258
auto AddResultDecl = [&](const NamedDecl *D) {
257
259
const NamedDecl *Def = getDefinition (D);
258
260
const NamedDecl *Preferred = Def ? Def : D;
@@ -277,16 +279,15 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
277
279
// Emit all symbol locations (declaration or definition) from AST.
278
280
DeclRelationSet Relations =
279
281
DeclRelation::TemplatePattern | DeclRelation::Alias;
280
- for (const NamedDecl *D : getDeclAtPosition (AST, SourceLoc , Relations)) {
282
+ for (const NamedDecl *D : getDeclAtPosition (AST, *CurLoc , Relations)) {
281
283
// Special case: void foo() ^override: jump to the overridden method.
282
284
if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) {
283
- const InheritableAttr* Attr = D->getAttr <OverrideAttr>();
285
+ const InheritableAttr * Attr = D->getAttr <OverrideAttr>();
284
286
if (!Attr)
285
287
Attr = D->getAttr <FinalAttr>();
286
- const syntax::Token *Tok =
287
- spelledIdentifierTouching (SourceLoc, AST.getTokens ());
288
- if (Attr && Tok &&
289
- SM.getSpellingLoc (Attr->getLocation ()) == Tok->location ()) {
288
+ if (Attr && TouchedIdentifier &&
289
+ SM.getSpellingLoc (Attr->getLocation ()) ==
290
+ TouchedIdentifier->location ()) {
290
291
// We may be overridding multiple methods - offer them all.
291
292
for (const NamedDecl *ND : CMD->overridden_methods ())
292
293
AddResultDecl (ND);
@@ -296,8 +297,9 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
296
297
297
298
// Special case: the point of declaration of a template specialization,
298
299
// it's more useful to navigate to the template declaration.
299
- if (SM.getMacroArgExpandedLocation (D->getLocation ()) == IdentStartLoc) {
300
- if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
300
+ if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
301
+ if (TouchedIdentifier &&
302
+ D->getLocation () == TouchedIdentifier->location ()) {
301
303
AddResultDecl (CTSD->getSpecializedTemplate ());
302
304
continue ;
303
305
}
@@ -416,12 +418,12 @@ std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
416
418
// FIXME: show references to macro within file?
417
419
DeclRelationSet Relations =
418
420
DeclRelation::TemplatePattern | DeclRelation::Alias;
419
- auto References = findRefs (
420
- getDeclAtPosition (AST,
421
- SM. getMacroArgExpandedLocation ( getBeginningOfIdentifier (
422
- Pos, SM, AST. getLangOpts ())),
423
- Relations),
424
- AST);
421
+ auto CurLoc = sourceLocationInMainFile (SM, Pos);
422
+ if (!CurLoc) {
423
+ llvm::consumeError (CurLoc. takeError ());
424
+ return {};
425
+ }
426
+ auto References = findRefs ( getDeclAtPosition (AST, *CurLoc, Relations), AST);
425
427
426
428
// FIXME: we may get multiple DocumentHighlights with the same location and
427
429
// different kinds, deduplicate them.
@@ -456,11 +458,18 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
456
458
return Results;
457
459
}
458
460
auto URIMainFile = URIForFile::canonicalize (*MainFilePath, *MainFilePath);
459
- auto Loc = SM.getMacroArgExpandedLocation (
460
- getBeginningOfIdentifier (Pos, SM, AST.getLangOpts ()));
461
+ auto CurLoc = sourceLocationInMainFile (SM, Pos);
462
+ if (!CurLoc) {
463
+ llvm::consumeError (CurLoc.takeError ());
464
+ return {};
465
+ }
466
+ SourceLocation SLocId;
467
+ if (const auto *IdentifierAtCursor =
468
+ syntax::spelledIdentifierTouching (*CurLoc, AST.getTokens ()))
469
+ SLocId = IdentifierAtCursor->location ();
461
470
RefsRequest Req;
462
471
463
- if (auto Macro = locateMacroAt (Loc , AST.getPreprocessor ())) {
472
+ if (auto Macro = locateMacroAt (SLocId , AST.getPreprocessor ())) {
464
473
// Handle references to macro.
465
474
if (auto MacroSID = getSymbolID (Macro->Name , Macro->Info , SM)) {
466
475
// Collect macro references from main file.
@@ -483,7 +492,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
483
492
// DeclRelation::Underlying.
484
493
DeclRelationSet Relations = DeclRelation::TemplatePattern |
485
494
DeclRelation::Alias | DeclRelation::Underlying;
486
- auto Decls = getDeclAtPosition (AST, Loc , Relations);
495
+ auto Decls = getDeclAtPosition (AST, *CurLoc , Relations);
487
496
488
497
// We traverse the AST to find references in the main file.
489
498
auto MainFileRefs = findRefs (Decls, AST);
@@ -541,16 +550,19 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
541
550
542
551
std::vector<SymbolDetails> getSymbolInfo (ParsedAST &AST, Position Pos) {
543
552
const SourceManager &SM = AST.getSourceManager ();
544
- auto Loc = SM.getMacroArgExpandedLocation (
545
- getBeginningOfIdentifier (Pos, SM, AST.getLangOpts ()));
553
+ auto CurLoc = sourceLocationInMainFile (SM, Pos);
554
+ if (!CurLoc) {
555
+ llvm::consumeError (CurLoc.takeError ());
556
+ return {};
557
+ }
546
558
547
559
std::vector<SymbolDetails> Results;
548
560
549
561
// We also want the targets of using-decls, so we include
550
562
// DeclRelation::Underlying.
551
563
DeclRelationSet Relations = DeclRelation::TemplatePattern |
552
564
DeclRelation::Alias | DeclRelation::Underlying;
553
- for (const NamedDecl *D : getDeclAtPosition (AST, Loc , Relations)) {
565
+ for (const NamedDecl *D : getDeclAtPosition (AST, *CurLoc , Relations)) {
554
566
SymbolDetails NewSymbol;
555
567
std::string QName = printQualifiedName (*D);
556
568
auto SplitQName = splitQualifiedName (QName);
@@ -570,7 +582,13 @@ std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
570
582
Results.push_back (std::move (NewSymbol));
571
583
}
572
584
573
- if (auto M = locateMacroAt (Loc, AST.getPreprocessor ())) {
585
+ const auto *IdentifierAtCursor =
586
+ syntax::spelledIdentifierTouching (*CurLoc, AST.getTokens ());
587
+ if (!IdentifierAtCursor)
588
+ return Results;
589
+
590
+ if (auto M = locateMacroAt (IdentifierAtCursor->location (),
591
+ AST.getPreprocessor ())) {
574
592
SymbolDetails NewMacro;
575
593
NewMacro.name = std::string (M->Name );
576
594
llvm::SmallString<32 > USR;
@@ -747,17 +765,18 @@ const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
747
765
};
748
766
749
767
const SourceManager &SM = AST.getSourceManager ();
750
- SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation (
751
- getBeginningOfIdentifier (Pos, SM, AST.getLangOpts ()));
752
- unsigned Offset = SM.getDecomposedSpellingLoc (SourceLocationBeg).second ;
753
768
const CXXRecordDecl *Result = nullptr ;
754
- SelectionTree::createEach (AST.getASTContext (), AST.getTokens (), Offset,
755
- Offset, [&](SelectionTree ST) {
769
+ auto Offset = positionToOffset (SM.getBufferData (SM.getMainFileID ()), Pos);
770
+ if (!Offset) {
771
+ llvm::consumeError (Offset.takeError ());
772
+ return Result;
773
+ }
774
+ SelectionTree::createEach (AST.getASTContext (), AST.getTokens (), *Offset,
775
+ *Offset, [&](SelectionTree ST) {
756
776
Result = RecordFromNode (ST.commonAncestor ());
757
777
return Result != nullptr ;
758
778
});
759
779
return Result;
760
-
761
780
}
762
781
763
782
std::vector<const CXXRecordDecl *> typeParents (const CXXRecordDecl *CXXRD) {
0 commit comments