https://github.com/jtstogel updated https://github.com/llvm/llvm-project/pull/190374
>From 6b8b34a84f1fb8ba66dc0b2f20e3870a43291715 Mon Sep 17 00:00:00 2001 From: jtstogel <[email protected]> Date: Fri, 3 Apr 2026 10:31:09 -0700 Subject: [PATCH 1/4] [DiagnosticInfo] Allow std::string_view in DiagnosticBuilder opeartor<<. After a68ae7b0cc0922b79114aabe8cf1ec8dc68524d7, calling `<<` with a `std::string_view` gives: ``` clang/include/clang/Basic/Diagnostic.h:1319:8: error: use of overloaded operator '<<' is ambiguous (with operand types 'const StreamingDiagnostic' and 'const std::string_view') 1319 | DB << V; | ~~ ^ ~ ``` --- clang/include/clang/Basic/Diagnostic.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h index 5a6f5fd61a5db..6dc2eb040c9ca 100644 --- a/clang/include/clang/Basic/Diagnostic.h +++ b/clang/include/clang/Basic/Diagnostic.h @@ -1374,6 +1374,12 @@ inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, return DB; } +inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, + std::string_view S) { + DB.AddString(S); + return DB; +} + inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, const std::string &S) { DB.AddString(S); >From e1578e5caa8ca7a0090eb1112492e567322497eb Mon Sep 17 00:00:00 2001 From: jtstogel <[email protected]> Date: Fri, 3 Apr 2026 11:20:40 -0700 Subject: [PATCH 2/4] Include string_view --- clang/include/clang/Basic/Diagnostic.h | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h index 6dc2eb040c9ca..c7c98c0a460fb 100644 --- a/clang/include/clang/Basic/Diagnostic.h +++ b/clang/include/clang/Basic/Diagnostic.h @@ -35,6 +35,7 @@ #include <map> #include <memory> #include <optional> +#include <string_view> #include <string> #include <type_traits> #include <utility> >From e10f38eda2be0daf471b5ccbac6612896a3afa62 Mon Sep 17 00:00:00 2001 From: jtstogel <[email protected]> Date: Fri, 3 Apr 2026 11:28:00 -0700 Subject: [PATCH 3/4] Format --- clang/include/clang/Basic/Diagnostic.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h index c7c98c0a460fb..aa05f9d60dadf 100644 --- a/clang/include/clang/Basic/Diagnostic.h +++ b/clang/include/clang/Basic/Diagnostic.h @@ -35,8 +35,8 @@ #include <map> #include <memory> #include <optional> -#include <string_view> #include <string> +#include <string_view> #include <type_traits> #include <utility> #include <vector> >From e1c2ad07c3a89e4d316c31bc9cd5b1e5b587d1d8 Mon Sep 17 00:00:00 2001 From: jtstogel <[email protected]> Date: Fri, 3 Apr 2026 13:10:33 -0700 Subject: [PATCH 4/4] Add test using DiagnosticBuilder w/ string_view. --- clang/unittests/Basic/DiagnosticTest.cpp | 56 ++++++++++++++++-------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/clang/unittests/Basic/DiagnosticTest.cpp b/clang/unittests/Basic/DiagnosticTest.cpp index 5492146f40fa9..2110af981dca2 100644 --- a/clang/unittests/Basic/DiagnosticTest.cpp +++ b/clang/unittests/Basic/DiagnosticTest.cpp @@ -23,6 +23,7 @@ #include "gtest/gtest.h" #include <memory> #include <optional> +#include <string_view> #include <vector> using namespace llvm; @@ -42,8 +43,19 @@ void clang::DiagnosticsTestHelper(DiagnosticsEngine &diag) { namespace { using testing::AllOf; using testing::ElementsAre; +using testing::HasSubstr; using testing::IsEmpty; +MATCHER_P(WithMessage, M, + "has diagnostic message that " + + ::testing::DescribeMatcher<std::string>(M)) { + return testing::ExplainMatchResult(M, arg.getMessage().str(), + result_listener); +} +MATCHER(IsError, "has error severity") { + return arg.getLevel() == DiagnosticsEngine::Level::Error; +} + // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics. TEST(DiagnosticTest, suppressAndTrap) { DiagnosticOptions DiagOpts; @@ -164,20 +176,20 @@ TEST(DiagnosticTest, diagnosticError) { EXPECT_EQ(Value->first, 20); } +class CaptureDiagnosticConsumer : public DiagnosticConsumer { +public: + SmallVector<StoredDiagnostic> StoredDiags; + + void HandleDiagnostic(DiagnosticsEngine::Level level, + const Diagnostic &Info) override { + StoredDiags.push_back(StoredDiagnostic(level, Info)); + } +}; + TEST(DiagnosticTest, storedDiagEmptyWarning) { DiagnosticOptions DiagOpts; DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts); - 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) << ""; @@ -187,6 +199,21 @@ TEST(DiagnosticTest, storedDiagEmptyWarning) { Diags.Report(CaptureConsumer.StoredDiags.front()); } +// std::string_view is used by downstream consumers. +TEST(DiagnosticTest, reportAcceptsStringViewMessage) { + DiagnosticOptions DiagOpts; + DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts); + + CaptureDiagnosticConsumer CaptureConsumer; + Diags.setClient(&CaptureConsumer, /*ShouldOwnClient=*/false); + + std::string_view SV = "diagnostic"; + Diags.Report(diag::err_target_unknown_triple) << SV; + + EXPECT_THAT(CaptureConsumer.StoredDiags, + ElementsAre(WithMessage(HasSubstr("diagnostic")))); +} + class SuppressionMappingTest : public testing::Test { public: SuppressionMappingTest() { @@ -226,13 +253,6 @@ class SuppressionMappingTest : public testing::Test { CaptureDiagnosticConsumer CaptureConsumer; }; -MATCHER_P(WithMessage, Msg, "has diagnostic message") { - return arg.getMessage() == Msg; -} -MATCHER(IsError, "has error severity") { - return arg.getLevel() == DiagnosticsEngine::Level::Error; -} - TEST_F(SuppressionMappingTest, MissingMappingFile) { Diags.getDiagnosticOptions().DiagnosticSuppressionMappingsFile = "foo.txt"; clang::ProcessWarningOptions(Diags, Diags.getDiagnosticOptions(), *FS); @@ -363,7 +383,7 @@ TEST_F(SuppressionMappingTest, IsIgnored) { auto ClangID = SM.createFileID(llvm::MemoryBuffer::getMemBuffer("", "clang/foo.h")); auto NonClangID = - SM.createFileID(llvm::MemoryBuffer::getMemBuffer("", "llvm/foo.h")); + SM.createFileID(llvm::MemoryBuffer::getMemBuffer("", "llvm/foo2.h")); auto PresumedClangID = SM.createFileID(llvm::MemoryBuffer::getMemBuffer("", "llvm/foo2.h")); // Add a line directive to point into clang/foo.h _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
