@@ -197,6 +197,37 @@ const ValueDecl *Symbol::getDeclInheritingDocs() const {
197
197
}
198
198
}
199
199
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
+
200
231
void Symbol::serializeDocComment (llvm::json::OStream &OS) const {
201
232
if (ClangNode ClangN = VD->getClangNode ()) {
202
233
if (!Graph->Walker .Options .IncludeClangDocs )
@@ -221,6 +252,12 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
221
252
splitIntoLines (Text, Lines);
222
253
223
254
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
+ }
224
261
OS.attributeArray (" lines" , [&]() {
225
262
for (StringRef Line : Lines) {
226
263
OS.object ([&](){
@@ -247,6 +284,12 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
247
284
}
248
285
249
286
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
+ }
250
293
auto LL = Graph->Ctx .getLineList (RC);
251
294
StringRef FirstNonBlankLine;
252
295
for (const auto &Line : LL.getLines ()) {
@@ -415,37 +458,31 @@ void Symbol::serializeLocationMixin(llvm::json::OStream &OS) const {
415
458
return ;
416
459
417
460
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 ()) {
426
463
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);
430
468
});
431
469
}
432
470
}
433
471
434
472
return ;
435
473
}
436
474
437
- auto Loc = VD-> getLoc ( /* SerializedOK= */ true );
438
- if (Loc. isInvalid ()) {
475
+ auto FileName = getFileNameForDecl (VD );
476
+ if (FileName. empty ()) {
439
477
return ;
440
478
}
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 ()) {
443
482
return ;
444
483
}
445
484
OS.attributeObject (" location" , [&](){
446
- SmallString<1024 > FileURI (" file://" );
447
- FileURI.append (FileName);
448
- OS.attribute (" uri" , FileURI.str ());
485
+ serializeFileURI (OS, FileName);
449
486
serializePosition (" position" , Loc, Graph->M .getASTContext ().SourceMgr , OS);
450
487
});
451
488
}
0 commit comments