|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "CodeCompletionStrings.h" |
| 10 | +#include "Config.h" |
| 11 | +#include "SymbolDocumentation.h" |
10 | 12 | #include "clang-c/Index.h" |
11 | 13 | #include "clang/AST/ASTContext.h" |
| 14 | +#include "clang/AST/Comment.h" |
| 15 | +#include "clang/AST/Decl.h" |
12 | 16 | #include "clang/AST/RawCommentList.h" |
13 | 17 | #include "clang/Basic/SourceManager.h" |
14 | 18 | #include "clang/Sema/CodeCompleteConsumer.h" |
15 | 19 | #include "llvm/Support/Compiler.h" |
16 | 20 | #include "llvm/Support/JSON.h" |
| 21 | +#include "llvm/Support/raw_ostream.h" |
17 | 22 | #include <limits> |
18 | 23 | #include <utility> |
19 | 24 |
|
@@ -100,16 +105,51 @@ std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl) { |
100 | 105 | // the comments for namespaces. |
101 | 106 | return ""; |
102 | 107 | } |
103 | | - const RawComment *RC = getCompletionComment(Ctx, &Decl); |
104 | | - if (!RC) |
105 | | - return ""; |
106 | | - // Sanity check that the comment does not come from the PCH. We choose to not |
107 | | - // write them into PCH, because they are racy and slow to load. |
108 | | - assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc())); |
109 | | - std::string Doc = |
110 | | - RC->getFormattedText(Ctx.getSourceManager(), Ctx.getDiagnostics()); |
111 | | - if (!looksLikeDocComment(Doc)) |
112 | | - return ""; |
| 108 | + |
| 109 | + const RawComment *RC = nullptr; |
| 110 | + const Config &Cfg = Config::current(); |
| 111 | + |
| 112 | + std::string Doc; |
| 113 | + |
| 114 | + if (Cfg.Documentation.CommentFormat == Config::CommentFormatPolicy::Doxygen && |
| 115 | + isa<ParmVarDecl>(Decl)) { |
| 116 | + // Parameters are documented in their declaration context (function or |
| 117 | + // template function). |
| 118 | + const NamedDecl *ND = dyn_cast<NamedDecl>(Decl.getDeclContext()); |
| 119 | + if (!ND) |
| 120 | + return ""; |
| 121 | + |
| 122 | + RC = getCompletionComment(Ctx, ND); |
| 123 | + if (!RC) |
| 124 | + return ""; |
| 125 | + |
| 126 | + // Sanity check that the comment does not come from the PCH. We choose to |
| 127 | + // not write them into PCH, because they are racy and slow to load. |
| 128 | + assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc())); |
| 129 | + |
| 130 | + comments::FullComment *FC = RC->parse(Ctx, /*PP=*/nullptr, ND); |
| 131 | + if (!FC) |
| 132 | + return ""; |
| 133 | + |
| 134 | + SymbolDocCommentVisitor V(FC, Ctx.getLangOpts().CommentOpts); |
| 135 | + std::string RawDoc; |
| 136 | + llvm::raw_string_ostream OS(RawDoc); |
| 137 | + |
| 138 | + V.parameterDocToString(dyn_cast<ParmVarDecl>(&Decl)->getName(), OS); |
| 139 | + |
| 140 | + Doc = StringRef(RawDoc).trim().str(); |
| 141 | + } else { |
| 142 | + RC = getCompletionComment(Ctx, &Decl); |
| 143 | + if (!RC) |
| 144 | + return ""; |
| 145 | + // Sanity check that the comment does not come from the PCH. We choose to |
| 146 | + // not write them into PCH, because they are racy and slow to load. |
| 147 | + assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc())); |
| 148 | + Doc = RC->getFormattedText(Ctx.getSourceManager(), Ctx.getDiagnostics()); |
| 149 | + if (!looksLikeDocComment(Doc)) |
| 150 | + return ""; |
| 151 | + } |
| 152 | + |
113 | 153 | // Clang requires source to be UTF-8, but doesn't enforce this in comments. |
114 | 154 | if (!llvm::json::isUTF8(Doc)) |
115 | 155 | Doc = llvm::json::fixUTF8(Doc); |
|
0 commit comments