@@ -416,12 +416,19 @@ class ContainerTracker {
416
416
}
417
417
};
418
418
419
+ struct MappedLoc {
420
+ unsigned Line;
421
+ unsigned Column;
422
+ bool IsGenerated;
423
+ };
424
+
419
425
class IndexSwiftASTWalker : public SourceEntityWalker {
420
426
IndexDataConsumer &IdxConsumer;
421
427
SourceManager &SrcMgr;
422
428
unsigned BufferID;
423
429
bool enableWarnings;
424
430
431
+ ModuleDecl *CurrentModule = nullptr ;
425
432
bool IsModuleFile = false ;
426
433
bool isSystemModule = false ;
427
434
@@ -584,7 +591,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
584
591
assert (Cancelled || Containers.empty ());
585
592
}
586
593
587
- // / Walk both the arguments and expansion of the macro, so we index both.Only walk the arguments of a macro, to represent the source as written.
594
+ // / Walk both the arguments and expansion of the macro, so we index both.
588
595
MacroWalking getMacroWalkingBehavior () const override {
589
596
return MacroWalking::ArgumentsAndExpansion;
590
597
}
@@ -877,14 +884,17 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
877
884
}
878
885
879
886
bool visitModuleReference (ModuleEntity Mod, CharSourceRange Range) override {
880
- SourceLoc Loc = Range.getStart ();
881
-
882
- if (Loc.isInvalid ())
887
+ auto MappedLoc = getMappedLocation (Range.getStart ());
888
+ if (!MappedLoc)
883
889
return true ;
884
890
885
891
IndexSymbol Info;
886
- std::tie (Info.line , Info.column ) = getLineCol (Loc);
892
+ Info.line = MappedLoc->Line ;
893
+ Info.column = MappedLoc->Column ;
887
894
Info.roles |= (unsigned )SymbolRole::Reference;
895
+ if (MappedLoc->IsGenerated ) {
896
+ Info.roles |= (unsigned )SymbolRole::Implicit;
897
+ }
888
898
Info.symInfo = getSymbolInfoForModule (Mod);
889
899
getModuleNameAndUSR (Mod, Info.name , Info.USR );
890
900
addContainedByRelationIfContained (Info);
@@ -959,11 +969,11 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
959
969
960
970
bool reportPseudoGetterDecl (VarDecl *D) {
961
971
return reportPseudoAccessor (D, AccessorKind::Get, /* IsRef=*/ false ,
962
- D->getLoc ());
972
+ D->getLoc (/* SerializedOK= */ false ));
963
973
}
964
974
bool reportPseudoSetterDecl (VarDecl *D) {
965
975
return reportPseudoAccessor (D, AccessorKind::Set, /* IsRef=*/ false ,
966
- D->getLoc ());
976
+ D->getLoc (/* SerializedOK= */ false ));
967
977
}
968
978
bool reportPseudoAccessor (AbstractStorageDecl *D, AccessorKind AccKind,
969
979
bool IsRef, SourceLoc Loc);
@@ -994,10 +1004,31 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
994
1004
995
1005
bool indexComment (const Decl *D);
996
1006
997
- std::pair<unsigned , unsigned > getLineCol (SourceLoc Loc) {
998
- if (Loc.isInvalid ())
999
- return std::make_pair (0 , 0 );
1000
- return SrcMgr.getLineAndColumnInBuffer (Loc, BufferID);
1007
+ // Return the line and column of \p loc, as well as whether it was from a
1008
+ // generated buffer or not. 0:0 if indexing a module and \p loc is invalid.
1009
+ // \c None if \p loc is otherwise invalid or its original location isn't
1010
+ // contained within the current buffer.
1011
+ Optional<MappedLoc> getMappedLocation (SourceLoc loc) {
1012
+ if (loc.isInvalid ()) {
1013
+ if (IsModuleFile)
1014
+ return {{0 , 0 , false }};
1015
+ return None;
1016
+ }
1017
+
1018
+ bool inGeneratedBuffer =
1019
+ !SrcMgr.rangeContainsTokenLoc (SrcMgr.getRangeForBuffer (BufferID), loc);
1020
+
1021
+ auto bufferID = BufferID;
1022
+ if (inGeneratedBuffer) {
1023
+ std::tie (bufferID, loc) = CurrentModule->getOriginalLocation (loc);
1024
+ if (BufferID != bufferID) {
1025
+ assert (false && " Location is not within file being indexed" );
1026
+ return None;
1027
+ }
1028
+ }
1029
+
1030
+ auto [line, col] = SrcMgr.getLineAndColumnInBuffer (loc, bufferID);
1031
+ return {{line, col, inGeneratedBuffer}};
1001
1032
}
1002
1033
1003
1034
bool shouldIndex (ValueDecl *D, bool IsRef) const {
@@ -1070,6 +1101,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
1070
1101
} // anonymous namespace
1071
1102
1072
1103
void IndexSwiftASTWalker::visitDeclContext (DeclContext *Context) {
1104
+ CurrentModule = Context->getParentModule ();
1073
1105
IsModuleFile = false ;
1074
1106
isSystemModule = Context->getParentModule ()->isNonUserModule ();
1075
1107
auto accessor = dyn_cast<AccessorDecl>(Context);
@@ -1081,6 +1113,8 @@ void IndexSwiftASTWalker::visitDeclContext(DeclContext *Context) {
1081
1113
}
1082
1114
1083
1115
void IndexSwiftASTWalker::visitModule (ModuleDecl &Mod) {
1116
+ CurrentModule = &Mod;
1117
+
1084
1118
SourceFile *SrcFile = nullptr ;
1085
1119
for (auto File : Mod.getFiles ()) {
1086
1120
if (auto SF = dyn_cast<SourceFile>(File)) {
@@ -1662,10 +1696,8 @@ bool IndexSwiftASTWalker::initIndexSymbol(ValueDecl *D, SourceLoc Loc,
1662
1696
bool IsRef, IndexSymbol &Info) {
1663
1697
assert (D);
1664
1698
1665
- if (Loc.isInvalid () && !IsModuleFile)
1666
- return true ;
1667
-
1668
- if (Loc.isValid () && SrcMgr.findBufferContainingLoc (Loc) != BufferID)
1699
+ auto MappedLoc = getMappedLocation (Loc);
1700
+ if (!MappedLoc)
1669
1701
return true ;
1670
1702
1671
1703
if (auto *VD = dyn_cast<VarDecl>(D)) {
@@ -1674,15 +1706,17 @@ bool IndexSwiftASTWalker::initIndexSymbol(ValueDecl *D, SourceLoc Loc,
1674
1706
}
1675
1707
1676
1708
Info.decl = D;
1709
+ Info.line = MappedLoc->Line ;
1710
+ Info.column = MappedLoc->Column ;
1677
1711
Info.symInfo = getSymbolInfoForDecl (D);
1712
+
1678
1713
if (Info.symInfo .Kind == SymbolKind::Unknown)
1679
1714
return true ;
1680
1715
1681
1716
// Cannot be extension, which is not a ValueDecl.
1682
1717
1683
1718
if (getNameAndUSR (D, /* ExtD=*/ nullptr , Info.name , Info.USR ))
1684
1719
return true ;
1685
- std::tie (Info.line , Info.column ) = getLineCol (Loc);
1686
1720
1687
1721
if (IsRef) {
1688
1722
Info.roles |= (unsigned )SymbolRole::Reference;
@@ -1695,23 +1729,37 @@ bool IndexSwiftASTWalker::initIndexSymbol(ValueDecl *D, SourceLoc Loc,
1695
1729
Info.group = Group.value ();
1696
1730
}
1697
1731
1732
+ if (MappedLoc->IsGenerated ) {
1733
+ Info.roles |= (unsigned )SymbolRole::Implicit;
1734
+ }
1735
+
1698
1736
return false ;
1699
1737
}
1700
1738
1701
1739
bool IndexSwiftASTWalker::initIndexSymbol (ExtensionDecl *ExtD, ValueDecl *ExtendedD,
1702
1740
SourceLoc Loc, IndexSymbol &Info) {
1703
1741
assert (ExtD && ExtendedD);
1742
+
1743
+ auto MappedLoc = getMappedLocation (Loc);
1744
+ if (!MappedLoc)
1745
+ return true ;
1746
+
1704
1747
Info.decl = ExtendedD;
1748
+ Info.line = MappedLoc->Line ;
1749
+ Info.column = MappedLoc->Column ;
1705
1750
Info.symInfo = getSymbolInfoForDecl (ExtD);
1751
+
1706
1752
if (Info.symInfo .Kind == SymbolKind::Unknown)
1707
1753
return true ;
1708
1754
1709
1755
Info.roles |= (unsigned )SymbolRole::Definition;
1756
+ if (MappedLoc->IsGenerated ) {
1757
+ Info.roles |= (unsigned )SymbolRole::Implicit;
1758
+ }
1710
1759
1711
1760
if (getNameAndUSR (ExtendedD, ExtD, Info.name , Info.USR ))
1712
1761
return true ;
1713
1762
1714
- std::tie (Info.line , Info.column ) = getLineCol (Loc);
1715
1763
if (auto Group = ExtD->getGroupName ())
1716
1764
Info.group = Group.value ();
1717
1765
return false ;
@@ -1832,21 +1880,34 @@ bool IndexSwiftASTWalker::indexComment(const Decl *D) {
1832
1880
break ;
1833
1881
}
1834
1882
}
1835
- if (loc.isInvalid ())
1883
+
1884
+ auto mappedLoc = getMappedLocation (loc);
1885
+ if (!mappedLoc)
1836
1886
continue ;
1887
+
1837
1888
IndexSymbol Info;
1889
+
1838
1890
Info.decl = nullptr ;
1891
+
1892
+ Info.line = mappedLoc->Line ;
1893
+ Info.column = mappedLoc->Column ;
1894
+
1839
1895
Info.symInfo = SymbolInfo{ SymbolKind::CommentTag, SymbolSubKind::None,
1840
1896
SymbolLanguage::Swift, SymbolPropertySet () };
1897
+
1841
1898
Info.roles |= (unsigned )SymbolRole::Definition;
1899
+ if (mappedLoc->IsGenerated ) {
1900
+ Info.roles |= (unsigned )SymbolRole::Implicit;
1901
+ }
1902
+
1842
1903
Info.name = StringRef ();
1843
1904
SmallString<128 > storage;
1844
1905
{
1845
1906
llvm::raw_svector_ostream OS (storage);
1846
1907
OS << " t:" << tagName;
1847
1908
Info.USR = stringStorage.copyString (OS.str ());
1848
1909
}
1849
- std::tie (Info. line , Info. column ) = getLineCol (loc);
1910
+
1850
1911
if (!IdxConsumer.startSourceEntity (Info) || !IdxConsumer.finishSourceEntity (Info.symInfo , Info.roles )) {
1851
1912
Cancelled = true ;
1852
1913
break ;
0 commit comments