Skip to content

Commit bc5e4d7

Browse files
committed
[incrParse] Compute byte offsets of pre-edit file based on that file
1 parent a1ff223 commit bc5e4d7

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

Diff for: test/incrParse/simple.swift

+8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %incparse-test %s --test-case REPLACE
3+
// RUN: %incparse-test %s --test-case REPLACE_BY_LONGER
4+
// RUN: %incparse-test %s --test-case REPLACE_BY_SHORTER
5+
// RUN: %incparse-test %s --test-case INSERT
6+
// RUN: %incparse-test %s --test-case REMOVE
37

48
func foo() {
59
}
610

711
_ = <<REPLACE<6|||7>>>
12+
_ = <<REPLACE_BY_LONGER<6|||"Hello World">>>
13+
_ = <<REPLACE_BY_SHORTER<"Hello again"|||"a">>>
14+
<<INSERT<|||foo()>>>
15+
<<REMOVE<print("abc")|||>>>
816
_ = 1

Diff for: tools/swift-syntax-test/swift-syntax-test.cpp

+27-9
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ OldSyntaxTreeFilename("old-syntax-tree-filename",
108108
llvm::cl::desc("Path to the serialized syntax tree of "
109109
"the pre-edit file"));
110110

111+
static llvm::cl::opt<std::string>
112+
OldSourceFilename("old-source-filename",
113+
llvm::cl::desc("Path to the pre-edit source file to "
114+
"translate line:column edits into the "
115+
"file's byte offsets"));
116+
111117
static llvm::cl::list<std::string>
112118
IncrementalEdits("incremental-edit",
113119
llvm::cl::desc("An edit that was applied to reach the input "
@@ -197,8 +203,17 @@ getTokensFromFile(const StringRef InputFilename,
197203
void anchorForGetMainExecutable() {}
198204

199205
bool parseIncrementalEditArguments(SyntaxParsingCache *Cache,
200-
SourceManager &SourceMgr,
201-
unsigned BufferID) {
206+
StringRef OldFileName) {
207+
// Get a source manager for the old file
208+
InputFile OldFile = InputFile(OldFileName, true);
209+
auto OldFileBufferOrErrror = llvm::MemoryBuffer::getFileOrSTDIN(OldFileName);
210+
if (!OldFileBufferOrErrror) {
211+
llvm::errs() << "Unable to open old source file";
212+
return false;
213+
}
214+
SourceManager SourceMgr;
215+
unsigned BufferID = SourceMgr.addNewSourceBuffer(std::move(OldFileBufferOrErrror.get()));
216+
202217
// Parse the source edits
203218
for (auto EditPattern : options::IncrementalEdits) {
204219
llvm::Regex MatchRegex("([0-9]+):([0-9]+)-([0-9]+):([0-9]+)=(.*)");
@@ -330,6 +345,16 @@ int parseFile(const char *MainExecutablePath, const StringRef InputFileName,
330345
SyntaxCache = new SyntaxParsingCache(OldSyntaxTree.getValue());
331346

332347
SyntaxCache->recordReuseInformation();
348+
349+
if (options::OldSourceFilename.empty()) {
350+
llvm::errs() << "The old syntax file must be provided to translate "
351+
"line:column edits to byte offsets";
352+
return EXIT_FAILURE;
353+
}
354+
if (!parseIncrementalEditArguments(SyntaxCache,
355+
options::OldSourceFilename)) {
356+
return EXIT_FAILURE;
357+
}
333358
}
334359

335360
// Set up the compiler invocation
@@ -356,13 +381,6 @@ int parseFile(const char *MainExecutablePath, const StringRef InputFileName,
356381
assert(BufferIDs.size() == 1 && "Only expecting to process one source file");
357382
unsigned BufferID = BufferIDs.front();
358383

359-
if (SyntaxCache) {
360-
if (!parseIncrementalEditArguments(SyntaxCache, Instance.getSourceMgr(),
361-
BufferID)) {
362-
return EXIT_FAILURE;
363-
}
364-
}
365-
366384
// Parse the actual source file
367385
Instance.performParseOnly();
368386

Diff for: utils/incrparse_test_util.py

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ def main():
154154
serialized_pre_edit_filename,
155155
'-input-source-filename',
156156
post_edit_file.name,
157+
'--old-source-filename',
158+
pre_edit_file.name,
157159
'-output-filename',
158160
serialized_incr_filename
159161
] + incremental_edit_args)

0 commit comments

Comments
 (0)