@@ -197,14 +197,44 @@ const ValueDecl *Symbol::getDeclInheritingDocs() const {
197
197
}
198
198
}
199
199
200
+ namespace {
201
+
202
+ StringRef getFileURI (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 getFileURI (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 )
203
234
return ;
204
235
205
236
if (auto *ClangD = ClangN.getAsDecl ()) {
206
237
const clang::ASTContext &ClangContext = ClangD->getASTContext ();
207
- const clang::SourceManager &ClangSourceMgr = ClangContext.getSourceManager ();
208
238
const clang::RawComment *RC =
209
239
ClangContext.getRawCommentForAnyRedecl (ClangD);
210
240
if (!RC || !RC->isDocumentation ())
@@ -214,20 +244,17 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
214
244
// line and column ranges. Also consider handling cross-language
215
245
// hierarchies, ie. if there's no comment on the ObjC decl we should
216
246
// look up the hierarchy (and vice versa).
217
- std::string Text = RC->getFormattedText (ClangSourceMgr ,
247
+ std::string Text = RC->getFormattedText (ClangContext. getSourceManager () ,
218
248
ClangContext.getDiagnostics ());
219
249
Text = unicode::sanitizeUTF8 (Text);
220
250
221
251
SmallVector<StringRef, 8 > Lines;
222
252
splitIntoLines (Text, Lines);
223
253
224
254
OS.attributeObject (" docComment" , [&]() {
225
- clang::PresumedLoc Loc = ClangSourceMgr.getPresumedLoc (ClangD->getLocation ());
226
- if (Loc.isValid ()) {
227
- SmallString<1024 > FileURI (" file://" );
228
- FileURI.append (Loc.getFilename ());
229
- OS.attribute (" uri" , FileURI.str ());
230
- }
255
+ StringRef FileName = getFileURI (ClangD);
256
+ if (!FileName.empty ())
257
+ serializeFileURI (OS, FileName);
231
258
if (const auto *ModuleD = VD->getModuleContext ()) {
232
259
OS.attribute (" module" , ModuleD->getNameStr ());
233
260
}
@@ -257,15 +284,9 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
257
284
}
258
285
259
286
OS.attributeObject (" docComment" , [&](){
260
- auto Loc = DocCommentProvidingDecl->getLoc (/* SerializedOK=*/ true );
261
- if (Loc.isValid ()) {
262
- auto FileName = DocCommentProvidingDecl->getASTContext ().SourceMgr .getDisplayNameForLoc (Loc);
263
- if (!FileName.empty ()) {
264
- SmallString<1024 > FileURI (" file://" );
265
- FileURI.append (FileName);
266
- OS.attribute (" uri" , FileURI.str ());
267
- }
268
- }
287
+ StringRef FileName = getFileURI (DocCommentProvidingDecl);
288
+ if (!FileName.empty ())
289
+ serializeFileURI (OS, FileName);
269
290
if (const auto *ModuleD = DocCommentProvidingDecl->getModuleContext ()) {
270
291
OS.attribute (" module" , ModuleD->getNameStr ());
271
292
}
@@ -437,37 +458,31 @@ void Symbol::serializeLocationMixin(llvm::json::OStream &OS) const {
437
458
return ;
438
459
439
460
if (auto *ClangD = ClangN.getAsDecl ()) {
440
- clang::SourceManager &ClangSM =
441
- ClangD->getASTContext ().getSourceManager ();
442
-
443
- clang::PresumedLoc Loc = ClangSM.getPresumedLoc (ClangD->getLocation ());
444
- if (Loc.isValid ()) {
445
- // TODO: We should use a common function to fill in the location
446
- // information for both cursor info and symbol graph gen, then also
447
- // include position here.
461
+ StringRef FileName = getFileURI (ClangD);
462
+ if (!FileName.empty ()) {
448
463
OS.attributeObject (" location" , [&](){
449
- SmallString<1024 > FileURI (" file://" );
450
- FileURI.append (Loc.getFilename ());
451
- 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);
452
468
});
453
469
}
454
470
}
455
471
456
472
return ;
457
473
}
458
474
459
- auto Loc = VD-> getLoc ( /* SerializedOK= */ true );
460
- if (Loc. isInvalid ()) {
475
+ auto FileName = getFileURI (VD );
476
+ if (FileName. empty ()) {
461
477
return ;
462
478
}
463
- auto FileName = VD->getASTContext ().SourceMgr .getDisplayNameForLoc (Loc);
464
- 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 ()) {
465
482
return ;
466
483
}
467
484
OS.attributeObject (" location" , [&](){
468
- SmallString<1024 > FileURI (" file://" );
469
- FileURI.append (FileName);
470
- OS.attribute (" uri" , FileURI.str ());
485
+ serializeFileURI (OS, FileName);
471
486
serializePosition (" position" , Loc, Graph->M .getASTContext ().SourceMgr , OS);
472
487
});
473
488
}
0 commit comments