Skip to content

Commit c13af38

Browse files
authored
Merge pull request #6008 from nkcsgexi/edits-serialization
2 parents 9a95b67 + 7a58f00 commit c13af38

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

include/swift/Basic/Edit.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,23 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#ifndef SWIFT_BASIC_EDIT_H
14+
#define SWIFT_BASIC_EDIT_H
15+
1316
#include "swift/Basic/LLVM.h"
17+
#include "swift/Basic/SourceLoc.h"
18+
1419
namespace swift {
1520
class SourceManager;
1621
class CharSourceRange;
1722

18-
void writeEdit(SourceManager &SM, CharSourceRange Range, StringRef Text,
19-
llvm::raw_ostream &OS);
23+
struct SingleEdit {
24+
SourceManager &SM;
25+
CharSourceRange Range;
26+
std::string Text;
27+
};
28+
29+
void writeEditsInJson(ArrayRef<SingleEdit> Edits, llvm::raw_ostream &OS);
2030
}
31+
32+
#endif // SWIFT_BASIC_EDIT_H

lib/Basic/Edit.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,30 @@
1414
#include "swift/Basic/Edit.h"
1515
#include "swift/Basic/SourceManager.h"
1616

17-
void swift::writeEdit(SourceManager &SM, CharSourceRange Range, StringRef Text,
18-
llvm::raw_ostream &OS) {
19-
SourceLoc Loc = Range.getStart();
20-
unsigned BufID = SM.findBufferContainingLoc(Loc);
21-
unsigned Offset = SM.getLocOffsetInBuffer(Loc, BufID);
22-
unsigned Length = Range.getByteLength();
23-
StringRef Path(SM.getIdentifierForBuffer(BufID));
17+
void swift::
18+
writeEditsInJson(ArrayRef<SingleEdit> AllEdits, llvm::raw_ostream &OS) {
19+
OS << "[\n";
20+
for (auto &Edit : AllEdits) {
21+
SourceManager &SM = Edit.SM;
22+
CharSourceRange Range = Edit.Range;
23+
StringRef Text = Edit.Text;
24+
SourceLoc Loc = Range.getStart();
25+
unsigned BufID = SM.findBufferContainingLoc(Loc);
26+
unsigned Offset = SM.getLocOffsetInBuffer(Loc, BufID);
27+
unsigned Length = Range.getByteLength();
28+
StringRef Path(SM.getIdentifierForBuffer(BufID));
2429

25-
OS << " {\n";
26-
OS << " \"file\": \"";
27-
OS.write_escaped(Path) << "\",\n";
28-
OS << " \"offset\": " << Offset << ",\n";
29-
if (Length != 0)
30-
OS << " \"remove\": " << Length << ",\n";
31-
if (!Text.empty()) {
32-
OS << " \"text\": \"";
33-
OS.write_escaped(Text) << "\",\n";
30+
OS << " {\n";
31+
OS << " \"file\": \"";
32+
OS.write_escaped(Path) << "\",\n";
33+
OS << " \"offset\": " << Offset << ",\n";
34+
if (Length != 0)
35+
OS << " \"remove\": " << Length << ",\n";
36+
if (!Text.empty()) {
37+
OS << " \"text\": \"";
38+
OS.write_escaped(Text) << "\",\n";
39+
}
40+
OS << " },\n";
3441
}
35-
OS << " },\n";
42+
OS << "]\n";
3643
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,26 +564,25 @@ namespace {
564564
class JSONFixitWriter : public DiagnosticConsumer {
565565
std::unique_ptr<llvm::raw_ostream> OSPtr;
566566
bool FixitAll;
567+
std::vector<SingleEdit> AllEdits;
567568

568569
public:
569570
JSONFixitWriter(std::unique_ptr<llvm::raw_ostream> OS,
570571
const DiagnosticOptions &DiagOpts)
571572
: OSPtr(std::move(OS)),
572-
FixitAll(DiagOpts.FixitCodeForAllDiagnostics) {
573-
*OSPtr << "[\n";
574-
}
573+
FixitAll(DiagOpts.FixitCodeForAllDiagnostics) {}
574+
575575
~JSONFixitWriter() {
576-
*OSPtr << "]\n";
576+
swift::writeEditsInJson(llvm::makeArrayRef(AllEdits), *OSPtr);
577577
}
578-
579578
private:
580579
void handleDiagnostic(SourceManager &SM, SourceLoc Loc,
581580
DiagnosticKind Kind, StringRef Text,
582581
const DiagnosticInfo &Info) override {
583582
if (!shouldFix(Kind, Info))
584583
return;
585584
for (const auto &Fix : Info.FixIts) {
586-
swift::writeEdit(SM, Fix.getRange(), Fix.getText(), *OSPtr);
585+
AllEdits.push_back({SM, Fix.getRange(), Fix.getText()});
587586
}
588587
}
589588

0 commit comments

Comments
 (0)