Skip to content

Revert "[GSoC] Refactor DiagnosticEngine to use YAML files" #32460

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 1 commit into from
Jun 19, 2020
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
3 changes: 0 additions & 3 deletions include/swift/AST/CMakeLists.txt

This file was deleted.

38 changes: 3 additions & 35 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ namespace swift {
DiagID ID;
};

struct DiagnosticNode {
DiagID id;
std::string msg;
};

namespace detail {
/// Describes how to pass a diagnostic argument of the given type.
///
Expand Down Expand Up @@ -634,26 +629,7 @@ namespace swift {
DiagnosticState(DiagnosticState &&) = default;
DiagnosticState &operator=(DiagnosticState &&) = default;
};

class LocalizationProducer {
public:
/// If the message isn't available/localized in the current `yaml` file,
/// return the fallback default message.
virtual std::string getMessageOr(DiagID id,
std::string defaultMessage) const {
return defaultMessage;
}

virtual ~LocalizationProducer() {}
};

class YAMLLocalizationProducer final : public LocalizationProducer {
public:
std::vector<std::string> diagnostics;
explicit YAMLLocalizationProducer(std::string locale, std::string path);
std::string getMessageOr(DiagID id,
std::string defaultMessage) const override;
};

/// Class responsible for formatting diagnostics and presenting them
/// to the user.
class DiagnosticEngine {
Expand Down Expand Up @@ -687,10 +663,6 @@ namespace swift {
/// but rather stored until all transactions complete.
llvm::StringSet<llvm::BumpPtrAllocator &> TransactionStrings;

/// Diagnostic producer to handle the logic behind retriving a localized
/// diagnostic message.
std::unique_ptr<LocalizationProducer> localization;

/// The number of open diagnostic transactions. Diagnostics are only
/// emitted once all transactions have closed.
unsigned TransactionCount = 0;
Expand Down Expand Up @@ -762,11 +734,6 @@ namespace swift {
return diagnosticDocumentationPath;
}

void setLocalization(std::string locale, std::string path) {
if (!locale.empty() && !path.empty())
localization = std::make_unique<YAMLLocalizationProducer>(locale, path);
}

void ignoreDiagnostic(DiagID id) {
state.setDiagnosticBehavior(id, DiagnosticState::Behavior::Ignore);
}
Expand Down Expand Up @@ -988,7 +955,8 @@ namespace swift {
void emitTentativeDiagnostics();

public:
const char *diagnosticStringFor(const DiagID id, bool printDiagnosticName);
static const char *diagnosticStringFor(const DiagID id,
bool printDiagnosticName);

/// If there is no clear .dia file for a diagnostic, put it in the one
/// corresponding to the SourceLoc given here.
Expand Down
42 changes: 0 additions & 42 deletions include/swift/AST/DiagnosticMessageFormat.h

This file was deleted.

19 changes: 0 additions & 19 deletions include/swift/AST/Diagnostics/fr.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions include/swift/AST/diagnostics/en.yaml

This file was deleted.

80 changes: 1 addition & 79 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/AST/DiagnosticMessageFormat.h"
#include "swift/AST/Module.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/PrintOptions.h"
Expand All @@ -33,8 +32,6 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
Expand Down Expand Up @@ -143,52 +140,6 @@ struct EducationalNotes {
static constexpr EducationalNotes<LocalDiagID::NumDiags> _EducationalNotes = EducationalNotes<LocalDiagID::NumDiags>();
static constexpr auto educationalNotes = _EducationalNotes.value;

class LocalizationInput : public llvm::yaml::Input {
using Input::Input;

/// Read diagnostics in the YAML file iteratively
template <typename T, typename Context>
static typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
void>::type
readYAML(IO &io, T &Seq, bool, Context &Ctx) {
unsigned count = io.beginSequence();

// Resize Diags from YAML file to be the same size
// as diagnosticStrings from def files.
Seq.resize(LocalDiagID::NumDiags);
for (unsigned i = 0; i < count; ++i) {
void *SaveInfo;
if (io.preflightElement(i, SaveInfo)) {
DiagnosticNode current;
yamlize(io, current, true, Ctx);
io.postflightElement(SaveInfo);
// YAML file isn't guaranteed to have diagnostics in order of their
// declaration in `.def` files, to accommodate that we need to leave
// holes in diagnostic array for diagnostics which haven't yet been
// localized and for the ones that have `DiagnosticNode::id`
// indicates their position.
Seq[static_cast<unsigned>(current.id)] = std::move(current.msg);
}
}
io.endSequence();
}

template <typename T>
inline friend
typename std::enable_if<llvm::yaml::has_SequenceTraits<T>::value,
LocalizationInput &>::type
operator>>(LocalizationInput &yin, T &diagnostics) {
llvm::yaml::EmptyContext Ctx;
if (yin.setCurrentDocument()) {
// If YAML file's format doesn't match the current format in
// DiagnosticMessageFormat, will throw an error.
readYAML(yin, diagnostics, true, Ctx);
}

return yin;
}
};

DiagnosticState::DiagnosticState() {
// Initialize our per-diagnostic state to default
perDiagnosticBehavior.resize(LocalDiagID::NumDiags, Behavior::Unspecified);
Expand Down Expand Up @@ -360,28 +311,6 @@ void Diagnostic::addChildNote(Diagnostic &&D) {
ChildNotes.push_back(std::move(D));
}

YAMLLocalizationProducer::YAMLLocalizationProducer(std::string locale,
std::string path) {
llvm::SmallString<128> DiagnosticsFilePath(path);
llvm::sys::path::append(DiagnosticsFilePath, locale);
llvm::sys::path::replace_extension(DiagnosticsFilePath, ".yaml");
auto FileBufOrErr = llvm::MemoryBuffer::getFileOrSTDIN(DiagnosticsFilePath);
if (!FileBufOrErr)
llvm_unreachable("Failed to read yaml file");
llvm::MemoryBuffer *document = FileBufOrErr->get();
LocalizationInput yin(document->getBuffer());
yin >> diagnostics;
}

std::string
YAMLLocalizationProducer::getMessageOr(DiagID id,
std::string defaultMessage) const {
std::string diagnosticMessage = diagnostics[(unsigned)id];
if (diagnosticMessage.empty())
return defaultMessage;
return diagnosticMessage;
}

bool DiagnosticEngine::isDiagnosticPointsToFirstBadToken(DiagID ID) const {
return storedDiagnosticInfos[(unsigned) ID].pointsToFirstBadToken;
}
Expand Down Expand Up @@ -1082,17 +1011,10 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {

const char *DiagnosticEngine::diagnosticStringFor(const DiagID id,
bool printDiagnosticName) {
// TODO: Print diagnostic names from `localization`.
if (printDiagnosticName) {
return debugDiagnosticStrings[(unsigned)id];
}
auto defaultMessage = diagnosticStrings[(unsigned)id];
if (localization) {
std::string localizedMessage =
localization.get()->getMessageOr(id, defaultMessage);
return localizedMessage.c_str();
}
return defaultMessage;
return diagnosticStrings[(unsigned)id];
}

const char *InFlightDiagnostic::fixItStringFor(const FixItID id) {
Expand Down