mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Localization] Make localization producer handle printing diagnostics IDs
This commit is contained in:
@@ -804,15 +804,15 @@ namespace swift {
|
||||
if (llvm::sys::fs::exists(filePath)) {
|
||||
if (auto file = llvm::MemoryBuffer::getFile(filePath)) {
|
||||
localization = std::make_unique<diag::SerializedLocalizationProducer>(
|
||||
std::move(file.get()));
|
||||
std::move(file.get()), getPrintDiagnosticNames());
|
||||
}
|
||||
} else {
|
||||
llvm::sys::path::replace_extension(filePath, ".yaml");
|
||||
// In case of missing localization files, we should fallback to messages
|
||||
// from `.def` files.
|
||||
if (llvm::sys::fs::exists(filePath)) {
|
||||
localization =
|
||||
std::make_unique<diag::YAMLLocalizationProducer>(filePath.str());
|
||||
localization = std::make_unique<diag::YAMLLocalizationProducer>(
|
||||
filePath.str(), getPrintDiagnosticNames());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,14 +154,16 @@ public:
|
||||
};
|
||||
|
||||
class LocalizationProducer {
|
||||
bool printDiagnosticName;
|
||||
|
||||
public:
|
||||
LocalizationProducer(bool printDiagnosticName = false)
|
||||
: printDiagnosticName(printDiagnosticName) {}
|
||||
|
||||
/// If the message isn't available/localized in current context
|
||||
/// return the fallback default message.
|
||||
virtual llvm::StringRef getMessageOr(swift::DiagID id,
|
||||
llvm::StringRef defaultMessage) const {
|
||||
auto message = getMessage(id);
|
||||
return message.empty() ? defaultMessage : message;
|
||||
}
|
||||
llvm::StringRef defaultMessage) const;
|
||||
|
||||
virtual ~LocalizationProducer() {}
|
||||
|
||||
@@ -177,7 +179,8 @@ class YAMLLocalizationProducer final : public LocalizationProducer {
|
||||
public:
|
||||
/// The diagnostics IDs that are no longer available in `.def`
|
||||
std::vector<std::string> unknownIDs;
|
||||
explicit YAMLLocalizationProducer(llvm::StringRef filePath);
|
||||
explicit YAMLLocalizationProducer(llvm::StringRef filePath,
|
||||
bool printDiagnosticName = false);
|
||||
|
||||
/// Iterate over all of the available (non-empty) translations
|
||||
/// maintained by this producer, callback gets each translation
|
||||
@@ -198,7 +201,8 @@ class SerializedLocalizationProducer final : public LocalizationProducer {
|
||||
|
||||
public:
|
||||
explicit SerializedLocalizationProducer(
|
||||
std::unique_ptr<llvm::MemoryBuffer> buffer);
|
||||
std::unique_ptr<llvm::MemoryBuffer> buffer,
|
||||
bool printDiagnosticName = false);
|
||||
|
||||
protected:
|
||||
llvm::StringRef getMessage(swift::DiagID id) const override;
|
||||
|
||||
@@ -1114,25 +1114,14 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
|
||||
llvm::StringRef
|
||||
DiagnosticEngine::diagnosticStringFor(const DiagID id,
|
||||
bool printDiagnosticName) {
|
||||
auto defaultMessage = diagnosticStrings[(unsigned)id];
|
||||
auto diagnosticName = diagnosticNameStrings[(unsigned)id];
|
||||
auto defaultMessage = printDiagnosticName
|
||||
? debugDiagnosticStrings[(unsigned)id]
|
||||
: diagnosticStrings[(unsigned)id];
|
||||
if (localization) {
|
||||
auto localizedMessage =
|
||||
localization.get()->getMessageOr(id, defaultMessage);
|
||||
if (printDiagnosticName) {
|
||||
std::string debugDiagnosticString = localizedMessage.str().c_str();
|
||||
debugDiagnosticString += diagnosticName;
|
||||
const std::string &localizedDebugDiagnosticString = debugDiagnosticString;
|
||||
return localizedDebugDiagnosticString;
|
||||
}
|
||||
return localizedMessage;
|
||||
}
|
||||
|
||||
if (printDiagnosticName) {
|
||||
const std::string &debugDiagnosticString =
|
||||
std::string(defaultMessage) + diagnosticName;
|
||||
return debugDiagnosticString;
|
||||
}
|
||||
return defaultMessage;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,15 @@ enum LocalDiagID : uint32_t {
|
||||
NumDiags
|
||||
};
|
||||
|
||||
static constexpr const char *const diagnosticNameStrings[] = {
|
||||
#define ERROR(ID, Options, Text, Signature) " [" #ID "]",
|
||||
#define WARNING(ID, Options, Text, Signature) " [" #ID "]",
|
||||
#define NOTE(ID, Options, Text, Signature) " [" #ID "]",
|
||||
#define REMARK(ID, Options, Text, Signature) " [" #ID "]",
|
||||
#include "swift/AST/DiagnosticsAll.def"
|
||||
"<not a diagnostic>",
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace llvm {
|
||||
@@ -85,9 +94,22 @@ bool SerializedLocalizationWriter::emit(llvm::StringRef filePath) {
|
||||
return OS.has_error();
|
||||
}
|
||||
|
||||
llvm::StringRef
|
||||
LocalizationProducer::getMessageOr(swift::DiagID id,
|
||||
llvm::StringRef defaultMessage) const {
|
||||
auto localizedMessage = getMessage(id);
|
||||
if (localizedMessage.empty())
|
||||
return defaultMessage;
|
||||
llvm::StringRef diagnosticName(diagnosticNameStrings[(unsigned)id]);
|
||||
const std::string &localizedDebugDiagnosticMessage =
|
||||
localizedMessage.str() + diagnosticName.str();
|
||||
return printDiagnosticName ? localizedDebugDiagnosticMessage
|
||||
: localizedMessage;
|
||||
}
|
||||
|
||||
SerializedLocalizationProducer::SerializedLocalizationProducer(
|
||||
std::unique_ptr<llvm::MemoryBuffer> buffer)
|
||||
: Buffer(std::move(buffer)) {
|
||||
std::unique_ptr<llvm::MemoryBuffer> buffer, bool printDiagnosticName)
|
||||
: LocalizationProducer(printDiagnosticName), Buffer(std::move(buffer)) {
|
||||
auto base =
|
||||
reinterpret_cast<const unsigned char *>(Buffer.get()->getBufferStart());
|
||||
auto tableOffset = endian::read<offset_type>(base, little);
|
||||
@@ -103,7 +125,9 @@ SerializedLocalizationProducer::getMessage(swift::DiagID id) const {
|
||||
return {(const char *)value.getDataPtr(), value.getDataLen()};
|
||||
}
|
||||
|
||||
YAMLLocalizationProducer::YAMLLocalizationProducer(llvm::StringRef filePath) {
|
||||
YAMLLocalizationProducer::YAMLLocalizationProducer(llvm::StringRef filePath,
|
||||
bool printDiagnosticName)
|
||||
: LocalizationProducer(printDiagnosticName) {
|
||||
auto FileBufOrErr = llvm::MemoryBuffer::getFileOrSTDIN(filePath);
|
||||
llvm::MemoryBuffer *document = FileBufOrErr->get();
|
||||
diag::LocalizationInput yin(document->getBuffer());
|
||||
|
||||
@@ -9,4 +9,4 @@ var self1 = self1
|
||||
struct Broken {
|
||||
var b : Bool = True // expected-error{{cannot find 'True' in scope}}
|
||||
}
|
||||
var v1 : Int[1 // expected-error {{expected ']' in array type}} expected-note {{to match this opening '['}}
|
||||
// CHECK_NAMES: error: cannot find 'True' in scope [cannot_find_in_scope]{{$}}
|
||||
|
||||
Reference in New Issue
Block a user