Skip to content

Commit 10b609c

Browse files
committed
[Localization] Remove YAML format and tool
1 parent 5a164c5 commit 10b609c

File tree

7 files changed

+9
-337
lines changed

7 files changed

+9
-337
lines changed

include/swift/Localization/LocalizationFormat.h

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#include "llvm/Support/MemoryBuffer.h"
2828
#include "llvm/Support/OnDiskHashTable.h"
2929
#include "llvm/Support/StringSaver.h"
30-
#include "llvm/Support/YAMLParser.h"
31-
#include "llvm/Support/YAMLTraits.h"
3230
#include "llvm/Support/raw_ostream.h"
3331
#include <cstdint>
3432
#include <memory>
@@ -50,20 +48,6 @@ enum LocalizationProducerState : uint8_t {
5048
FailedInitialization
5149
};
5250

53-
class DefToYAMLConverter {
54-
llvm::ArrayRef<const char *> IDs;
55-
llvm::ArrayRef<const char *> Messages;
56-
57-
public:
58-
DefToYAMLConverter(llvm::ArrayRef<const char *> ids,
59-
llvm::ArrayRef<const char *> messages)
60-
: IDs(ids), Messages(messages) {
61-
assert(IDs.size() == Messages.size());
62-
}
63-
64-
void convert(llvm::raw_ostream &out);
65-
};
66-
6751
class DefToStringsConverter {
6852
llvm::ArrayRef<const char *> IDs;
6953
llvm::ArrayRef<const char *> Messages;
@@ -195,9 +179,9 @@ class LocalizationProducer {
195179
llvm::StringRef defaultMessage);
196180

197181
/// \returns a `SerializedLocalizationProducer` pointer if the serialized
198-
/// diagnostics file available, otherwise returns a `YAMLLocalizationProducer`
199-
/// if the `YAML` file is available. If both files aren't available returns a
200-
/// `nullptr`.
182+
/// diagnostics file available, otherwise returns a
183+
/// `StringsLocalizationProducer` if the `.strings` file is available. If both
184+
/// files aren't available returns a `nullptr`.
201185
static std::unique_ptr<LocalizationProducer>
202186
producerFor(llvm::StringRef locale, llvm::StringRef path,
203187
bool printDiagnosticNames);
@@ -218,27 +202,6 @@ class LocalizationProducer {
218202
virtual llvm::StringRef getMessage(swift::DiagID id) const = 0;
219203
};
220204

221-
class YAMLLocalizationProducer final : public LocalizationProducer {
222-
std::vector<std::string> diagnostics;
223-
std::string filePath;
224-
225-
public:
226-
/// The diagnostics IDs that are no longer available in `.def`
227-
std::vector<std::string> unknownIDs;
228-
explicit YAMLLocalizationProducer(llvm::StringRef filePath,
229-
bool printDiagnosticNames = false);
230-
231-
/// Iterate over all of the available (non-empty) translations
232-
/// maintained by this producer, callback gets each translation
233-
/// with its unique identifier.
234-
void forEachAvailable(
235-
llvm::function_ref<void(swift::DiagID, llvm::StringRef)> callback);
236-
237-
protected:
238-
bool initializeImpl() override;
239-
llvm::StringRef getMessage(swift::DiagID id) const override;
240-
};
241-
242205
class StringsLocalizationProducer final : public LocalizationProducer {
243206
std::string filePath;
244207

@@ -281,32 +244,6 @@ class SerializedLocalizationProducer final : public LocalizationProducer {
281244
llvm::StringRef getMessage(swift::DiagID id) const override;
282245
};
283246

284-
class LocalizationInput : public llvm::yaml::Input {
285-
using Input::Input;
286-
287-
/// Read diagnostics in the YAML file iteratively
288-
template <typename T, typename Context>
289-
friend typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
290-
void>::type
291-
readYAML(llvm::yaml::IO &io, T &Seq, T &unknownIDs, bool, Context &Ctx);
292-
293-
template <typename T>
294-
friend typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
295-
LocalizationInput &>::type
296-
operator>>(LocalizationInput &yin, T &diagnostics);
297-
298-
public:
299-
/// A vector that keeps track of the diagnostics IDs that are available in
300-
/// YAML and not available in `.def` files.
301-
std::vector<std::string> unknownIDs;
302-
303-
/// A diagnostic ID might be present in YAML and not in `.def` file, if that's
304-
/// the case the `id` won't have a `DiagID` value.
305-
/// If the `id` is available in `.def` file this method will return the `id`'s
306-
/// value, otherwise this method won't return a value.
307-
static llvm::Optional<uint32_t> readID(llvm::yaml::IO &io);
308-
};
309-
310247
} // namespace diag
311248
} // namespace swift
312249

