[llvm] Introduce LDBG_OS() macro as a variant of LDBG() (PR #157194)
Andrzej WarzyĆski via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 9 04:12:28 PDT 2025
================
@@ -76,29 +105,143 @@ namespace llvm {
#define __LLVM_FILE_NAME__ ::llvm::impl::getShortFileName(__FILE__)
#endif
-#define DEBUGLOG_WITH_STREAM_TYPE_FILE_AND_LINE(STREAM, LEVEL, TYPE, FILE, \
- LINE) \
- for (bool _c = \
- (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL)); \
+/// These macros detect if the DEBUG_TYPE or LDBG_DEBUG_STREAM macros are
+/// defined. They use a combination of preprocessor tricks and C++11
+/// user-defined string literals to achieve this.
+/// For example, if DEBUG_TYPE is defined to "foo", the preprocessor will expand
+/// the macro and then stringify the result to
+/// "foo"_LDBG_VARIABLE_CHECK
+/// This dispatches to the user-defined string literal operator named
+/// _LDBG_VARIABLE_CHECK which returns true. Otherwise it expands to
+/// DEBUG_TYPE_LDBG_VARIABLE_CHECK which we define as a macro that returns
+/// false.
+#define LDBG_VARIABLE_CHECK_(VARIABLE, ...) VARIABLE##__VA_ARGS__
+#define LDBG_VARIABLE_CHECK(VARIABLE) \
+ LDBG_VARIABLE_CHECK_(VARIABLE, _LDBG_VARIABLE_CHECK)
+/// User-defined string literal operator for the LDBG_VARIABLE_CHECK macro.
+constexpr bool operator""_LDBG_VARIABLE_CHECK(const char *, std::size_t) {
+ return true;
+}
+
+#define IS_DEBUG_TYPE_DEFINED() LDBG_VARIABLE_CHECK(DEBUG_TYPE)
+#define DEBUG_TYPE_LDBG_VARIABLE_CHECK 0
+
+#define IS_LDBG_DEBUG_STREAM_DEFINED() LDBG_VARIABLE_CHECK(LDBG_DEBUG_STREAM)
+#define LDBG_DEBUG_STREAM_LDBG_VARIABLE_CHECK 0
----------------
banach-space wrote:
```suggestion
/// These macros detect if the DEBUG_TYPE or LDBG_DEBUG_STREAM macros are
/// defined. They use a combination of preprocessor tricks and C++11
/// user-defined string literals to achieve this.
/// For example, if DEBUG_TYPE is defined to "foo", the preprocessor will expand
/// the macro and then stringify the result to:
/// * "foo"_LDBG_VARIABLE_CHECK
/// This dispatches to the user-defined string literal operator named
/// _LDBG_VARIABLE_CHECK which returns true. Otherwise it expands to
/// DEBUG_TYPE_LDBG_VARIABLE_CHECK which we define as a macro that returns
/// false.
#define LDBG_VARIABLE_CHECK_(VARIABLE, ...) VARIABLE##__VA_ARGS__
#define LDBG_VARIABLE_CHECK(VARIABLE) \
LDBG_VARIABLE_CHECK_(VARIABLE, _LDBG_VARIABLE_CHECK)
#define IS_DEBUG_TYPE_DEFINED() LDBG_VARIABLE_CHECK(DEBUG_TYPE)
#define IS_LDBG_DEBUG_STREAM_DEFINED() LDBG_VARIABLE_CHECK(LDBG_DEBUG_STREAM)
/// User-defined string literal operator for the LDBG_VARIABLE_CHECK macro.
constexpr bool operator""_LDBG_VARIABLE_CHECK(const char *, std::size_t) {
return true;
}
/// Fallback when LDBG_DEBUG_STREAM is undefined
#define LDBG_DEBUG_STREAM_LDBG_VARIABLE_CHECK 0
/// Fallback when DEBUG_TYPE is undefined
#define DEBUG_TYPE_LDBG_VARIABLE_CHECK 0
```
My main suggestion is to group these together:
* `operator""_LDBG_VARIABLE_CHECK`,
* `LDBG_DEBUG_STREAM_LDBG_VARIABLE_CHECK`,
* `DEBUG_TYPE_LDBG_VARIABLE_CHECK`.
To me, these provide similar "implementation detail", i.e. "is this defined or not"?
Otherwise I made a small punctuation suggestion.
https://github.com/llvm/llvm-project/pull/157194
More information about the llvm-commits
mailing list