Skip to content

[Diag] Print the diagnostic ID name from localization #34319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -808,15 +808,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());
}
}
}
Expand Down Expand Up @@ -1043,7 +1043,7 @@ namespace swift {

public:
llvm::StringRef diagnosticStringFor(const DiagID id,
bool printDiagnosticName);
bool printDiagnosticNames);

/// If there is no clear .dia file for a diagnostic, put it in the one
/// corresponding to the SourceLoc given here.
Expand Down
24 changes: 18 additions & 6 deletions include/swift/Localization/LocalizationFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/OnDiskHashTable.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -154,14 +155,23 @@ class SerializedLocalizationWriter {
};

class LocalizationProducer {
/// This allocator will retain localized diagnostic strings containing the
/// diagnostic's message and identifier as `message [id]` for the duration of
/// compiler invocation. This will be used when the frontend flag
/// `-debug-diagnostic-names` is used.
llvm::BumpPtrAllocator localizationAllocator;
llvm::StringSaver localizationSaver;
bool printDiagnosticNames;

public:
LocalizationProducer(bool printDiagnosticNames = false)
: localizationSaver(localizationAllocator),
printDiagnosticNames(printDiagnosticNames) {}

/// 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);

virtual ~LocalizationProducer() {}

Expand All @@ -177,7 +187,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 printDiagnosticNames = false);

/// Iterate over all of the available (non-empty) translations
/// maintained by this producer, callback gets each translation
Expand All @@ -198,7 +209,8 @@ class SerializedLocalizationProducer final : public LocalizationProducer {

public:
explicit SerializedLocalizationProducer(
std::unique_ptr<llvm::MemoryBuffer> buffer);
std::unique_ptr<llvm::MemoryBuffer> buffer,
bool printDiagnosticNames = false);

protected:
llvm::StringRef getMessage(swift::DiagID id) const override;
Expand Down
10 changes: 4 additions & 6 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,12 +1113,10 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {

llvm::StringRef
DiagnosticEngine::diagnosticStringFor(const DiagID id,
bool printDiagnosticName) {
// TODO: Print diagnostic names from `localization`.
if (printDiagnosticName) {
return debugDiagnosticStrings[(unsigned)id];
}
auto defaultMessage = diagnosticStrings[(unsigned)id];
bool printDiagnosticNames) {
auto defaultMessage = printDiagnosticNames
? debugDiagnosticStrings[(unsigned)id]
: diagnosticStrings[(unsigned)id];
if (localization) {
auto localizedMessage =
localization.get()->getMessageOr(id, defaultMessage);
Expand Down
29 changes: 26 additions & 3 deletions lib/Localization/LocalizationFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ enum LocalDiagID : uint32_t {
NumDiags
};

static constexpr const char *const diagnosticNameStrings[] = {
#define DIAG(KIND, ID, Options, Text, Signature) " [" #ID "]",
#include "swift/AST/DiagnosticsAll.def"
"<not a diagnostic>",
};

} // namespace

namespace llvm {
Expand Down Expand Up @@ -85,9 +91,24 @@ bool SerializedLocalizationWriter::emit(llvm::StringRef filePath) {
return OS.has_error();
}

llvm::StringRef
LocalizationProducer::getMessageOr(swift::DiagID id,
llvm::StringRef defaultMessage) {
auto localizedMessage = getMessage(id);
if (localizedMessage.empty())
return defaultMessage;
if (printDiagnosticNames) {
llvm::StringRef diagnosticName(diagnosticNameStrings[(unsigned)id]);
auto localizedDebugDiagnosticMessage =
localizationSaver.save(localizedMessage.str() + diagnosticName.str());
return localizedDebugDiagnosticMessage;
}
return localizedMessage;
}

SerializedLocalizationProducer::SerializedLocalizationProducer(
std::unique_ptr<llvm::MemoryBuffer> buffer)
: Buffer(std::move(buffer)) {
std::unique_ptr<llvm::MemoryBuffer> buffer, bool printDiagnosticNames)
: LocalizationProducer(printDiagnosticNames), Buffer(std::move(buffer)) {
auto base =
reinterpret_cast<const unsigned char *>(Buffer.get()->getBufferStart());
auto tableOffset = endian::read<offset_type>(base, little);
Expand All @@ -103,7 +124,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 printDiagnosticNames)
: LocalizationProducer(printDiagnosticNames) {
auto FileBufOrErr = llvm::MemoryBuffer::getFileOrSTDIN(filePath);
llvm::MemoryBuffer *document = FileBufOrErr->get();
diag::LocalizationInput yin(document->getBuffer());
Expand Down
13 changes: 13 additions & 0 deletions test/diagnostics/Localization/en_debug_diagnostic_name.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: not %target-swift-frontend -debug-diagnostic-names -localization-path %S/Inputs -locale en -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK_NAMES

_ = "HI!
// CHECK_NAMES: error: unterminated string literal [lex_unterminated_string]{{$}}

var self1 = self1
// CHECK_NAMES: error: circular reference [circular_reference]{{$}}
// CHECK_NAMES: note: through reference here [circular_reference_through]{{$}}

struct Broken {
var b : Bool = True
}
// CHECK_NAMES: error: cannot find 'True' in scope [cannot_find_in_scope]{{$}}
19 changes: 19 additions & 0 deletions test/diagnostics/Localization/fr_debug_diagnostic_name.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %empty-directory(%t)
// RUN: swift-serialize-diagnostics --input-file-path=%S/Inputs/fr.yaml --output-directory=%t/
// RUN: swift-serialize-diagnostics --input-file-path=%S/Inputs/en.yaml --output-directory=%t/ 2>&1 | %FileCheck %s
// RUN: not %target-swift-frontend -debug-diagnostic-names -localization-path %S/Inputs -locale fr -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK_NAMES

// CHECK: These diagnostic IDs are no longer availiable: 'not_available_in_def, not_available_in_def_2, not_available_in_def_3, not_available_in_def_4, not_available_in_def_5'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove these diagnostics from yaml in a separate PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! But can you explain why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean shouldn't we be testing/checking on yaml.unknownIDs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since .def file is still a source of truth and if it doesn't have it the that diagnostic is no longer viable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay... makes sense thank you!

_ = "HI!
// CHECK_NAMES: error: chaîne non terminée littérale [lex_unterminated_string]{{$}}

// FIXME: This used to produce a localized diagnostic.

var self1 = self1
// CHECK_NAMES: error: circular reference [circular_reference]{{$}}
// CHECK_NAMES: note: through reference here [circular_reference_through]{{$}}

struct Broken {
var b : Bool = True
}
// CHECK_NAMES: error: impossible de trouver 'True' portée [cannot_find_in_scope]{{$}}