Skip to content

[CursorInfo] Add Clang documentation to SymbolGraph output #42268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions include/swift/AST/ASTPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,6 @@ class ASTPrinter {
printStructurePre(Kind, D);
}

/// To sanitize a malformed utf8 string to a well-formed one.
static std::string sanitizeUtf8(StringRef Text);

private:
virtual void anchor();
};
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/Unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ unsigned extractFirstUnicodeScalar(StringRef S);
/// unit (Unicode scalar).
bool isWellFormedUTF8(StringRef S);

/// Replaces any ill-formed subsequences with `u8"\ufffd"`.
std::string sanitizeUTF8(StringRef Text);

} // end namespace unicode
} // end namespace swift

Expand Down
3 changes: 3 additions & 0 deletions include/swift/SymbolGraphGen/SymbolGraphOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ struct SymbolGraphOptions {

/// Whether to emit symbols with SPI information.
bool IncludeSPISymbols;

/// Whether to include documentation for clang nodes or not.
bool IncludeClangDocs;
};

} // end namespace symbolgraphgen
Expand Down
30 changes: 3 additions & 27 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "swift/Basic/QuotedString.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Basic/Unicode.h"
#include "swift/ClangImporter/ClangImporterRequests.h"
#include "swift/Config.h"
#include "swift/Parse/Lexer.h"
Expand Down Expand Up @@ -302,31 +303,6 @@ bool TypeTransformContext::isPrintingSynthesizedExtension() const {
return !Decl.isNull();
}

std::string ASTPrinter::sanitizeUtf8(StringRef Text) {
llvm::SmallString<256> Builder;
Builder.reserve(Text.size());
const llvm::UTF8* Data = reinterpret_cast<const llvm::UTF8*>(Text.begin());
const llvm::UTF8* End = reinterpret_cast<const llvm::UTF8*>(Text.end());
StringRef Replacement = u8"\ufffd";
while (Data < End) {
auto Step = llvm::getNumBytesForUTF8(*Data);
if (Data + Step > End) {
Builder.append(Replacement);
break;
}

if (llvm::isLegalUTF8Sequence(Data, Data + Step)) {
Builder.append(Data, Data + Step);
} else {

// If malformed, add replacement characters.
Builder.append(Replacement);
}
Data += Step;
}
return std::string(Builder.str());
}

void ASTPrinter::anchor() {}

void ASTPrinter::printIndent() {
Expand Down Expand Up @@ -633,9 +609,9 @@ class PrintAST : public ASTVisitor<PrintAST> {
bool FirstLine = true;
for (auto Line : Lines) {
if (FirstLine)
Printer << sanitizeClangDocCommentStyle(ASTPrinter::sanitizeUtf8(Line));
Printer << sanitizeClangDocCommentStyle(unicode::sanitizeUTF8(Line));
else
Printer << ASTPrinter::sanitizeUtf8(Line);
Printer << unicode::sanitizeUTF8(Line);
Printer.printNewline();
FirstLine = false;
}
Expand Down
26 changes: 26 additions & 0 deletions lib/Basic/Unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "swift/Basic/Unicode.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/ConvertUTF.h"

Expand Down Expand Up @@ -128,3 +129,28 @@ bool swift::unicode::isWellFormedUTF8(StringRef S) {
const llvm::UTF8 *begin = S.bytes_begin();
return llvm::isLegalUTF8String(&begin, S.bytes_end());
}

std::string swift::unicode::sanitizeUTF8(StringRef Text) {
llvm::SmallString<256> Builder;
Builder.reserve(Text.size());
const llvm::UTF8* Data = reinterpret_cast<const llvm::UTF8*>(Text.begin());
const llvm::UTF8* End = reinterpret_cast<const llvm::UTF8*>(Text.end());
StringRef Replacement = u8"\ufffd";
while (Data < End) {
auto Step = llvm::getNumBytesForUTF8(*Data);
if (Data + Step > End) {
Builder.append(Replacement);
break;
}

if (llvm::isLegalUTF8Sequence(Data, Data + Step)) {
Builder.append(Data, Data + Step);
} else {

// If malformed, add replacement characters.
Builder.append(Replacement);
}
Data += Step;
}
return std::string(Builder.str());
}
5 changes: 3 additions & 2 deletions lib/IDE/ModuleInterfacePrinting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "swift/AST/PrintOptions.h"
#include "swift/AST/SourceFile.h"
#include "swift/Basic/PrimitiveParsing.h"
#include "swift/Basic/Unicode.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Parse/Token.h"
Expand Down Expand Up @@ -977,7 +978,7 @@ void ClangCommentPrinter::printDeclPost(const Decl *D,
return;

for (auto CommentText : PendingComments) {
*this << " " << ASTPrinter::sanitizeUtf8(CommentText);
*this << " " << unicode::sanitizeUTF8(CommentText);
}
PendingComments.clear();
if (auto ClangN = swift::ide::getEffectiveClangNode(D))
Expand Down Expand Up @@ -1068,7 +1069,7 @@ void ClangCommentPrinter::printComment(StringRef RawText, unsigned StartCol) {
trimLeadingWhitespaceFromLines(RawText, WhitespaceToTrim, Lines);

for (auto Line : Lines) {
*this << ASTPrinter::sanitizeUtf8(Line) << "\n";
*this << unicode::sanitizeUTF8(Line) << "\n";
printIndent();
}
}
Expand Down
39 changes: 39 additions & 0 deletions lib/SymbolGraphGen/Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#include "swift/AST/ParameterList.h"
#include "swift/AST/RawComment.h"
#include "swift/AST/USRGeneration.h"
#include "swift/Basic/PrimitiveParsing.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Unicode.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "AvailabilityMixin.h"
#include "JSON.h"
#include "Symbol.h"
Expand Down Expand Up @@ -193,6 +197,41 @@ const ValueDecl *Symbol::getDeclInheritingDocs() const {
}

void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
if (ClangNode ClangN = VD->getClangNode()) {
if (!Graph->Walker.Options.IncludeClangDocs)
return;

if (auto *ClangD = ClangN.getAsDecl()) {
const clang::ASTContext &ClangContext = ClangD->getASTContext();
const clang::RawComment *RC =
ClangContext.getRawCommentForAnyRedecl(ClangD);
if (!RC || !RC->isDocumentation())
return;

// TODO: Replace this with `getFormattedLines` when it's in and add the
// line and column ranges. Also consider handling cross-language
// hierarchies, ie. if there's no comment on the ObjC decl we should
// look up the hierarchy (and vice versa).
std::string Text = RC->getFormattedText(ClangContext.getSourceManager(),
ClangContext.getDiagnostics());
Text = unicode::sanitizeUTF8(Text);

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

OS.attributeObject("docComment", [&]() {
OS.attributeArray("lines", [&]() {
for (StringRef Line : Lines) {
OS.object([&](){
OS.attribute("text", Line);
});
}
});
});
}
return;
}

const auto *DocCommentProvidingDecl = VD;
if (!Graph->Walker.Options.SkipInheritedDocs) {
DocCommentProvidingDecl = dyn_cast_or_null<ValueDecl>(
Expand Down
Loading