Skip to content

Commit fd8b24a

Browse files
Use uint32_t in DiagnosticNodes
1 parent c685d38 commit fd8b24a

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

include/swift/AST/LocalizationFormat.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,29 @@
1818
#ifndef SWIFT_LOCALIZATIONFORMAT_H
1919
#define SWIFT_LOCALIZATIONFORMAT_H
2020

21+
#include "llvm/ADT/StringRef.h"
2122
#include "llvm/Support/YAMLParser.h"
2223
#include "llvm/Support/YAMLTraits.h"
24+
#include <string>
25+
#include <type_traits>
2326

2427
namespace {
25-
enum LocalDiagID : uint32_t;
28+
enum class DiagID : uint32_t;
2629
}
2730

2831
namespace swift {
2932
namespace diag {
3033

3134
struct DiagnosticNode {
32-
LocalDiagID id;
35+
uint32_t id;
3336
std::string msg;
3437
};
3538

3639
class LocalizationProducer {
3740
public:
3841
/// If the message isn't available/localized in the current `yaml` file,
3942
/// return the fallback default message.
40-
virtual llvm::StringRef getMessageOr(LocalDiagID id,
43+
virtual llvm::StringRef getMessageOr(DiagID id,
4144
llvm::StringRef defaultMessage) const {
4245
return defaultMessage;
4346
}
@@ -49,7 +52,7 @@ class YAMLLocalizationProducer final : public LocalizationProducer {
4952
public:
5053
std::vector<std::string> diagnostics;
5154
explicit YAMLLocalizationProducer(std::string locale, std::string path);
52-
llvm::StringRef getMessageOr(LocalDiagID id,
55+
llvm::StringRef getMessageOr(DiagID id,
5356
llvm::StringRef defaultMessage) const override;
5457
};
5558

lib/AST/DiagnosticEngine.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,11 +1018,11 @@ const char *DiagnosticEngine::diagnosticStringFor(const DiagID id,
10181018
}
10191019
auto defaultMessage = diagnosticStrings[(unsigned)id];
10201020
if (localization) {
1021-
const std::string &localizedMessage =
1022-
localization.get()->getMessageOr((LocalDiagID)id, defaultMessage);
1023-
return localizedMessage.c_str();
1021+
auto localizedMessage =
1022+
localization.get()->getMessageOr(id, defaultMessage);
1023+
return localizedMessage.data();
10241024
}
1025-
return diagnosticStrings[(unsigned)id];
1025+
return defaultMessage;
10261026
}
10271027

10281028
const char *InFlightDiagnostic::fixItStringFor(const FixItID id) {

lib/AST/LocalizationFormat.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "swift/AST/LocalizationFormat.h"
19+
#include "llvm/ADT/SmallString.h"
20+
#include "llvm/ADT/StringRef.h"
1921
#include "llvm/Support/CommandLine.h"
22+
#include "llvm/Support/MemoryBuffer.h"
2023
#include "llvm/Support/YAMLParser.h"
2124
#include "llvm/Support/YAMLTraits.h"
25+
#include <string>
26+
#include <type_traits>
2227

2328
namespace {
2429
enum LocalDiagID : uint32_t {
@@ -41,8 +46,16 @@ template <> struct ScalarEnumerationTraits<LocalDiagID> {
4146

4247
template <> struct MappingTraits<swift::diag::DiagnosticNode> {
4348
static void mapping(IO &io, swift::diag::DiagnosticNode &node) {
44-
io.mapRequired("id", node.id);
49+
// Cast `uint32_t` to `LocalDiagID` to use `diagID` at
50+
// ScalarEnumerationTraits, because EnumerationTraits has to have an `id` of
51+
// type `LocalDiagID`
52+
auto diagID = static_cast<LocalDiagID>(node.id);
53+
io.mapRequired("id", diagID);
4554
io.mapRequired("msg", node.msg);
55+
// We will need to cast `diagID` back again to unsigned int, and assign it
56+
// to `node.id` because we will need the `uint32_t` value as it'll be used
57+
// to retrieve diagnostic nodes later.
58+
node.id = (unsigned)diagID;
4659
}
4760
};
4861

0 commit comments

Comments
 (0)