Skip to content

Commit 5d11fe6

Browse files
committed
[Localization] Extract def-to-yaml conversion logic into DefToYAMLConverter to ease testing
1 parent b51b1f1 commit 5d11fe6

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

include/swift/Localization/LocalizationFormat.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_LOCALIZATIONFORMAT_H
1818
#define SWIFT_LOCALIZATIONFORMAT_H
1919

20+
#include "llvm/ADT/ArrayRef.h"
2021
#include "llvm/ADT/Hashing.h"
2122
#include "llvm/ADT/Optional.h"
2223
#include "llvm/ADT/STLExtras.h"
@@ -42,6 +43,20 @@ namespace diag {
4243

4344
using namespace llvm::support;
4445

46+
class DefToYAMLConverter {
47+
llvm::ArrayRef<const char *> IDs;
48+
llvm::ArrayRef<const char *> Messages;
49+
50+
public:
51+
DefToYAMLConverter(llvm::ArrayRef<const char *> ids,
52+
llvm::ArrayRef<const char *> messages)
53+
: IDs(ids), Messages(messages) {
54+
assert(IDs.size() == Messages.size());
55+
}
56+
57+
void convert(llvm::raw_ostream &out);
58+
};
59+
4560
class LocalizationWriterInfo {
4661
public:
4762
using key_type = uint32_t;

lib/Localization/LocalizationFormat.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#include "swift/Basic/Range.h"
1718
#include "swift/Localization/LocalizationFormat.h"
1819
#include "llvm/ADT/Optional.h"
1920
#include "llvm/ADT/SmallString.h"
@@ -190,5 +191,28 @@ operator>>(LocalizationInput &yin, T &diagnostics) {
190191
return yin;
191192
}
192193

194+
void DefToYAMLConverter::convert(llvm::raw_ostream &out) {
195+
for (auto i : swift::indices(IDs)) {
196+
out << "- id: " << IDs[i] << "\n";
197+
198+
const std::string &msg = Messages[i];
199+
200+
out << " msg: \"";
201+
// Add an escape character before a double quote `"` or a backslash `\`.
202+
for (unsigned j = 0; j < msg.length(); ++j) {
203+
if (msg[j] == '"') {
204+
out << '\\';
205+
out << '"';
206+
} else if (msg[j] == '\\') {
207+
out << '\\';
208+
out << '\\';
209+
} else {
210+
out << msg[j];
211+
}
212+
}
213+
out << "\"\r\n";
214+
}
215+
}
216+
193217
} // namespace diag
194218
} // namespace swift

tools/swift-def-to-yaml-converter/swift-def-to-yaml-converter.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Basic/LLVMInitialize.h"
18+
#include "swift/Localization/LocalizationFormat.h"
19+
#include "llvm/ADT/ArrayRef.h"
1820
#include "llvm/ADT/SmallString.h"
1921
#include "llvm/ADT/StringExtras.h"
2022
#include "llvm/Support/CommandLine.h"
@@ -89,25 +91,12 @@ int main(int argc, char *argv[]) {
8991
return EXIT_FAILURE;
9092
}
9193

92-
for (unsigned i = 0; i < LocalDiagID::NumDiags; ++i) {
93-
OS << "- id: " << diagnosticID[i] << "\n";
94-
std::string msg = diagnosticMessages[i];
95-
std::string finalMsg = "";
96-
97-
// Add an escape character before a double quote `"` or a backslash `\`.
98-
for (unsigned j = 0; j < msg.length(); ++j) {
99-
if (msg[j] == '"') {
100-
finalMsg += '\\';
101-
finalMsg += '"';
102-
} else if (msg[j] == '\\') {
103-
finalMsg += '\\';
104-
finalMsg += '\\';
105-
} else {
106-
finalMsg += msg[j];
107-
}
108-
}
109-
OS << " msg: \"" << finalMsg << "\"\r\n";
110-
}
94+
llvm::ArrayRef<const char *> ids(diagnosticID, LocalDiagID::NumDiags);
95+
llvm::ArrayRef<const char *> messages(diagnosticMessages,
96+
LocalDiagID::NumDiags);
97+
98+
swift::diag::DefToYAMLConverter converter(ids, messages);
99+
converter.convert(OS);
111100

112101
return EXIT_SUCCESS;
113102
}

0 commit comments

Comments
 (0)