aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/DebugLogTest.cpp
blob: 4df5a809d11aabced7c93e16e7014ffbd3dbf737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//===- 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/ADT/Sequence.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);
    LDGB_STREAM_LEVEL_AND_TYPE(os, "", 0) << "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);
    LDGB_STREAM_LEVEL_AND_TYPE(os, 0, "A") << "A";
    LDGB_STREAM_LEVEL_AND_TYPE(os, "B", 0) << "B";
    EXPECT_TRUE(StringRef(os.str()).starts_with('['));
    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)
      LDGB_STREAM_LEVEL_AND_TYPE(os, 0, "A") << "A";
    else
      LDGB_STREAM_LEVEL_AND_TYPE(os, 0, "A") << "B";
    LDGB_STREAM_LEVEL_AND_TYPE(os, 0, "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));
    LDGB_STREAM_LEVEL_AND_TYPE(os, 0, "A") << inc();
    EXPECT_THAT(count, Eq(1));
    LDGB_STREAM_LEVEL_AND_TYPE(os, 0, "B") << inc();
    EXPECT_THAT(count, Eq(1));
  }
}

TEST(DebugLogTest, BasicWithLevel) {
  llvm::DebugFlag = true;
  // We expect A to be always printed, B to be printed only when level is 1 or
  // below, and C to be printed only when level is 0 or below.
  static const char *DT[] = {"A", "B:1", "C:"};

  setCurrentDebugTypes(DT, sizeof(DT) / sizeof(DT[0]));
  std::string str;
  raw_string_ostream os(str);
  for (auto type : {"A", "B", "C", "D"})
    for (int level : llvm::seq<int>(0, 4))
      LDBG_STREAM_LEVEL_TYPE_FILE_AND_LINE(os, level, type, type, level)
          << level;
  EXPECT_EQ(os.str(), "[A:0 0] 0\n[A:1 1] 1\n[A:2 2] 2\n[A:3 3] 3\n[B:0 0] "
                      "0\n[B:1 1] 1\n[C:0 0] 0\n");
}

TEST(DebugLogTest, NegativeLevel) {
  llvm::DebugFlag = true;
  // Test the special behavior when all the levels are 0.
  // In this case we expect all the debug types to be printed.
  static const char *DT[] = {"A:"};

  setCurrentDebugTypes(DT, sizeof(DT) / sizeof(DT[0]));
  std::string str;
  raw_string_ostream os(str);
  for (auto type : {"A", "B"})
    for (int level : llvm::seq<int>(0, 2))
      LDBG_STREAM_LEVEL_TYPE_FILE_AND_LINE(
          os, level, type, (std::string(type) + ".cpp").c_str(), level)
          << level;
  EXPECT_EQ(os.str(), "[A A.cpp:0 0] 0\n[B B.cpp:0 0] 0\n[B B.cpp:1 1] 1\n");
}

TEST(DebugLogTest, StreamPrefix) {
  llvm::DebugFlag = true;
  static const char *DT[] = {"A", "B"};
  setCurrentDebugTypes(DT, 2);

  std::string str;
  raw_string_ostream os(str);
  std::string expected = "PrefixA 1\nPrefixA 2\nPrefixA \nPrefixB "
                         "3\nPrefixB 4\nPrefixA 5";
  {
    llvm::impl::raw_ldbg_ostream ldbg_osB("PrefixB ", os);
    llvm::impl::raw_ldbg_ostream ldbg_osA("PrefixA ", os);
    ldbg_osA << "1\n2";
    ldbg_osA << "\n\n";
    ldbg_osB << "3\n4\n";
    ldbg_osA << "5";
    EXPECT_EQ(os.str(), expected);
  }
  EXPECT_EQ(os.str(), expected);
}

TEST(DebugLogTest, DestructorPrefix) {
  llvm::DebugFlag = true;
  std::string str;
  raw_string_ostream os(str);
  {
    llvm::impl::raw_ldbg_ostream ldbg_osB("PrefixB ", os);
  }
  // After destructors, nothing should have been printed.
  EXPECT_EQ(os.str(), "");
}

TEST(DebugLogTest, LDBG_MACROS) {
  llvm::DebugFlag = true;
  static const char *DT[] = {"A:3", "B:2"};
  setCurrentDebugTypes(DT, sizeof(DT) / sizeof(DT[0]));
  std::string Str;
  raw_string_ostream DebugOs(Str);
  std::string StrExpected;
  raw_string_ostream ExpectedOs(StrExpected);
#undef LDBG_STREAM
#define LDBG_STREAM DebugOs
#define DEBUG_TYPE "A"
  LDBG() << "Hello, world!";
  ExpectedOs << "[A " << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 1] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a level, no type.
  LDBG(2) << "Hello, world!";
  ExpectedOs << "[A " << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 2] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

