[PATCH] D135784: Add option to reparse an ASTUnit keeping its SourceManager

Giuliano Belinassi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 12 08:38:35 PDT 2022


giulianobelinassi created this revision.
Herald added a project: All.
giulianobelinassi requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When issuing a reparse, the SourceManager is then rebuilt from the
FileMgr object it had in ASTUnit. This, however, issues an reload
of the files MemoryBuffer which could have been changed by some
analysis process.

For example, an tooling application can call overrideFileContents with
a new MemoryBuffer with modified pieces of code, and then issue a
reparse to either rebuild the tree or update the SourceLocations. This
is useful when used with the Rewriter object because one can do:

  std::string *modified_str = new std::string(rewritebuf->begin(), rewritebuf->end());
  auto new_membuff = llvm::MemoryBuffer::getMemBuffer(*modified_str);
  SM.overrideFileContents(file_entry, std::move(new_membuff));
  
  AU->Reparse(..., /*KeepSourceManager=*/true);

And then the modifications to the file is loaded into the AU instance
of ASTUnit object. The test case in this patch reflects this use case.

Signed-off-by: Giuliano Belinassi <gbelinassi at suse.de>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135784

Files:
  clang/include/clang/Frontend/ASTUnit.h
  clang/lib/Frontend/ASTUnit.cpp
  clang/unittests/Frontend/ASTUnitTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135784.467163.patch
Type: text/x-patch
Size: 7141 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221012/5e1930be/attachment.bin>


More information about the cfe-commits mailing list