Skip to content

Commit bca9004

Browse files
authored
Merge pull request #60707 from xedin/switch-to-string-format-for-locale
[Localization] Switch diagnostics/localization to `.strings` format
2 parents ae9bc59 + 10b609c commit bca9004

File tree

21 files changed

+287
-293
lines changed

21 files changed

+287
-293
lines changed

Diff for: include/swift/Localization/LocalizationFormat.h

+17-41
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,14 +48,14 @@ enum LocalizationProducerState : uint8_t {
5048
FailedInitialization
5149
};
5250

53-
class DefToYAMLConverter {
51+
class DefToStringsConverter {
5452
llvm::ArrayRef<const char *> IDs;
5553
llvm::ArrayRef<const char *> Messages;
5654

5755
public:
58-
DefToYAMLConverter(llvm::ArrayRef<const char *> ids,
59-
llvm::ArrayRef<const char *> messages)
60-
: IDs(ids), Messages(messages) {
56+
DefToStringsConverter(llvm::ArrayRef<const char *> ids,
57+
llvm::ArrayRef<const char *> messages)
58+
: IDs(ids), Messages(messages) {
6159
assert(IDs.size() == Messages.size());
6260
}
6361

@@ -181,9 +179,9 @@ class LocalizationProducer {
181179
llvm::StringRef defaultMessage);
182180

183181
/// \returns a `SerializedLocalizationProducer` pointer if the serialized
184-
/// diagnostics file available, otherwise returns a `YAMLLocalizationProducer`
185-
/// if the `YAML` file is available. If both files aren't available returns a
186-
/// `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`.
187185
static std::unique_ptr<LocalizationProducer>
188186
producerFor(llvm::StringRef locale, llvm::StringRef path,
189187
bool printDiagnosticNames);
@@ -204,15 +202,15 @@ class LocalizationProducer {
204202
virtual llvm::StringRef getMessage(swift::DiagID id) const = 0;
205203
};
206204

207-
class YAMLLocalizationProducer final : public LocalizationProducer {
208-
std::vector<std::string> diagnostics;
205+
class StringsLocalizationProducer final : public LocalizationProducer {
209206
std::string filePath;
210207

208+
std::vector<std::string> diagnostics;
209+
211210
public:
212-
/// The diagnostics IDs that are no longer available in `.def`
213-
std::vector<std::string> unknownIDs;
214-
explicit YAMLLocalizationProducer(llvm::StringRef filePath,
215-
bool printDiagnosticNames = false);
211+
explicit StringsLocalizationProducer(llvm::StringRef filePath,
212+
bool printDiagnosticNames = false)
213+
: LocalizationProducer(printDiagnosticNames), filePath(filePath) {}
216214

217215
/// Iterate over all of the available (non-empty) translations
218216
/// maintained by this producer, callback gets each translation
@@ -223,6 +221,10 @@ class YAMLLocalizationProducer final : public LocalizationProducer {
223221
protected:
224222
bool initializeImpl() override;
225223
llvm::StringRef getMessage(swift::DiagID id) const override;
224+
225+
private:
226+
static void readStringsFile(llvm::MemoryBuffer *in,
227+
std::vector<std::string> &diagnostics);
226228
};
227229

228230
class SerializedLocalizationProducer final : public LocalizationProducer {
@@ -242,32 +244,6 @@ class SerializedLocalizationProducer final : public LocalizationProducer {
242244
llvm::StringRef getMessage(swift::DiagID id) const override;
243245
};
244246

245-
class LocalizationInput : public llvm::yaml::Input {
246-
using Input::Input;
247-
248-
/// Read diagnostics in the YAML file iteratively
249-
template <typename T, typename Context>
250-
friend typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
251-
void>::type
252-
readYAML(llvm::yaml::IO &io, T &Seq, T &unknownIDs, bool, Context &Ctx);
253-
254-
template <typename T>
255-
friend typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
256-
LocalizationInput &>::type
257-
operator>>(LocalizationInput &yin, T &diagnostics);
258-
259-
public:
260-
/// A vector that keeps track of the diagnostics IDs that are available in
261-
/// YAML and not available in `.def` files.
262-
std::vector<std::string> unknownIDs;
263-
264-
/// A diagnostic ID might be present in YAML and not in `.def` file, if that's
265-
/// the case the `id` won't have a `DiagID` value.
266-
/// If the `id` is available in `.def` file this method will return the `id`'s
267-
/// value, otherwise this method won't return a value.
268-
static llvm::Optional<uint32_t> readID(llvm::yaml::IO &io);
269-
};
270-
271247
} // namespace diag
272248
} // namespace swift
273249

Diff for: lib/ConstExtract/ConstExtract.cpp

+2
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>

Diff for: lib/Frontend/CompilerInvocation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
14521452
// for the specified locale code.
14531453
llvm::SmallString<128> localizationPath(A->getValue());
14541454
llvm::sys::path::append(localizationPath, Opts.LocalizationCode);
1455-
llvm::sys::path::replace_extension(localizationPath, ".yaml");
1455+
llvm::sys::path::replace_extension(localizationPath, ".strings");
14561456
if (!llvm::sys::fs::exists(localizationPath)) {
14571457
Diags.diagnose(SourceLoc(), diag::warning_cannot_find_locale_file,
14581458
Opts.LocalizationCode, localizationPath);

0 commit comments

Comments
 (0)