// Now check when we don't use DEBUG_TYPE, the file name is implicitly used
// instead.
#undef DEBUG_TYPE

  // Repeat the tests above, they won't match since the debug types defined
  // above don't match the file name.
  LDBG() << "Hello, world!";
  EXPECT_EQ(DebugOs.str(), "");
  Str.clear();
  StrExpected.clear();

  // Test with a level, no type.
  LDBG(2) << "Hello, world!";
  EXPECT_EQ(DebugOs.str(), "");
  Str.clear();
  StrExpected.clear();

  // Now enable the debug types that match the file name.
  auto fileNameAndLevel = std::string(__LLVM_FILE_NAME__) + ":3";
  static const char *DT2[] = {fileNameAndLevel.c_str(), "B:2"};
  setCurrentDebugTypes(DT2, sizeof(DT2) / sizeof(DT2[0]));

  // Repeat the tests above, they should match now.

  LDBG() << "Hello, world!";
  ExpectedOs << "[" << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 1] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a level, no type.
  LDBG(2) << "Hello, world!";
  ExpectedOs << "[" << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 2] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a type
  LDBG("B") << "Hello, world!";
  ExpectedOs << "[B " << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 1] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a type and a level
  LDBG("B", 2) << "Hello, world!";
  ExpectedOs << "[B " << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 2] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a type not enabled.
  LDBG("C", 1) << "Hello, world!";
  EXPECT_EQ(DebugOs.str(), "");

  // Test with a level not enabled.
  LDBG("B", 3) << "Hello, world!";
  EXPECT_EQ(DebugOs.str(), "");
  LDBG(__LLVM_FILE_NAME__, 4) << "Hello, world!";
  EXPECT_EQ(DebugOs.str(), "");
}

TEST(DebugLogTest, LDBG_OS_MACROS) {
  llvm::DebugFlag = true;
  static const char *DT[] = {"A:3", "B:2"};
  setCurrentDebugTypes(DT, sizeof(DT) / sizeof(DT[0]));
  std::string Str;
  raw_string_ostream DebugOs(Str);
  std::string StrExpected;
  raw_string_ostream ExpectedOs(StrExpected);
#undef LDBG_STREAM
#define LDBG_STREAM DebugOs
#define DEBUG_TYPE "A"
  LDBG_OS([](raw_ostream &Os) { Os << "Hello, world!"; });
  ExpectedOs << "[A " << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 1] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a level, no type.
  LDBG_OS(2, [](raw_ostream &Os) { Os << "Hello, world!"; });
  ExpectedOs << "[A " << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 2] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

// Now check when we don't use DEBUG_TYPE, the file name is implicitly used
// instead.
#undef DEBUG_TYPE

  // Repeat the tests above, they won't match since the debug types defined
  // above don't match the file name.
  LDBG_OS([](raw_ostream &Os) { Os << "Hello, world!"; });
  EXPECT_EQ(DebugOs.str(), "");
  Str.clear();
  StrExpected.clear();

  // Test with a level, no type.
  LDBG_OS(2, [](raw_ostream &Os) { Os << "Hello, world!"; });
  EXPECT_EQ(DebugOs.str(), "");
  Str.clear();
  StrExpected.clear();

  // Now enable the debug types that match the file name.
  auto fileNameAndLevel = std::string(__LLVM_FILE_NAME__) + ":3";
  static const char *DT2[] = {fileNameAndLevel.c_str(), "B:2"};
  setCurrentDebugTypes(DT2, sizeof(DT2) / sizeof(DT2[0]));

  // Repeat the tests above, they should match now.
  LDBG_OS([](raw_ostream &Os) { Os << "Hello, world!"; });
  ExpectedOs << "[" << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 1] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a level, no type.
  LDBG_OS(2, [](raw_ostream &Os) { Os << "Hello, world!"; });
  ExpectedOs << "[" << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 2] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a type.
  LDBG_OS("B", [](raw_ostream &Os) { Os << "Hello, world!"; });
  ExpectedOs << "[B " << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 1] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a type and a level
  LDBG_OS("B", 2, [](raw_ostream &Os) { Os << "Hello, world!"; });
  ExpectedOs << "[B " << __LLVM_FILE_NAME__ << ":" << (__LINE__ - 1)
             << " 2] Hello, world!\n";
  EXPECT_EQ(DebugOs.str(), ExpectedOs.str());
  Str.clear();
  StrExpected.clear();

  // Test with a type not enabled.
  LDBG_OS("C", 1, [](raw_ostream &Os) { Os << "Hello, world!"; });
  EXPECT_EQ(DebugOs.str(), "");

  // Test with a level not enabled.
  LDBG_OS("B", 3, [](raw_ostream &Os) { Os << "Hello, world!"; });
  EXPECT_EQ(DebugOs.str(), "");
}

#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[] = {"A"};
  setCurrentDebugTypes(DT, 0);
  int count = 0;
  auto inc = [&]() { return ++count; };
  EXPECT_THAT(count, Eq(0));
  LDBG() << inc();
  EXPECT_THAT(count, Eq(0));
}
#endif