@@ -28,7 +28,7 @@ TEST(DebugLogTest, Basic) {
28
28
std::string str;
29
29
raw_string_ostream os (str);
30
30
LDGB_STREAM_LEVEL_AND_TYPE (os, " " , 0 ) << " NoType" ;
31
- EXPECT_FALSE (StringRef (os.str ()).starts_with (' [' ));
31
+ EXPECT_TRUE (StringRef (os.str ()).starts_with (' [' ));
32
32
EXPECT_TRUE (StringRef (os.str ()).ends_with (" NoType\n " ));
33
33
}
34
34
@@ -77,8 +77,8 @@ TEST(DebugLogTest, BasicWithLevel) {
77
77
for (int level : llvm::seq<int >(0 , 4 ))
78
78
LDBG_STREAM_LEVEL_TYPE_FILE_AND_LINE (os, level, type, type, level)
79
79
<< level;
80
- EXPECT_EQ (os.str (), " [A:0] A:0 0\n [A:1] A:1 1\n [A:2] A:2 2\n [A:3] A:3 "
81
- " 3 \n [B:0] B:0 0 \n [B:1] B:1 1\n [C:0] C:0 0\n " );
80
+ 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] "
81
+ " 0 \n [B:1 1] 1\n [C:0 0] 0\n " );
82
82
}
83
83
84
84
TEST (DebugLogTest, NegativeLevel) {
@@ -92,9 +92,10 @@ TEST(DebugLogTest, NegativeLevel) {
92
92
raw_string_ostream os (str);
93
93
for (auto type : {" A" , " B" })
94
94
for (int level : llvm::seq<int >(0 , 2 ))
95
- LDBG_STREAM_LEVEL_TYPE_FILE_AND_LINE (os, level, type, type, level)
95
+ LDBG_STREAM_LEVEL_TYPE_FILE_AND_LINE (
96
+ os, level, type, (std::string (type) + " .cpp" ).c_str (), level)
96
97
<< level;
97
- EXPECT_EQ (os.str (), " [A:0] A :0 0\n [B:0] B :0 0\n [B:1] B:1 1\n " );
98
+ 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 " );
98
99
}
99
100
100
101
TEST (DebugLogTest, StreamPrefix) {
@@ -141,35 +142,71 @@ TEST(DebugLogTest, LDBG_MACROS) {
141
142
#define LDBG_STREAM DebugOs
142
143
#define DEBUG_TYPE " A"
143
144
LDBG () << " Hello, world!" ;
144
- ExpectedOs << " [A:1] " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
145
- << " Hello, world!\n " ;
145
+ ExpectedOs << " [A " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
146
+ << " 1] Hello, world!\n " ;
146
147
EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
147
148
Str.clear ();
148
149
StrExpected.clear ();
149
150
150
151
// Test with a level, no type.
151
152
LDBG (2 ) << " Hello, world!" ;
152
- ExpectedOs << " [A:2] " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
153
- << " Hello, world!\n " ;
153
+ ExpectedOs << " [A " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
154
+ << " 2] Hello, world!\n " ;
154
155
EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
155
156
Str.clear ();
156
157
StrExpected.clear ();
157
158
158
- // Now the type will be explicit, check we don't use DEBUG_TYPE.
159
+ // Now check when we don't use DEBUG_TYPE, the file name is implicitly used
160
+ // instead.
159
161
#undef DEBUG_TYPE
160
162
163
+ // Repeat the tests above, they won't match since the debug types defined
164
+ // above don't match the file name.
165
+ LDBG () << " Hello, world!" ;
166
+ EXPECT_EQ (DebugOs.str (), " " );
167
+ Str.clear ();
168
+ StrExpected.clear ();
169
+
170
+ // Test with a level, no type.
171
+ LDBG (2 ) << " Hello, world!" ;
172
+ EXPECT_EQ (DebugOs.str (), " " );
173
+ Str.clear ();
174
+ StrExpected.clear ();
175
+
176
+ // Now enable the debug types that match the file name.
177
+ auto fileNameAndLevel = std::string (__LLVM_FILE_NAME__) + " :3" ;
178
+ static const char *DT2[] = {fileNameAndLevel.c_str (), " B:2" };
179
+ setCurrentDebugTypes (DT2, sizeof (DT2) / sizeof (DT2[0 ]));
180
+
181
+ // Repeat the tests above, they should match now.
182
+
183
+ LDBG () << " Hello, world!" ;
184
+ ExpectedOs << " [" << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
185
+ << " 1] Hello, world!\n " ;
186
+ EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
187
+ Str.clear ();
188
+ StrExpected.clear ();
189
+
190
+ // Test with a level, no type.
191
+ LDBG (2 ) << " Hello, world!" ;
192
+ ExpectedOs << " [" << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
193
+ << " 2] Hello, world!\n " ;
194
+ EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
195
+ Str.clear ();
196
+ StrExpected.clear ();
197
+
161
198
// Test with a type
162
199
LDBG (" B" ) << " Hello, world!" ;
163
- ExpectedOs << " [B:1] " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
164
- << " Hello, world!\n " ;
200
+ ExpectedOs << " [B " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
201
+ << " 1] Hello, world!\n " ;
165
202
EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
166
203
Str.clear ();
167
204
StrExpected.clear ();
168
205
169
206
// Test with a type and a level
170
207
LDBG (" B" , 2 ) << " Hello, world!" ;
171
- ExpectedOs << " [B:2] " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
172
- << " Hello, world!\n " ;
208
+ ExpectedOs << " [B " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
209
+ << " 2] Hello, world!\n " ;
173
210
EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
174
211
Str.clear ();
175
212
StrExpected.clear ();
@@ -181,6 +218,8 @@ TEST(DebugLogTest, LDBG_MACROS) {
181
218
// Test with a level not enabled.
182
219
LDBG (" B" , 3 ) << " Hello, world!" ;
183
220
EXPECT_EQ (DebugOs.str (), " " );
221
+ LDBG (__LLVM_FILE_NAME__, 4 ) << " Hello, world!" ;
222
+ EXPECT_EQ (DebugOs.str (), " " );
184
223
}
185
224
186
225
TEST (DebugLogTest, LDBG_OS_MACROS) {
@@ -195,35 +234,70 @@ TEST(DebugLogTest, LDBG_OS_MACROS) {
195
234
#define LDBG_STREAM DebugOs
196
235
#define DEBUG_TYPE " A"
197
236
LDBG_OS ([](raw_ostream &Os) { Os << " Hello, world!" ; });
198
- ExpectedOs << " [A:1] " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
199
- << " Hello, world!\n " ;
237
+ ExpectedOs << " [A " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
238
+ << " 1] Hello, world!\n " ;
200
239
EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
201
240
Str.clear ();
202
241
StrExpected.clear ();
203
242
204
243
// Test with a level, no type.
205
244
LDBG_OS (2 , [](raw_ostream &Os) { Os << " Hello, world!" ; });
206
- ExpectedOs << " [A:2] " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
207
- << " Hello, world!\n " ;
245
+ ExpectedOs << " [A " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
246
+ << " 2] Hello, world!\n " ;
208
247
EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
209
248
Str.clear ();
210
249
StrExpected.clear ();
211
250
212
- // Now the type will be explicit, check we don't use DEBUG_TYPE.
251
+ // Now check when we don't use DEBUG_TYPE, the file name is implicitly used
252
+ // instead.
213
253
#undef DEBUG_TYPE
214
254
255
+ // Repeat the tests above, they won't match since the debug types defined
256
+ // above don't match the file name.
257
+ LDBG_OS ([](raw_ostream &Os) { Os << " Hello, world!" ; });
258
+ EXPECT_EQ (DebugOs.str (), " " );
259
+ Str.clear ();
260
+ StrExpected.clear ();
261
+
262
+ // Test with a level, no type.
263
+ LDBG_OS (2 , [](raw_ostream &Os) { Os << " Hello, world!" ; });
264
+ EXPECT_EQ (DebugOs.str (), " " );
265
+ Str.clear ();
266
+ StrExpected.clear ();
267
+
268
+ // Now enable the debug types that match the file name.
269
+ auto fileNameAndLevel = std::string (__LLVM_FILE_NAME__) + " :3" ;
270
+ static const char *DT2[] = {fileNameAndLevel.c_str (), " B:2" };
271
+ setCurrentDebugTypes (DT2, sizeof (DT2) / sizeof (DT2[0 ]));
272
+
273
+ // Repeat the tests above, they should match now.
274
+ LDBG_OS ([](raw_ostream &Os) { Os << " Hello, world!" ; });
275
+ ExpectedOs << " [" << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
276
+ << " 1] Hello, world!\n " ;
277
+ EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
278
+ Str.clear ();
279
+ StrExpected.clear ();
280
+
281
+ // Test with a level, no type.
282
+ LDBG_OS (2 , [](raw_ostream &Os) { Os << " Hello, world!" ; });
283
+ ExpectedOs << " [" << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
284
+ << " 2] Hello, world!\n " ;
285
+ EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
286
+ Str.clear ();
287
+ StrExpected.clear ();
288
+
215
289
// Test with a type.
216
290
LDBG_OS (" B" , [](raw_ostream &Os) { Os << " Hello, world!" ; });
217
- ExpectedOs << " [B:1] " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
218
- << " Hello, world!\n " ;
291
+ ExpectedOs << " [B " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
292
+ << " 1] Hello, world!\n " ;
219
293
EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
220
294
Str.clear ();
221
295
StrExpected.clear ();
222
296
223
297
// Test with a type and a level
224
298
LDBG_OS (" B" , 2 , [](raw_ostream &Os) { Os << " Hello, world!" ; });
225
- ExpectedOs << " [B:2] " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
226
- << " Hello, world!\n " ;
299
+ ExpectedOs << " [B " << __LLVM_FILE_NAME__ << " :" << (__LINE__ - 1 )
300
+ << " 2] Hello, world!\n " ;
227
301
EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
228
302
Str.clear ();
229
303
StrExpected.clear ();
0 commit comments