Skip to content

Commit 214b3c8

Browse files
authored
Merge pull request #5964 from nkcsgexi/edit-api
2 parents 0dda3db + f1113b2 commit 214b3c8

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

include/swift/Basic/Edit.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===--- Edit.h - Misc edit utilities ---------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Basic/LLVM.h"
14+
namespace swift {
15+
class SourceManager;
16+
class CharSourceRange;
17+
18+
void writeEdit(SourceManager &SM, CharSourceRange Range, StringRef Text,
19+
llvm::raw_ostream &OS);
20+
}

lib/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ add_swift_library(swiftBasic STATIC
6565
DemangleWrappers.cpp
6666
DiagnosticConsumer.cpp
6767
DiverseStack.cpp
68+
Edit.cpp
6869
EditorPlaceholder.cpp
6970
FileSystem.cpp
7071
JSONSerialization.cpp

lib/Basic/Edit.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===--- Edit.cpp - Misc edit utilities -----------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/Support/raw_ostream.h"
14+
#include "swift/Basic/Edit.h"
15+
#include "swift/Basic/SourceManager.h"
16+
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));
24+
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";
34+
}
35+
OS << " },\n";
36+
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "swift/AST/ReferencedNameTracker.h"
3333
#include "swift/AST/TypeRefinementContext.h"
3434
#include "swift/Basic/Dwarf.h"
35+
#include "swift/Basic/Edit.h"
3536
#include "swift/Basic/Fallthrough.h"
3637
#include "swift/Basic/FileSystem.h"
3738
#include "swift/Basic/LLVMContext.h"
@@ -423,7 +424,7 @@ static bool emitReferenceDependencies(DiagnosticEngine &diags,
423424
auto rhsMangledName = mangleTypeAsContext(rhs->first.first);
424425
return lhsMangledName.compare(rhsMangledName);
425426
});
426-
427+
427428
for (auto &entry : sortedMembers) {
428429
assert(entry.first.first != nullptr);
429430
if (entry.first.first->hasAccessibility() &&
@@ -582,7 +583,7 @@ class JSONFixitWriter : public DiagnosticConsumer {
582583
if (!shouldFix(Kind, Info))
583584
return;
584585
for (const auto &Fix : Info.FixIts) {
585-
writeEdit(SM, Fix.getRange(), Fix.getText(), *OSPtr);
586+
swift::writeEdit(SM, Fix.getRange(), Fix.getText(), *OSPtr);
586587
}
587588
}
588589

@@ -662,28 +663,6 @@ class JSONFixitWriter : public DiagnosticConsumer {
662663

663664
return false;
664665
}
665-
666-
void writeEdit(SourceManager &SM, CharSourceRange Range, StringRef Text,
667-
llvm::raw_ostream &OS) {
668-
SourceLoc Loc = Range.getStart();
669-
unsigned BufID = SM.findBufferContainingLoc(Loc);
670-
unsigned Offset = SM.getLocOffsetInBuffer(Loc, BufID);
671-
unsigned Length = Range.getByteLength();
672-
SmallString<200> Path =
673-
StringRef(SM.getIdentifierForBuffer(BufID));
674-
675-
OS << " {\n";
676-
OS << " \"file\": \"";
677-
OS.write_escaped(Path.str()) << "\",\n";
678-
OS << " \"offset\": " << Offset << ",\n";
679-
if (Length != 0)
680-
OS << " \"remove\": " << Length << ",\n";
681-
if (!Text.empty()) {
682-
OS << " \"text\": \"";
683-
OS.write_escaped(Text) << "\",\n";
684-
}
685-
OS << " },\n";
686-
}
687666
};
688667

689668
} // anonymous namespace

0 commit comments

Comments
 (0)