[llvm] 8e66344 - [Support] Use macro var args to allow templates within DEBUG_WITH_TYPE (#117614)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 5 05:48:46 PST 2024


Author: Tyler Nowicki
Date: 2024-12-05T08:48:42-05:00
New Revision: 8e6634444876a59c1352a4d62704e93b23b7c5ae

URL: https://github.com/llvm/llvm-project/commit/8e6634444876a59c1352a4d62704e93b23b7c5ae
DIFF: https://github.com/llvm/llvm-project/commit/8e6634444876a59c1352a4d62704e93b23b7c5ae.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/Support/Debug.h
    llvm/unittests/Support/DebugTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/Debug.h b/llvm/include/llvm/Support/Debug.h
index 3e2f0d9b43fc0d..23c54ac9bb4b17 100644
--- a/llvm/include/llvm/Support/Debug.h
+++ b/llvm/include/llvm/Support/Debug.h
@@ -61,15 +61,20 @@ void setCurrentDebugTypes(const char **Types, unsigned Count);
 ///
 /// This will emit the debug information if -debug is present, and -debug-only
 /// is not specified, or is specified as "bitset".
-#define DEBUG_WITH_TYPE(TYPE, X)                                        \
-  do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { X; } \
+#define DEBUG_WITH_TYPE(TYPE, ...)                                             \
+  do {                                                                         \
+    if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) {               \
+      __VA_ARGS__;                                                             \
+    }                                                                          \
   } while (false)
 
 #else
 #define isCurrentDebugType(X) (false)
 #define setCurrentDebugType(X) do { (void)(X); } while (false)
 #define setCurrentDebugTypes(X, N) do { (void)(X); (void)(N); } while (false)
-#define DEBUG_WITH_TYPE(TYPE, X) do { } while (false)
+#define DEBUG_WITH_TYPE(TYPE, ...)                                             \
+  do {                                                                         \
+  } while (false)
 #endif
 
 /// This boolean is set to true if the '-debug' command line option
@@ -98,7 +103,7 @@ raw_ostream &dbgs();
 //
 // LLVM_DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
 //
-#define LLVM_DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X)
+#define LLVM_DEBUG(...) DEBUG_WITH_TYPE(DEBUG_TYPE, __VA_ARGS__)
 
 } // end namespace llvm
 

diff  --git a/llvm/unittests/Support/DebugTest.cpp b/llvm/unittests/Support/DebugTest.cpp
index e68e3764b43839..e8b754892a4c25 100644
--- a/llvm/unittests/Support/DebugTest.cpp
+++ b/llvm/unittests/Support/DebugTest.cpp
@@ -7,6 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/Debug.h"
+#include "llvm/ADT/MapVector.h"
+#include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include "gtest/gtest.h"
 
@@ -30,4 +32,22 @@ TEST(DebugTest, Basic) {
   DEBUG_WITH_TYPE("B", os2 << "B");
   EXPECT_EQ("A", os2.str());
 }
+
+TEST(DebugTest, CommaInDebugBlock) {
+  std::string s1, s2;
+  raw_string_ostream os1(s1), os2(s2);
+  static const char *DT[] = {"A", "B"};
+  static const char Letters[] = {'X', 'Y', 'Z'};
+
+  llvm::DebugFlag = true;
+  setCurrentDebugTypes(DT, 2);
+  DEBUG_WITH_TYPE("A", {
+    SmallMapVector<int, char, 4> map;
+    for (int i = 0; i < 3; i++)
+      map[i] = Letters[i];
+    for (int i = 2; i >= 0; i--)
+      os1 << map[i];
+  });
+  EXPECT_EQ("ZYX", os1.str());
+}
 #endif


        


More information about the llvm-commits mailing list