24
24
#include " llvm/Support/FileSystem.h"
25
25
#include " llvm/Support/MemoryBuffer.h"
26
26
#include " llvm/Support/Path.h"
27
- #include " llvm/Support/YAMLParser.h"
28
- #include " llvm/Support/YAMLTraits.h"
29
27
#include < cstdint>
28
+ #include < map>
30
29
#include < string>
31
30
#include < system_error>
32
31
#include < type_traits>
@@ -47,24 +46,6 @@ static constexpr const char *const diagnosticNameStrings[] = {
47
46
48
47
} // namespace
49
48
50
- namespace llvm {
51
- namespace yaml {
52
-
53
- template <> struct ScalarEnumerationTraits <LocalDiagID> {
54
- static void enumeration (IO &io, LocalDiagID &value) {
55
- #define DIAG (KIND, ID, Options, Text, Signature ) \
56
- io.enumCase (value, #ID, LocalDiagID::ID);
57
- #include " swift/AST/DiagnosticsAll.def"
58
- // Ignore diagnostic IDs that are available in the YAML file and not
59
- // available in the `.def` file.
60
- if (io.matchEnumFallback ())
61
- value = LocalDiagID::NumDiags;
62
- }
63
- };
64
-
65
- } // namespace yaml
66
- } // namespace llvm
67
-
68
49
namespace swift {
69
50
namespace diag {
70
51
@@ -149,38 +130,6 @@ SerializedLocalizationProducer::getMessage(swift::DiagID id) const {
149
130
return {(const char *)value.getDataPtr (), value.getDataLen ()};
150
131
}
151
132
152
- YAMLLocalizationProducer::YAMLLocalizationProducer (llvm::StringRef filePath,
153
- bool printDiagnosticNames)
154
- : LocalizationProducer(printDiagnosticNames), filePath(filePath) {
155
- }
156
-
157
- bool YAMLLocalizationProducer::initializeImpl () {
158
- auto FileBufOrErr = llvm::MemoryBuffer::getFileOrSTDIN (filePath);
159
- llvm::MemoryBuffer *document = FileBufOrErr->get ();
160
- diag::LocalizationInput yin (document->getBuffer ());
161
- yin >> diagnostics;
162
- unknownIDs = std::move (yin.unknownIDs );
163
- return true ;
164
- }
165
-
166
- llvm::StringRef YAMLLocalizationProducer::getMessage (swift::DiagID id) const {
167
- return diagnostics[(unsigned )id];
168
- }
169
-
170
- void YAMLLocalizationProducer::forEachAvailable (
171
- llvm::function_ref<void (swift::DiagID, llvm::StringRef)> callback) {
172
- initializeIfNeeded ();
173
- if (getState () == FailedInitialization) {
174
- return ;
175
- }
176
-
177
- for (uint32_t i = 0 , n = diagnostics.size (); i != n; ++i) {
178
- auto translation = diagnostics[i];
179
- if (!translation.empty ())
180
- callback (static_cast <swift::DiagID>(i), translation);
181
- }
182
- }
183
-
184
133
std::unique_ptr<LocalizationProducer>
185
134
LocalizationProducer::producerFor (llvm::StringRef locale, llvm::StringRef path,
186
135
bool printDiagnosticNames) {
@@ -189,21 +138,13 @@ LocalizationProducer::producerFor(llvm::StringRef locale, llvm::StringRef path,
189
138
llvm::sys::path::replace_extension (filePath, " .db" );
190
139
191
140
// If the serialized diagnostics file not available,
192
- // fallback to the `YAML ` file.
141
+ // fallback to the `.strings ` file.
193
142
if (llvm::sys::fs::exists (filePath)) {
194
143
if (auto file = llvm::MemoryBuffer::getFile (filePath)) {
195
144
return std::make_unique<diag::SerializedLocalizationProducer>(
196
145
std::move (file.get ()), printDiagnosticNames);
197
146
}
198
147
} else {
199
- llvm::sys::path::replace_extension (filePath, " .yaml" );
200
- // In case of missing localization files, we should fallback to messages
201
- // from `.def` files.
202
- if (llvm::sys::fs::exists (filePath)) {
203
- return std::make_unique<diag::YAMLLocalizationProducer>(
204
- filePath.str (), printDiagnosticNames);
205
- }
206
-
207
148
llvm::sys::path::replace_extension (filePath, " .strings" );
208
149
if (llvm::sys::fs::exists (filePath)) {
209
150
return std::make_unique<diag::StringsLocalizationProducer>(
@@ -214,86 +155,6 @@ LocalizationProducer::producerFor(llvm::StringRef locale, llvm::StringRef path,
214
155
return std::unique_ptr<LocalizationProducer>();
215
156
}
216
157
217
- llvm::Optional<uint32_t > LocalizationInput::readID (llvm::yaml::IO &io) {
218
- LocalDiagID diagID;
219
- io.mapRequired (" id" , diagID);
220
- if (diagID == LocalDiagID::NumDiags)
221
- return llvm::None;
222
- return static_cast <uint32_t >(diagID);
223
- }
224
-
225
- template <typename T, typename Context>
226
- typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value, void >::type
227
- readYAML (llvm::yaml::IO &io, T &Seq, T &unknownIDs, bool , Context &Ctx) {
228
- unsigned count = io.beginSequence ();
229
- if (count) {
230
- Seq.resize (LocalDiagID::NumDiags);
231
- }
232
-
233
- for (unsigned i = 0 ; i < count; ++i) {
234
- void *SaveInfo;
235
- if (io.preflightElement (i, SaveInfo)) {
236
- io.beginMapping ();
237
-
238
- // If the current diagnostic ID is available in YAML and in `.def`, add it
239
- // to the diagnostics array. Otherwise, re-parse the current diagnostic
240
- // id as a string and store it in `unknownIDs` array.
241
- if (auto id = LocalizationInput::readID (io)) {
242
- // YAML file isn't guaranteed to have diagnostics in order of their
243
- // declaration in `.def` files, to accommodate that we need to leave
244
- // holes in diagnostic array for diagnostics which haven't yet been
245
- // localized and for the ones that have `id` indicates their position.
246
- io.mapRequired (" msg" , Seq[*id]);
247
- } else {
248
- std::string unknownID, message;
249
- // Read "raw" id since it doesn't exist in `.def` file.
250
- io.mapRequired (" id" , unknownID);
251
- io.mapRequired (" msg" , message);
252
- unknownIDs.push_back (unknownID);
253
- }
254
- io.endMapping ();
255
- io.postflightElement (SaveInfo);
256
- }
257
- }
258
- io.endSequence ();
259
- }
260
-
261
- template <typename T>
262
- typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
263
- LocalizationInput &>::type
264
- operator >>(LocalizationInput &yin, T &diagnostics) {
265
- llvm::yaml::EmptyContext Ctx;
266
- if (yin.setCurrentDocument ()) {
267
- // If YAML file's format doesn't match the current format in
268
- // DiagnosticMessageFormat, will throw an error.
269
- readYAML (yin, diagnostics, yin.unknownIDs , true , Ctx);
270
- }
271
- return yin;
272
- }
273
-
274
- void DefToYAMLConverter::convert (llvm::raw_ostream &out) {
275
- for (auto i : swift::indices (IDs)) {
276
- out << " - id: " << IDs[i] << " \n " ;
277
-
278
- const std::string &msg = Messages[i];
279
-
280
- out << " msg: \" " ;
281
- // Add an escape character before a double quote `"` or a backslash `\`.
282
- for (unsigned j = 0 ; j < msg.length (); ++j) {
283
- if (msg[j] == ' "' ) {
284
- out << ' \\ ' ;
285
- out << ' "' ;
286
- } else if (msg[j] == ' \\ ' ) {
287
- out << ' \\ ' ;
288
- out << ' \\ ' ;
289
- } else {
290
- out << msg[j];
291
- }
292
- }
293
- out << " \"\r\n " ;
294
- }
295
- }
296
-
297
158
void DefToStringsConverter::convert (llvm::raw_ostream &out) {
298
159
// "<id>" = "<msg>";
299
160
for (auto i : swift::indices (IDs)) {
0 commit comments