/// currently in-flight diagnostic.
class Diagnostic {
const DiagnosticsEngine *DiagObj;
- StringRef StoredDiagMessage;
+ std::optional<StringRef> StoredDiagMessage;
public:
explicit Diagnostic(const DiagnosticsEngine *DO) : DiagObj(DO) {}
/// array.
void Diagnostic::
FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
- if (!StoredDiagMessage.empty()) {
- OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
+ if (StoredDiagMessage.has_value()) {
+ OutStr.append(StoredDiagMessage->begin(), StoredDiagMessage->end());
return;
}
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticError.h"
#include "clang/Basic/DiagnosticIDs.h"
+#include "clang/Basic/DiagnosticLex.h"
#include "gtest/gtest.h"
#include <optional>
EXPECT_EQ(*Value, std::make_pair(20, 1));
EXPECT_EQ(Value->first, 20);
}
+
+TEST(DiagnosticTest, storedDiagEmptyWarning) {
+ DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions);
+
+ class CaptureDiagnosticConsumer : public DiagnosticConsumer {
+ public:
+ SmallVector<StoredDiagnostic> StoredDiags;
+
+ void HandleDiagnostic(DiagnosticsEngine::Level level,
+ const Diagnostic &Info) override {
+ StoredDiags.push_back(StoredDiagnostic(level, Info));
+ }
+ };
+
+ CaptureDiagnosticConsumer CaptureConsumer;
+ Diags.setClient(&CaptureConsumer, /*ShouldOwnClient=*/false);
+ Diags.Report(diag::pp_hash_warning) << "";
+ ASSERT_TRUE(CaptureConsumer.StoredDiags.size() == 1);
+
+ // Make sure an empty warning can round-trip with \c StoredDiagnostic.
+ Diags.Report(CaptureConsumer.StoredDiags.front());
+}
}