Skip to content

Commit 6c40a09

Browse files
author
tnowicki
committed
[Debug] Use macro var args to allow templates within a DEBUG_WITH_TYPE blocks
1 parent 7e3187e commit 6c40a09

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

llvm/include/llvm/Support/Debug.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ 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 { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { __VA_ARGS__; } \
6666
} while (false)
6767

6868
#else
6969
#define isCurrentDebugType(X) (false)
7070
#define setCurrentDebugType(X) do { (void)(X); } while (false)
7171
#define setCurrentDebugTypes(X, N) do { (void)(X); (void)(N); } while (false)
72-
#define DEBUG_WITH_TYPE(TYPE, X) do { } while (false)
72+
#define DEBUG_WITH_TYPE(TYPE, ...) do { } while (false)
7373
#endif
7474

7575
/// This boolean is set to true if the '-debug' command line option
@@ -98,7 +98,7 @@ raw_ostream &dbgs();
9898
//
9999
// LLVM_DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
100100
//
101-
#define LLVM_DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X)
101+
#define LLVM_DEBUG(...) DEBUG_WITH_TYPE(DEBUG_TYPE, __VA_ARGS__)
102102

103103
} // end namespace llvm
104104

llvm/unittests/Support/DebugTest.cpp

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

9+
#include "llvm/ADT/MapVector.h"
910
#include "llvm/Support/Debug.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)