Skip to content

Commit 640318f

Browse files
committed
[clangd] Support parsing comments without ASTContext
This is in preparation for implementing doxygen parsing, see discussion in clangd/clangd#529. Differential Revision: https://reviews.llvm.org/D143112
1 parent a451c3b commit 640318f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

clang-tools-extra/clangd/CodeCompletionStrings.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "CodeCompletionStrings.h"
1010
#include "clang-c/Index.h"
1111
#include "clang/AST/ASTContext.h"
12+
#include "clang/AST/CommentLexer.h"
13+
#include "clang/AST/CommentParser.h"
14+
#include "clang/AST/CommentSema.h"
1215
#include "clang/AST/RawCommentList.h"
1316
#include "clang/Basic/SourceManager.h"
1417
#include "clang/Sema/CodeCompleteConsumer.h"
@@ -316,5 +319,26 @@ std::string getReturnType(const CodeCompletionString &CCS) {
316319
return "";
317320
}
318321

322+
comments::FullComment *parseComment(llvm::StringRef Comment,
323+
llvm::BumpPtrAllocator &Allocator,
324+
comments::CommandTraits &Traits) {
325+
// The comment lexer expects markers, so add them back
326+
auto CommentWithMarkers = "/*" + Comment.str() + "*/";
327+
328+
SourceManagerForFile SourceMgrForFile("mock_file.cpp", CommentWithMarkers);
329+
SourceManager &SourceMgr = SourceMgrForFile.get();
330+
331+
comments::Lexer L(Allocator, SourceMgr.getDiagnostics(), Traits,
332+
SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()),
333+
CommentWithMarkers.data(),
334+
CommentWithMarkers.data() + CommentWithMarkers.size());
335+
comments::Sema S(Allocator, SourceMgr, SourceMgr.getDiagnostics(), Traits,
336+
nullptr);
337+
comments::Parser P(L, S, Allocator, SourceMgr, SourceMgr.getDiagnostics(),
338+
Traits);
339+
340+
return P.parseFullComment();
341+
}
342+
319343
} // namespace clangd
320344
} // namespace clang

clang-tools-extra/clangd/CodeCompletionStrings.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
namespace clang {
2020
class ASTContext;
2121

22+
namespace comments {
23+
class CommandTraits;
24+
class FullComment;
25+
} // namespace comments
26+
2227
namespace clangd {
2328

2429
/// Gets a minimally formatted documentation comment of \p Result, with comment
@@ -67,6 +72,12 @@ std::string formatDocumentation(const CodeCompletionString &CCS,
6772
/// is usually the return type of a function.
6873
std::string getReturnType(const CodeCompletionString &CCS);
6974

75+
/// Parse the \p Comment, storing the result in \p Allocator, assuming
76+
/// that comment markers have already been stripped (e.g. via getDocComment())
77+
comments::FullComment *parseComment(llvm::StringRef Comment,
78+
llvm::BumpPtrAllocator &Allocator,
79+
comments::CommandTraits &Traits);
80+
7081
} // namespace clangd
7182
} // namespace clang
7283

clang/include/clang/Basic/SourceManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ class BeforeThanCompare<SourceRange> {
19711971
};
19721972

19731973
/// SourceManager and necessary dependencies (e.g. VFS, FileManager) for a
1974-
/// single in-memorty file.
1974+
/// single in-memory file.
19751975
class SourceManagerForFile {
19761976
public:
19771977
/// Creates SourceManager and necessary dependencies (e.g. VFS, FileManager).

0 commit comments

Comments
 (0)