lib/ConstExtract/ConstExtract.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "swift/AST/TypeCheckRequests.h"
2222
#include "llvm/ADT/StringRef.h"
2323
#include "llvm/Support/JSON.h"
24+
#include "llvm/Support/YAMLParser.h"
25+
#include "llvm/Support/YAMLTraits.h"
2426

2527
#include <set>
2628
#include <sstream>

lib/Localization/LocalizationFormat.cpp

Lines changed: 2 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
#include "llvm/Support/FileSystem.h"
2525
#include "llvm/Support/MemoryBuffer.h"
2626
#include "llvm/Support/Path.h"
27-
#include "llvm/Support/YAMLParser.h"
28-
#include "llvm/Support/YAMLTraits.h"
2927
#include <cstdint>
28+
#include <map>
3029
#include <string>
3130
#include <system_error>
3231
#include <type_traits>
@@ -47,24 +46,6 @@ static constexpr const char *const diagnosticNameStrings[] = {
4746

4847
} // namespace
4948

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-
6849
namespace swift {
6950
namespace diag {
7051

@@ -149,38 +130,6 @@ SerializedLocalizationProducer::getMessage(swift::DiagID id) const {
149130
return {(const char *)value.getDataPtr(), value.getDataLen()};
150131
}
151132

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-
184133
std::unique_ptr<LocalizationProducer>
185134
LocalizationProducer::producerFor(llvm::StringRef locale, llvm::StringRef path,
186135
bool printDiagnosticNames) {
@@ -189,21 +138,13 @@ LocalizationProducer::producerFor(llvm::StringRef locale, llvm::StringRef path,
189138
llvm::sys::path::replace_extension(filePath, ".db");
190139

191140
// If the serialized diagnostics file not available,
192-
// fallback to the `YAML` file.
141+
// fallback to the `.strings` file.
193142
if (llvm::sys::fs::exists(filePath)) {
194143
if (auto file = llvm::MemoryBuffer::getFile(filePath)) {
195144
return std::make_unique<diag::SerializedLocalizationProducer>(
196145
std::move(file.get()), printDiagnosticNames);
197146
}
198147
} 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-
207148
llvm::sys::path::replace_extension(filePath, ".strings");
208149
if (llvm::sys::fs::exists(filePath)) {
209150
return std::make_unique<diag::StringsLocalizationProducer>(
@@ -214,86 +155,6 @@ LocalizationProducer::producerFor(llvm::StringRef locale, llvm::StringRef path,
214155
return std::unique_ptr<LocalizationProducer>();
215156
}
216157

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-
297158
void DefToStringsConverter::convert(llvm::raw_ostream &out) {
298159
// "<id>" = "<msg>";
299160
for (auto i : swift::indices(IDs)) {

tools/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ add_swift_tool_subdirectory(sil-opt)
2222
add_swift_tool_subdirectory(swift-dependency-tool)
2323
add_swift_tool_subdirectory(swift-demangle)
2424
add_swift_tool_subdirectory(swift-demangle-yamldump)
25-
add_swift_tool_subdirectory(swift-def-to-yaml-converter)
2625
add_swift_tool_subdirectory(swift-def-to-strings-converter)
2726
add_swift_tool_subdirectory(swift-serialize-diagnostics)
2827
add_swift_tool_subdirectory(sil-func-extractor)

tools/swift-def-to-yaml-converter/CMakeLists.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)