Skip to content

Commit 75d9acf

Browse files
refactor: group file URI collection/serialization together
1 parent 7182682 commit 75d9acf

File tree

1 file changed

+50
-35
lines changed

1 file changed

+50
-35
lines changed

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,44 @@ const ValueDecl *Symbol::getDeclInheritingDocs() const {
197197
}
198198
}
199199

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+
200231
void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
201232
if (ClangNode ClangN = VD->getClangNode()) {
202233
if (!Graph->Walker.Options.IncludeClangDocs)
203234
return;
204235

205236
if (auto *ClangD = ClangN.getAsDecl()) {
206237
const clang::ASTContext &ClangContext = ClangD->getASTContext();
207-
const clang::SourceManager &ClangSourceMgr = ClangContext.getSourceManager();
208238
const clang::RawComment *RC =
209239
ClangContext.getRawCommentForAnyRedecl(ClangD);
210240
if (!RC || !RC->isDocumentation())
@@ -214,20 +244,17 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
214244
// line and column ranges. Also consider handling cross-language
215245
// hierarchies, ie. if there's no comment on the ObjC decl we should
216246
// look up the hierarchy (and vice versa).
217-
std::string Text = RC->getFormattedText(ClangSourceMgr,
247+
std::string Text = RC->getFormattedText(ClangContext.getSourceManager(),
218248
ClangContext.getDiagnostics());
219249
Text = unicode::sanitizeUTF8(Text);
220250

221251
SmallVector<StringRef, 8> Lines;
222252
splitIntoLines(Text, Lines);
223253

224254
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);
231258
if (const auto *ModuleD = VD->getModuleContext()) {
232259
OS.attribute("module", ModuleD->getNameStr());
233260
}
@@ -257,15 +284,9 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
257284
}
258285

259286
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);
269290
if (const auto *ModuleD = DocCommentProvidingDecl->getModuleContext()) {
270291
OS.attribute("module", ModuleD->getNameStr());
271292
}
@@ -437,37 +458,31 @@ void Symbol::serializeLocationMixin(llvm::json::OStream &OS) const {
437458
return;
438459

439460
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()) {
448463
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);
452468
});
453469
}
454470
}
455471

456472
return;
457473
}
458474

459-
auto Loc = VD->getLoc(/*SerializedOK=*/true);
460-
if (Loc.isInvalid()) {
475+
auto FileName = getFileURI(VD);
476+
if (FileName.empty()) {
461477
return;
462478
}
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()) {
465482
return;
466483
}
467484
OS.attributeObject("location", [&](){
468-
SmallString<1024> FileURI("file://");
469-
FileURI.append(FileName);
470-
OS.attribute("uri", FileURI.str());
485+
serializeFileURI(OS, FileName);
471486
serializePosition("position", Loc, Graph->M.getASTContext().SourceMgr, OS);
472487
});
473488
}

0 commit comments

Comments
 (0)