|
| 1 | +//===- llvm/unittest/Support/DebugLogTest.cpp -----------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/Support/DebugLog.h" |
| 10 | +#include "llvm/Support/raw_ostream.h" |
| 11 | +#include "gmock/gmock.h" |
| 12 | +#include "gtest/gtest.h" |
| 13 | + |
| 14 | +#include <string> |
| 15 | +using namespace llvm; |
| 16 | +using testing::Eq; |
| 17 | +using testing::HasSubstr; |
| 18 | + |
| 19 | +#ifndef NDEBUG |
| 20 | +TEST(DebugLogTest, Basic) { |
| 21 | + llvm::DebugFlag = true; |
| 22 | + static const char *DT[] = {"A", "B"}; |
| 23 | + |
| 24 | + // Clear debug types. |
| 25 | + setCurrentDebugTypes(DT, 0); |
| 26 | + { |
| 27 | + std::string str; |
| 28 | + raw_string_ostream os(str); |
| 29 | + DEBUGLOG_WITH_STREAM_AND_TYPE(os, nullptr) << "NoType"; |
| 30 | + EXPECT_TRUE(StringRef(os.str()).starts_with('[')); |
| 31 | + EXPECT_TRUE(StringRef(os.str()).ends_with("NoType\n")); |
| 32 | + } |
| 33 | + |
| 34 | + setCurrentDebugTypes(DT, 2); |
| 35 | + { |
| 36 | + std::string str; |
| 37 | + raw_string_ostream os(str); |
| 38 | + DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "A"; |
| 39 | + DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << "B"; |
| 40 | + EXPECT_THAT(os.str(), AllOf(HasSubstr("A\n"), HasSubstr("B\n"))); |
| 41 | + } |
| 42 | + |
| 43 | + setCurrentDebugType("A"); |
| 44 | + { |
| 45 | + std::string str; |
| 46 | + raw_string_ostream os(str); |
| 47 | + // Just check that the macro doesn't result in dangling else. |
| 48 | + if (true) |
| 49 | + DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "A"; |
| 50 | + else |
| 51 | + DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "B"; |
| 52 | + DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << "B"; |
| 53 | + EXPECT_THAT(os.str(), AllOf(HasSubstr("A\n"), Not(HasSubstr("B\n")))); |
| 54 | + |
| 55 | + int count = 0; |
| 56 | + auto inc = [&]() { return ++count; }; |
| 57 | + EXPECT_THAT(count, Eq(0)); |
| 58 | + DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << inc(); |
| 59 | + EXPECT_THAT(count, Eq(1)); |
| 60 | + DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << inc(); |
| 61 | + EXPECT_THAT(count, Eq(1)); |
| 62 | + } |
| 63 | +} |
| 64 | +#else |
| 65 | +TEST(DebugLogTest, Basic) { |
| 66 | + // LDBG should be compiled out in NDEBUG, so just check it compiles and has |
| 67 | + // no effect. |
| 68 | + llvm::DebugFlag = true; |
| 69 | + static const char *DT[] = {}; |
| 70 | + setCurrentDebugTypes(DT, 0); |
| 71 | + int count = 0; |
| 72 | + auto inc = [&]() { return ++count; }; |
| 73 | + EXPECT_THAT(count, Eq(0)); |
| 74 | + LDBG() << inc(); |
| 75 | + EXPECT_THAT(count, Eq(0)); |
| 76 | +} |
| 77 | +#endif |
0 commit comments