@@ -197,6 +197,37 @@ const ValueDecl *Symbol::getDeclInheritingDocs() const {
197197 }
198198}
199199
200+ namespace {
201+
202+ StringRef getFileNameForDecl (const ValueDecl *VD) {
203+ if (!VD) return StringRef{};
204+
205+ SourceLoc Loc = VD->getLoc (/* SerializedOK=*/ true );
206+ if (Loc.isInvalid ()) return StringRef{};
207+
208+ SourceManager &SourceM = VD->getASTContext ().SourceMgr ;
209+ return SourceM.getDisplayNameForLoc (Loc);
210+ }
211+
212+ StringRef getFileNameForDecl (const clang::Decl *ClangD) {
213+ if (!ClangD) return StringRef{};
214+
215+ const clang::SourceManager &ClangSourceMgr = ClangD->getASTContext ().getSourceManager ();
216+ clang::PresumedLoc Loc = ClangSourceMgr.getPresumedLoc (ClangD->getLocation ());
217+ if (Loc.isInvalid ()) return StringRef{};
218+
219+ return StringRef (Loc.getFilename ());
220+ }
221+
222+ void serializeFileURI (llvm::json::OStream &OS, StringRef FileName) {
223+ // FIXME: This can emit invalid URIs if the file name has a space in it (rdar://69242070)
224+ SmallString<1024 > FileURI (" file://" );
225+ FileURI.append (FileName);
226+ OS.attribute (" uri" , FileURI.str ());
227+ }
228+
229+ }
230+
200231void Symbol::serializeDocComment (llvm::json::OStream &OS) const {
201232 if (ClangNode ClangN = VD->getClangNode ()) {
202233 if (!Graph->Walker .Options .IncludeClangDocs )
@@ -221,6 +252,12 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
221252 splitIntoLines (Text, Lines);
222253
223254 OS.attributeObject (" docComment" , [&]() {
255+ StringRef FileName = getFileNameForDecl (ClangD);
256+ if (!FileName.empty ())
257+ serializeFileURI (OS, FileName);
258+ if (const auto *ModuleD = VD->getModuleContext ()) {
259+ OS.attribute (" module" , ModuleD->getNameStr ());
260+ }
224261 OS.attributeArray (" lines" , [&]() {
225262 for (StringRef Line : Lines) {
226263 OS.object ([&](){
@@ -247,6 +284,12 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
247284 }
248285
249286 OS.attributeObject (" docComment" , [&](){
287+ StringRef FileName = getFileNameForDecl (DocCommentProvidingDecl);
288+ if (!FileName.empty ())
289+ serializeFileURI (OS, FileName);
290+ if (const auto *ModuleD = DocCommentProvidingDecl->getModuleContext ()) {
291+ OS.attribute (" module" , ModuleD->getNameStr ());
292+ }
250293 auto LL = Graph->Ctx .getLineList (RC);
251294 StringRef FirstNonBlankLine;
252295 for (const auto &Line : LL.getLines ()) {
@@ -415,37 +458,31 @@ void Symbol::serializeLocationMixin(llvm::json::OStream &OS) const {
415458 return ;
416459
417460 if (auto *ClangD = ClangN.getAsDecl ()) {
418- clang::SourceManager &ClangSM =
419- ClangD->getASTContext ().getSourceManager ();
420-
421- clang::PresumedLoc Loc = ClangSM.getPresumedLoc (ClangD->getLocation ());
422- if (Loc.isValid ()) {
423- // TODO: We should use a common function to fill in the location
424- // information for both cursor info and symbol graph gen, then also
425- // include position here.
461+ StringRef FileName = getFileNameForDecl (ClangD);
462+ if (!FileName.empty ()) {
426463 OS.attributeObject (" location" , [&](){
427- SmallString<1024 > FileURI (" file://" );
428- FileURI.append (Loc.getFilename ());
429- OS.attribute (" uri" , FileURI.str ());
464+ // TODO: We should use a common function to fill in the location
465+ // information for both cursor info and symbol graph gen, then also
466+ // include position here.
467+ serializeFileURI (OS, FileName);
430468 });
431469 }
432470 }
433471
434472 return ;
435473 }
436474
437- auto Loc = VD-> getLoc ( /* SerializedOK= */ true );
438- if (Loc. isInvalid ()) {
475+ auto FileName = getFileNameForDecl (VD );
476+ if (FileName. empty ()) {
439477 return ;
440478 }
441- auto FileName = VD->getASTContext ().SourceMgr .getDisplayNameForLoc (Loc);
442- if (FileName.empty ()) {
479+ // TODO: Fold serializePosition into serializeFileURI so we don't need to load Loc twice?
480+ auto Loc = VD->getLoc (/* SerializedOK=*/ true );
481+ if (Loc.isInvalid ()) {
443482 return ;
444483 }
445484 OS.attributeObject (" location" , [&](){
446- SmallString<1024 > FileURI (" file://" );
447- FileURI.append (FileName);
448- OS.attribute (" uri" , FileURI.str ());
485+ serializeFileURI (OS, FileName);
449486 serializePosition (" position" , Loc, Graph->M .getASTContext ().SourceMgr , OS);
450487 });
451488}
0 commit comments