@@ -28,7 +28,7 @@ TEST(DebugLogTest, Basic) {
2828 std::string str;
2929 raw_string_ostream os (str);
3030 LDGB_STREAM_LEVEL_AND_TYPE (os, " " , 0 ) << " NoType" ;
31- EXPECT_FALSE (StringRef (os.str ()).starts_with (' [' ));
31+ EXPECT_TRUE (StringRef (os.str ()).starts_with (' [' ));
3232 EXPECT_TRUE (StringRef (os.str ()).ends_with (" NoType\n " ));
3333 }
3434
@@ -77,8 +77,8 @@ TEST(DebugLogTest, BasicWithLevel) {
7777 for (int level : llvm::seq<int >(0 , 4 ))
7878 LDBG_STREAM_LEVEL_TYPE_FILE_AND_LINE (os, level, type, type, level)
7979 << 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 " );
8282}
8383
8484TEST (DebugLogTest, NegativeLevel) {
@@ -92,9 +92,10 @@ TEST(DebugLogTest, NegativeLevel) {
9292 raw_string_ostream os (str);
9393 for (auto type : {" A" , " B" })
9494 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)
9697 << 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 " );
9899}
99100
100101TEST (DebugLogTest, StreamPrefix) {
@@ -141,35 +142,71 @@ TEST(DebugLogTest, LDBG_MACROS) {
141142#define LDBG_STREAM DebugOs
142143#define DEBUG_TYPE " A"
143144 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 " ;
146147 EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
147148 Str.clear ();
148149 StrExpected.clear ();
149150
150151 // Test with a level, no type.
151152 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 " ;
154155 EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
155156 Str.clear ();
156157 StrExpected.clear ();
157158
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.
159161#undef DEBUG_TYPE
160162
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+
161198 // Test with a type
162199 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 " ;
165202 EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
166203 Str.clear ();
167204 StrExpected.clear ();
168205
169206 // Test with a type and a level
170207 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 " ;
173210 EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
174211 Str.clear ();
175212 StrExpected.clear ();
@@ -181,6 +218,8 @@ TEST(DebugLogTest, LDBG_MACROS) {
181218 // Test with a level not enabled.
182219 LDBG (" B" , 3 ) << " Hello, world!" ;
183220 EXPECT_EQ (DebugOs.str (), " " );
221+ LDBG (__LLVM_FILE_NAME__, 4 ) << " Hello, world!" ;
222+ EXPECT_EQ (DebugOs.str (), " " );
184223}
185224
186225TEST (DebugLogTest, LDBG_OS_MACROS) {
@@ -195,35 +234,70 @@ TEST(DebugLogTest, LDBG_OS_MACROS) {
195234#define LDBG_STREAM DebugOs
196235#define DEBUG_TYPE " A"
197236 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 " ;
200239 EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
201240 Str.clear ();
202241 StrExpected.clear ();
203242
204243 // Test with a level, no type.
205244 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 " ;
208247 EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
209248 Str.clear ();
210249 StrExpected.clear ();
211250
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.
213253#undef DEBUG_TYPE
214254
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+
215289 // Test with a type.
216290 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 " ;
219293 EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
220294 Str.clear ();
221295 StrExpected.clear ();
222296
223297 // Test with a type and a level
224298 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 " ;
227301 EXPECT_EQ (DebugOs.str (), ExpectedOs.str ());
228302 Str.clear ();
229303 StrExpected.clear ();
0 commit comments