This is an archive of the discontinued LLVM Phabricator instance.

Add option to reparse an ASTUnit keeping its SourceManager
Needs ReviewPublic

Authored by giulianobelinassi on Oct 12 2022, 8:38 AM.
This revision needs review, but there are no reviewers specified.

Details

Reviewers
None
Summary

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@suse.de>

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2022, 8:38 AM
giulianobelinassi requested review of this revision.Oct 12 2022, 8:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2022, 8:38 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript