tom-anders created this revision.
tom-anders added a reviewer: kadircet.
Herald added a project: All.
tom-anders requested review of this revision.
Herald added subscribers: cfe-commits, ilya-biryukov.
Herald added a project: clang.

This is in preparation for implementing doxygen parsing, see discussion in 
https://github.com/clangd/clangd/issues/529.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143112

Files:
  clang/include/clang/AST/RawCommentList.h
  clang/include/clang/Basic/SourceManager.h
  clang/lib/AST/RawCommentList.cpp
  clang/unittests/AST/CommentParser.cpp

Index: clang/unittests/AST/CommentParser.cpp
===================================================================
--- clang/unittests/AST/CommentParser.cpp
+++ clang/unittests/AST/CommentParser.cpp
@@ -6,11 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/AST/CommentParser.h"
 #include "clang/AST/Comment.h"
-#include "clang/AST/CommentCommandTraits.h"
-#include "clang/AST/CommentLexer.h"
-#include "clang/AST/CommentSema.h"
+#include "clang/AST/RawCommentList.h"
 #include "clang/Basic/CommentOptions.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticOptions.h"
@@ -28,52 +25,17 @@
 
 namespace {
 
-const bool MY_DEBUG = true;
-
 class CommentParserTest : public ::testing::Test {
 protected:
-  CommentParserTest()
-    : FileMgr(FileMgrOpts),
-      DiagID(new DiagnosticIDs()),
-      Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
-      SourceMgr(Diags, FileMgr),
-      Traits(Allocator, CommentOptions()) {
-  }
+  CommentParserTest() : Traits(Allocator, CommentOptions()) {}
 
-  FileSystemOptions FileMgrOpts;
-  FileManager FileMgr;
-  IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
-  DiagnosticsEngine Diags;
-  SourceManager SourceMgr;
   llvm::BumpPtrAllocator Allocator;
   CommandTraits Traits;
 
-  FullComment *parseString(const char *Source);
-};
-
-FullComment *CommentParserTest::parseString(const char *Source) {
-  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Source);
-  FileID File = SourceMgr.createFileID(std::move(Buf));
-  SourceLocation Begin = SourceMgr.getLocForStartOfFile(File);
-
-  Lexer L(Allocator, Diags, Traits, Begin, Source, Source + strlen(Source));
-
-  Sema S(Allocator, SourceMgr, Diags, Traits, /*PP=*/ nullptr);
-  Parser P(L, S, Allocator, SourceMgr, Diags, Traits);
-  FullComment *FC = P.parseFullComment();
-
-  if (MY_DEBUG) {
-    llvm::errs() << "=== Source:\n" << Source << "\n=== AST:\n";
-    FC->dump();
+  FullComment *parseString(const char *Source) {
+    return RawComment::parse(Source, Allocator, Traits);
   }
-
-  Token Tok;
-  L.lex(Tok);
-  if (Tok.is(tok::eof))
-    return FC;
-  else
-    return nullptr;
-}
+};
 
 ::testing::AssertionResult HasChildCount(const Comment *C, size_t Count) {
   if (!C)
Index: clang/lib/AST/RawCommentList.cpp
===================================================================
--- clang/lib/AST/RawCommentList.cpp
+++ clang/lib/AST/RawCommentList.cpp
@@ -201,26 +201,41 @@
   return BriefTextPtr;
 }
 
+namespace {
+comments::FullComment *
+parseComment(llvm::StringRef Comment, const SourceManager &SourceManager,
+             SourceLocation FileLoc, llvm::BumpPtrAllocator &Allocator,
+             DiagnosticsEngine &Diagnostics, comments::CommandTraits &Traits,
+             const Preprocessor *PP = nullptr, const Decl *D = nullptr) {
+  comments::Lexer L(Allocator, Diagnostics, Traits, FileLoc, Comment.begin(),
+                    Comment.end());
+  comments::Sema S(Allocator, SourceManager, Diagnostics, Traits, PP);
+  S.setDecl(D);
+  comments::Parser P(L, S, Allocator, SourceManager, Diagnostics, Traits);
+
+  return P.parseFullComment();
+}
+} // namespace
+
+comments::FullComment *RawComment::parse(llvm::StringRef Comment,
+                                         llvm::BumpPtrAllocator &Allocator,
+                                         comments::CommandTraits &Traits) {
+  SourceManagerForFile SourceMgrForFile("mock_file.cpp", Comment);
+  SourceManager &SourceMgr = SourceMgrForFile.get();
+  return parseComment(Comment, SourceMgr,
+                      SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()),
+                      Allocator, SourceMgr.getDiagnostics(), Traits);
+}
+
 comments::FullComment *RawComment::parse(const ASTContext &Context,
                                          const Preprocessor *PP,
                                          const Decl *D) const {
   // Lazily initialize RawText using the accessor before using it.
   (void)getRawText(Context.getSourceManager());
-
-  comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(),
-                    Context.getCommentCommandTraits(),
-                    getSourceRange().getBegin(),
-                    RawText.begin(), RawText.end());
-  comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
-                   Context.getDiagnostics(),
-                   Context.getCommentCommandTraits(),
-                   PP);
-  S.setDecl(D);
-  comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
-                     Context.getDiagnostics(),
-                     Context.getCommentCommandTraits());
-
-  return P.parseFullComment();
+  return parseComment(RawText, Context.getSourceManager(),
+                      getSourceRange().getBegin(), Context.getAllocator(),
+                      Context.getDiagnostics(),
+                      Context.getCommentCommandTraits(), PP, D);
 }
 
 static bool onlyWhitespaceBetween(SourceManager &SM,
Index: clang/include/clang/Basic/SourceManager.h
===================================================================
--- clang/include/clang/Basic/SourceManager.h
+++ clang/include/clang/Basic/SourceManager.h
@@ -1944,7 +1944,7 @@
 };
 
 /// SourceManager and necessary dependencies (e.g. VFS, FileManager) for a
-/// single in-memorty file.
+/// single in-memory file.
 class SourceManagerForFile {
 public:
   /// Creates SourceManager and necessary dependencies (e.g. VFS, FileManager).
Index: clang/include/clang/AST/RawCommentList.h
===================================================================
--- clang/include/clang/AST/RawCommentList.h
+++ clang/include/clang/AST/RawCommentList.h
@@ -27,6 +27,7 @@
 
 namespace comments {
   class FullComment;
+  class CommandTraits;
 } // end namespace comments
 
 class RawComment {
@@ -154,6 +155,11 @@
   std::vector<CommentLine> getFormattedLines(const SourceManager &SourceMgr,
                                              DiagnosticsEngine &Diags) const;
 
+  /// Parse the \p Comment, storing the result in \p Allocator
+  static comments::FullComment *parse(llvm::StringRef Comment,
+                                      llvm::BumpPtrAllocator &Allocator,
+                                      comments::CommandTraits &Traits);
+
   /// Parse the comment, assuming it is attached to decl \c D.
   comments::FullComment *parse(const ASTContext &Context,
                                const Preprocessor *PP, const Decl *D) const;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to