Skip to content

Commit 3a4d156

Browse files
committed
[Localization] Switch serialization format to use DiagID as a key
1 parent 67dbef5 commit 3a4d156

File tree

3 files changed

+33
-43
lines changed

3 files changed

+33
-43
lines changed

include/swift/AST/LocalizationFormat.h

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#ifndef SWIFT_LOCALIZATIONFORMAT_H
1818
#define SWIFT_LOCALIZATIONFORMAT_H
1919

20+
#include "llvm/ADT/Hashing.h"
2021
#include "llvm/ADT/STLExtras.h"
2122
#include "llvm/ADT/StringRef.h"
2223
#include "llvm/Bitstream/BitstreamReader.h"
23-
#include "llvm/Support/DJB.h"
2424
#include "llvm/Support/EndianStream.h"
2525
#include "llvm/Support/MemoryBuffer.h"
2626
#include "llvm/Support/OnDiskHashTable.h"
@@ -42,27 +42,27 @@ using namespace llvm::support;
4242

4343
class LocalizationWriterInfo {
4444
public:
45-
using key_type = std::string;
46-
using key_type_ref = key_type;
45+
using key_type = uint32_t;
46+
using key_type_ref = const uint32_t &;
4747
using data_type = std::string;
48-
using data_type_ref = data_type;
48+
using data_type_ref = llvm::StringRef;
4949
using hash_value_type = uint32_t;
5050
using offset_type = uint32_t;
5151

52-
hash_value_type ComputeHash(key_type_ref key) { return llvm::djbHash(key); }
52+
hash_value_type ComputeHash(key_type_ref key) { return llvm::hash_code(key); }
5353

5454
std::pair<offset_type, offset_type> EmitKeyDataLength(llvm::raw_ostream &out,
5555
key_type_ref key,
5656
data_type_ref data) {
57-
offset_type keyLength = static_cast<offset_type>(key.size());
5857
offset_type dataLength = static_cast<offset_type>(data.size());
59-
endian::write<offset_type>(out, keyLength, little);
6058
endian::write<offset_type>(out, dataLength, little);
61-
return {keyLength, dataLength};
59+
// No need to write the key length; it's constant.
60+
return {sizeof(key_type), dataLength};
6261
}
6362

6463
void EmitKey(llvm::raw_ostream &out, key_type_ref key, unsigned len) {
65-
out << key;
64+
assert(len == sizeof(key_type));
65+
endian::write<key_type>(out, key, little);
6666
}
6767

6868
void EmitData(llvm::raw_ostream &out, key_type_ref key, data_type_ref data,
@@ -73,38 +73,40 @@ class LocalizationWriterInfo {
7373

7474
class LocalizationReaderInfo {
7575
public:
76-
using internal_key_type = llvm::StringRef;
77-
using external_key_type = internal_key_type;
76+
using internal_key_type = uint32_t;
77+
using external_key_type = swift::DiagID;
7878
using data_type = llvm::StringRef;
7979
using hash_value_type = uint32_t;
8080
using offset_type = uint32_t;
8181

82-
internal_key_type GetInternalKey(external_key_type key) { return key; }
82+
internal_key_type GetInternalKey(external_key_type key) {
83+
return static_cast<internal_key_type>(key);
84+
}
8385

84-
external_key_type GetExternalKey(internal_key_type key) { return key; }
86+
external_key_type GetExternalKey(internal_key_type key) {
87+
return static_cast<external_key_type>(key);
88+
}
8589

8690
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
8791
return lhs == rhs;
8892
}
8993

9094
hash_value_type ComputeHash(internal_key_type key) {
91-
return llvm::djbHash(key);
95+
return llvm::hash_code(key);
9296
}
9397

9498
static std::pair<offset_type, offset_type>
9599
ReadKeyDataLength(const unsigned char *&data) {
96-
offset_type keyLength =
97-
endian::readNext<offset_type, little, unaligned>(data);
98100
offset_type dataLength =
99101
endian::readNext<offset_type, little, unaligned>(data);
100-
return {keyLength, dataLength};
102+
return {sizeof(uint32_t), dataLength};
101103
}
102104

103105
internal_key_type ReadKey(const unsigned char *data, offset_type length) {
104-
return internal_key_type((const char *)data, length);
106+
return endian::readNext<internal_key_type, little, unaligned>(data);
105107
}
106108

107-
data_type ReadData(llvm::StringRef Key, const unsigned char *data,
109+
data_type ReadData(internal_key_type Key, const unsigned char *data,
108110
offset_type length) {
109111
return data_type((const char *)data, length);
110112
}
@@ -119,10 +121,10 @@ class SerializedLocalizationWriter {
119121
/// file.
120122
///
121123
/// \param id The identifier associated with the given diagnostic message e.g.
122-
/// 'cannot_convert_argument'.
123-
/// \param translation The localized diagnostic
124-
/// message for the given identifier.
125-
void insert(llvm::StringRef id, llvm::StringRef translation);
124+
/// 'cannot_convert_argument'.
125+
/// \param translation The localized diagnostic message for the given
126+
/// identifier.
127+
void insert(swift::DiagID id, llvm::StringRef translation);
126128

127129
/// Write out previously inserted diagnostic translations into the given
128130
/// location.
@@ -158,7 +160,7 @@ class YAMLLocalizationProducer final : public LocalizationProducer {
158160
/// maintained by this producer, callback gets each translation
159161
/// with its unique identifier.
160162
void forEachAvailable(
161-
llvm::function_ref<void(uint32_t, llvm::StringRef)> callback) const;
163+
llvm::function_ref<void(swift::DiagID, llvm::StringRef)> callback) const;
162164
};
163165

164166
class SerializedLocalizationProducer final : public LocalizationProducer {

lib/AST/LocalizationFormat.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ enum LocalDiagID : uint32_t {
3434
NumDiags
3535
};
3636

37-
static constexpr const char *const diagnosticID[] = {
38-
#define DIAG(KIND, ID, Options, Text, Signature) #ID,
39-
#include "swift/AST/DiagnosticsAll.def"
40-
};
41-
4237
struct DiagnosticNode {
4338
uint32_t id;
4439
std::string msg;
@@ -75,9 +70,9 @@ template <> struct MappingTraits<DiagnosticNode> {
7570
namespace swift {
7671
namespace diag {
7772

78-
void SerializedLocalizationWriter::insert(llvm::StringRef id,
73+
void SerializedLocalizationWriter::insert(swift::DiagID id,
7974
llvm::StringRef translation) {
80-
generator.insert(id, translation);
75+
generator.insert(static_cast<uint32_t>(id), translation);
8176
}
8277

8378
bool SerializedLocalizationWriter::emit(llvm::StringRef filePath) {
@@ -112,7 +107,7 @@ SerializedLocalizationProducer::SerializedLocalizationProducer(
112107

113108
llvm::StringRef SerializedLocalizationProducer::getMessageOr(
114109
swift::DiagID id, llvm::StringRef defaultMessage) const {
115-
auto value = SerializedTable.get()->find(diagnosticID[(unsigned)id]);
110+
auto value = SerializedTable.get()->find(id);
116111
llvm::StringRef diagnosticMessage((const char *)value.getDataPtr(),
117112
value.getDataLen());
118113
if (diagnosticMessage.empty())
@@ -140,11 +135,11 @@ YAMLLocalizationProducer::getMessageOr(swift::DiagID id,
140135
}
141136

142137
void YAMLLocalizationProducer::forEachAvailable(
143-
llvm::function_ref<void(uint32_t, llvm::StringRef)> callback) const {
138+
llvm::function_ref<void(swift::DiagID, llvm::StringRef)> callback) const {
144139
for (uint32_t i = 0, n = diagnostics.size(); i != n; ++i) {
145140
auto translation = diagnostics[i];
146141
if (!translation.empty())
147-
callback(i, translation);
142+
callback(static_cast<swift::DiagID>(i), translation);
148143
}
149144
}
150145

tools/swift-serialize-diagnostics/swift-serialize-diagnostics.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@
3535
using namespace swift;
3636
using namespace swift::diag;
3737

38-
namespace {
39-
static constexpr const char *const diagnosticID[] = {
40-
#define DIAG(KIND, ID, Options, Text, Signature) #ID,
41-
#include "swift/AST/DiagnosticsAll.def"
42-
};
43-
} // namespace
44-
4538
namespace options {
4639

4740
static llvm::cl::OptionCategory Category("swift-serialize-diagnostics Options");
@@ -80,8 +73,8 @@ int main(int argc, char *argv[]) {
8073

8174
SerializedLocalizationWriter Serializer;
8275
yaml.forEachAvailable(
83-
[&Serializer](uint32_t id, llvm::StringRef translation) {
84-
Serializer.insert(diagnosticID[id], translation);
76+
[&Serializer](swift::DiagID id, llvm::StringRef translation) {
77+
Serializer.insert(id, translation);
8578
});
8679

8780
if (Serializer.emit(SerializedFilePath.str())) {

0 commit comments

Comments
 (0)