Skip to content

Commit 8e66344

Browse files
authored
[Support] Use macro var args to allow templates within DEBUG_WITH_TYPE (#117614)
Use variadic args with DEBUG_WITH_TYPE("name", ...) macros to resolve a compilation failure that occurs when using a comma within the last macro argument. Commas come up when instantiating templates such as SmallMapVector that require multiple template args.
1 parent 462cb3c commit 8e66344

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

llvm/include/llvm/Support/Debug.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,20 @@ void setCurrentDebugTypes(const char **Types, unsigned Count);
6161
///
6262
/// This will emit the debug information if -debug is present, and -debug-only
6363
/// is not specified, or is specified as "bitset".
64-
#define DEBUG_WITH_TYPE(TYPE, X) \
65-
do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { X; } \
64+
#define DEBUG_WITH_TYPE(TYPE, ...) \
65+
do { \
66+
if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { \
67+
__VA_ARGS__; \
68+
} \
6669
} while (false)
6770

6871
#else
6972
#define isCurrentDebugType(X) (false)
7073
#define setCurrentDebugType(X) do { (void)(X); } while (false)
7174
#define setCurrentDebugTypes(X, N) do { (void)(X); (void)(N); } while (false)
72-
#define DEBUG_WITH_TYPE(TYPE, X) do { } while (false)
75+
#define DEBUG_WITH_TYPE(TYPE, ...) \
76+
do { \
77+
} while (false)
7378
#endif
7479

7580
/// This boolean is set to true if the '-debug' command line option
@@ -98,7 +103,7 @@ raw_ostream &dbgs();
98103
//
99104
// LLVM_DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
100105
//
101-
#define LLVM_DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X)
106+
#define LLVM_DEBUG(...) DEBUG_WITH_TYPE(DEBUG_TYPE, __VA_ARGS__)
102107

103108
} // end namespace llvm
104109

llvm/unittests/Support/DebugTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Support/Debug.h"
10+
#include "llvm/ADT/MapVector.h"
11+
#include "llvm/Support/MathExtras.h"
1012
#include "llvm/Support/raw_ostream.h"
1113
#include "gtest/gtest.h"
1214

@@ -30,4 +32,22 @@ TEST(DebugTest, Basic) {
3032
DEBUG_WITH_TYPE("B", os2 << "B");
3133
EXPECT_EQ("A", os2.str());
3234
}
35+
36+
TEST(DebugTest, CommaInDebugBlock) {
37+
std::string s1, s2;
38+
raw_string_ostream os1(s1), os2(s2);
39+
static const char *DT[] = {"A", "B"};
40+
static const char Letters[] = {'X', 'Y', 'Z'};
41+
42+
llvm::DebugFlag = true;
43+
setCurrentDebugTypes(DT, 2);
44+
DEBUG_WITH_TYPE("A", {
45+
SmallMapVector<int, char, 4> map;
46+
for (int i = 0; i < 3; i++)
47+
map[i] = Letters[i];
48+
for (int i = 2; i >= 0; i--)
49+
os1 << map[i];
50+
});
51+
EXPECT_EQ("ZYX", os1.str());
52+
}
3353
#endif

0 commit comments

Comments
 (0)