Skip to content

Commit c4177f5

Browse files
committed
[clang/Diagnostic] Use optional to disambiguate between a StoredDiagMessage that is not set vs set as empty string
"But when would you have a completely empty diagnostic message", you ask dear reader? That is when there is an empty "#warning" in code. rdar://106155415 Differential Revision: https://reviews.llvm.org/D145256 (cherry picked from commit 5e03565)
1 parent ac6ccfa commit c4177f5

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

clang/include/clang/Basic/Diagnostic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ inline DiagnosticBuilder DiagnosticsEngine::Report(unsigned DiagID) {
15641564
/// currently in-flight diagnostic.
15651565
class Diagnostic {
15661566
const DiagnosticsEngine *DiagObj;
1567-
StringRef StoredDiagMessage;
1567+
std::optional<StringRef> StoredDiagMessage;
15681568

15691569
public:
15701570
explicit Diagnostic(const DiagnosticsEngine *DO) : DiagObj(DO) {}

clang/lib/Basic/Diagnostic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,8 @@ static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
801801
/// array.
802802
void Diagnostic::
803803
FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
804-
if (!StoredDiagMessage.empty()) {
805-
OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
804+
if (StoredDiagMessage.has_value()) {
805+
OutStr.append(StoredDiagMessage->begin(), StoredDiagMessage->end());
806806
return;
807807
}
808808

clang/unittests/Basic/DiagnosticTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "clang/Basic/Diagnostic.h"
1010
#include "clang/Basic/DiagnosticError.h"
1111
#include "clang/Basic/DiagnosticIDs.h"
12+
#include "clang/Basic/DiagnosticLex.h"
1213
#include "gtest/gtest.h"
1314

1415
using namespace llvm;
@@ -127,4 +128,26 @@ TEST(DiagnosticTest, diagnosticError) {
127128
EXPECT_EQ(*Value, std::make_pair(20, 1));
128129
EXPECT_EQ(Value->first, 20);
129130
}
131+
132+
TEST(DiagnosticTest, storedDiagEmptyWarning) {
133+
DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions);
134+
135+
class CaptureDiagnosticConsumer : public DiagnosticConsumer {
136+
public:
137+
SmallVector<StoredDiagnostic> StoredDiags;
138+
139+
void HandleDiagnostic(DiagnosticsEngine::Level level,
140+
const Diagnostic &Info) override {
141+
StoredDiags.push_back(StoredDiagnostic(level, Info));
142+
}
143+
};
144+
145+
CaptureDiagnosticConsumer CaptureConsumer;
146+
Diags.setClient(&CaptureConsumer, /*ShouldOwnClient=*/false);
147+
Diags.Report(diag::pp_hash_warning) << "";
148+
ASSERT_TRUE(CaptureConsumer.StoredDiags.size() == 1);
149+
150+
// Make sure an empty warning can round-trip with \c StoredDiagnostic.
151+
Diags.Report(CaptureConsumer.StoredDiags.front());
152+
}
130153
}

0 commit comments

Comments
 (0)