aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/DebugLogTest.cpp
diff options
context:
space:
mode:
authorJacques Pienaar <jpienaar@google.com>2025-07-25 06:58:05 +0200
committerGitHub <noreply@github.com>2025-07-25 06:58:05 +0200
commitd368d117e7ee9720acd3eac6fb41c0885575a114 (patch)
tree50210a5e7543a65fdca3e4b70287d110651d6af5 /llvm/unittests/Support/DebugLogTest.cpp
parentbd91e8a5bd115be1350d4ad3a7100303511b1d15 (diff)
downloadllvm-d368d117e7ee9720acd3eac6fb41c0885575a114.zip
llvm-d368d117e7ee9720acd3eac6fb41c0885575a114.tar.gz
llvm-d368d117e7ee9720acd3eac6fb41c0885575a114.tar.bz2
[llvm][support] Add LDBG macro. (#143704)
Add macro that mirror a common usage of logging to output .This makes it easy to have streaming log like behavior while still using the base debug logging. I also wanted to avoid inventing a full logging library here while enabling others to change the sink without too much pain, so put it in its own header (this also avoids making Debug depend on raw_ostream beyond forward reference). The should allow a consistent dev experience without fixing the sink too much. --------- Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
Diffstat (limited to 'llvm/unittests/Support/DebugLogTest.cpp')
-rw-r--r--llvm/unittests/Support/DebugLogTest.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/llvm/unittests/Support/DebugLogTest.cpp b/llvm/unittests/Support/DebugLogTest.cpp
new file mode 100644
index 0000000..5136999
--- /dev/null
+++ b/llvm/unittests/Support/DebugLogTest.cpp
@@ -0,0 +1,77 @@
+//===- llvm/unittest/Support/DebugLogTest.cpp -----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/DebugLog.h"
+#include "llvm/Support/raw_ostream.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include <string>
+using namespace llvm;
+using testing::Eq;
+using testing::HasSubstr;
+
+#ifndef NDEBUG
+TEST(DebugLogTest, Basic) {
+ llvm::DebugFlag = true;
+ static const char *DT[] = {"A", "B"};
+
+ // Clear debug types.
+ setCurrentDebugTypes(DT, 0);
+ {
+ std::string str;
+ raw_string_ostream os(str);
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os, nullptr) << "NoType";
+ EXPECT_TRUE(StringRef(os.str()).starts_with('['));
+ EXPECT_TRUE(StringRef(os.str()).ends_with("NoType\n"));
+ }
+
+ setCurrentDebugTypes(DT, 2);
+ {
+ std::string str;
+ raw_string_ostream os(str);
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "A";
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << "B";
+ EXPECT_THAT(os.str(), AllOf(HasSubstr("A\n"), HasSubstr("B\n")));
+ }
+
+ setCurrentDebugType("A");
+ {
+ std::string str;
+ raw_string_ostream os(str);
+ // Just check that the macro doesn't result in dangling else.
+ if (true)
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "A";
+ else
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "B";
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << "B";
+ EXPECT_THAT(os.str(), AllOf(HasSubstr("A\n"), Not(HasSubstr("B\n"))));
+
+ int count = 0;
+ auto inc = [&]() { return ++count; };
+ EXPECT_THAT(count, Eq(0));
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << inc();
+ EXPECT_THAT(count, Eq(1));
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << inc();
+ EXPECT_THAT(count, Eq(1));
+ }
+}
+#else
+TEST(DebugLogTest, Basic) {
+ // LDBG should be compiled out in NDEBUG, so just check it compiles and has
+ // no effect.
+ llvm::DebugFlag = true;
+ static const char *DT[] = {};
+ setCurrentDebugTypes(DT, 0);
+ int count = 0;
+ auto inc = [&]() { return ++count; };
+ EXPECT_THAT(count, Eq(0));
+ LDBG() << inc();
+ EXPECT_THAT(count, Eq(0));
+}
+#endif