|
16 | 16 | #include "swift/AST/ParameterList.h"
|
17 | 17 | #include "swift/AST/RawComment.h"
|
18 | 18 | #include "swift/AST/USRGeneration.h"
|
| 19 | +#include "swift/Basic/PrimitiveParsing.h" |
19 | 20 | #include "swift/Basic/SourceManager.h"
|
| 21 | +#include "swift/Basic/Unicode.h" |
| 22 | +#include "clang/AST/ASTContext.h" |
| 23 | +#include "clang/AST/Decl.h" |
20 | 24 | #include "AvailabilityMixin.h"
|
21 | 25 | #include "JSON.h"
|
22 | 26 | #include "Symbol.h"
|
@@ -193,6 +197,41 @@ const ValueDecl *Symbol::getDeclInheritingDocs() const {
|
193 | 197 | }
|
194 | 198 |
|
195 | 199 | void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
|
| 200 | + if (ClangNode ClangN = VD->getClangNode()) { |
| 201 | + if (!Graph->Walker.Options.IncludeClangDocs) |
| 202 | + return; |
| 203 | + |
| 204 | + if (auto *ClangD = ClangN.getAsDecl()) { |
| 205 | + const clang::ASTContext &ClangContext = ClangD->getASTContext(); |
| 206 | + const clang::RawComment *RC = |
| 207 | + ClangContext.getRawCommentForAnyRedecl(ClangD); |
| 208 | + if (!RC || !RC->isDocumentation()) |
| 209 | + return; |
| 210 | + |
| 211 | + // TODO: Replace this with `getFormattedLines` when it's in and add the |
| 212 | + // line and column ranges. Also consider handling cross-language |
| 213 | + // hierarchies, ie. if there's no comment on the ObjC decl we should |
| 214 | + // look up the hierarchy (and vice versa). |
| 215 | + std::string Text = RC->getFormattedText(ClangContext.getSourceManager(), |
| 216 | + ClangContext.getDiagnostics()); |
| 217 | + Text = unicode::sanitizeUTF8(Text); |
| 218 | + |
| 219 | + SmallVector<StringRef, 8> Lines; |
| 220 | + splitIntoLines(Text, Lines); |
| 221 | + |
| 222 | + OS.attributeObject("docComment", [&]() { |
| 223 | + OS.attributeArray("lines", [&]() { |
| 224 | + for (StringRef Line : Lines) { |
| 225 | + OS.object([&](){ |
| 226 | + OS.attribute("text", Line); |
| 227 | + }); |
| 228 | + } |
| 229 | + }); |
| 230 | + }); |
| 231 | + } |
| 232 | + return; |
| 233 | + } |
| 234 | + |
196 | 235 | const auto *DocCommentProvidingDecl = VD;
|
197 | 236 | if (!Graph->Walker.Options.SkipInheritedDocs) {
|
198 | 237 | DocCommentProvidingDecl = dyn_cast_or_null<ValueDecl>(
|
|
0 commit comments