Skip to content

Commit

Permalink
?cxx/Macros.hxx: +SUSUWU_SH_ESC, +SUSUWU_SH_CSI
Browse files Browse the repository at this point in the history
	, +`SUSUWU_SH_BEL`, +`SUSUWU_SH_ST`, +`SUSUWU_SH_OSC`,
	+`SUSUWU_SH_TO_CLIPBOARD(base64_str)` /* work-in-progress, just has `fprintf` version, doesn't include `base64(str)` function */
	`%s/"\\033\[/SUSUWU_SH_CSI "/` /* Replace magic constants with macros which refer to conventions */
	+`SUSUWU_SH_RUNTIME_OSC`, +`SUSUWU_SH_RUNTIME_COLORS` /* define to replace `#ifdef _POSIX_VERSION\ncommand\n#endif` with `termcmp`/`GetConsoleMode()`, work-in-progress */
	+`SUSUWU_PRAGMA`
	`%s/SUSUWU_USE_STDERR/SUSUWU_SH_STDERR/` /* Use _SH_ for all macro constants which have to do with `bash`/`ash`/`sh` */
	`%s/SUSUWU_SH_STDERR/SUSUWU_SH_PREFER_STDIO/` /* Operating System Commands use `std::cout`/`stdout`, thus refer to  */
	?`UNREACHABLE`: `%s/defined static_assert/defined(SUSUWU_CXX11)/` /* Fix test for `static_analysis()` */
	?`ASSUME`: "warning: found assert() that could be replaced by static_assert() [cert-dcl03-c,hicpp-static-assert,misc-static-assert]" fix (if C++11, use `static_assert()`).

?`posts/VirusAnalysis.md`: Include all of above which is used

?`README.md`:
	+How to use `SUSUWU_SH_RUNTIME_OSC`, `SUSUWU_SH_RUNTIME_COLORS`
	`%s/SUSUWU_USE_STDERR/SUSUWU_SH_PREFER_STDIO/`

Has to do with #17
Is followup to 08ed827 (+`SUSUWU_CXX11`) ce649ab (?`cxx/Macros.hxx`: `clang-tidy` fixes)
  • Loading branch information
SwuduSusuwu committed Nov 2, 2024
1 parent 579a908 commit c97fa94
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 139 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ Targets: Windows/Linux/Android/OSX/iOS; all C++ compilers, requires some extensi

Usage: `./build.sh` produces `*.o` static libraries (for distribution to others,) plus `a.out` to do unit tests (test harnesses). Allowed flags; `--debug` (default; includes frame-pointers/debug symbols (`-g`), includes `valgrind`-replacement tools (such as `-fsanitize=address`), `--release` (excludes `--debug`, strips frame-pointers/symbols, optimizes with `-O2`), `--mingw` (if on _Linux_, can use with `--release` or `--debug`, produces _Portable Executable_ for _Windows_. If on _Windows_, the default is to produce _Portable Executable_'s for _Windows_)
Optional flags (`vim build.sh` to use):
`-DSUSUWU_USE_STDERR` to replace `std::cerr` with `fprintf(stderr, ...)`, default is `!defined(__cplusplus)`.
`-DSUSUWU_SH_PREFER_STDIO` to replace `std::cXXX << ...` with `fprintf(stdXXX, ...)`, default is `!defined(__cplusplus)`.
`-DSUSUWU_SH_SKIP_BRACKETS = true` sets output format to `WARN_LEVEL: message`, default is `false`.
`-DSUSUWU_SH_FILE = true` sets output format to `[__FILE__: WARN_LEVEL: message]`, default is `!defined(NDEBUG)`.
`-DSUSUWU_SH_LINE = true` sets output format to `[__LINE__: WARN_LEVEL: message]`, default is `!defined(NDEBUG)`.
`-DSUSUWU_SH_FUNC = true` sets output format to `[__func__: WARN_LEVEL: message]`, default is `false`.
`-DSUSUWU_SH_SKIP_COLORS = true` to omit _VT100_ (_ANSI_) colors, default is `defined(SUSUWU_SH_COLORS_UNSUPPORTED)`).
`-DSUSUWU_SH_SKIP_COLORS = false` to force (even if unsupported) _VT100_ (_ANSI_) color use.
Flags which https://github.com/SwuduSusuwu/SubStack/issues/17 will introduce (TODO):
`-DSUSUWU_SH_RUNTIME_OSC` to replace `#ifdef _POSIX_VERSION\nAccessClipboard();\n#endif` with `termcmp`/`GetConsoleMode()` (for choices on whether or not to use Operating System Commands,) default is undefined.
`-DSUSUWU_SH_RUNTIME_COLORS` to replace `#if _POSIX_VERSION\nColors();\n#endif` `termcmp`/`GetConsoleMode()` (for choices on whether or not to use colors,) default is undefined.
To match `g++`/`clang++` output rules, use `-DSUSUWU_SKIP_BRACKETS = true, -DSUSUWU_SH_FILE = true, -DSUSUWU_SH_LINE = true, -DSUSUWU_SH_FUNC = false, -DSUSUWU_SKIP_COLORS = false` (sets output format to `__FILE__:__LINE__: WARN_LEVEL: message`).

Linter: `clang-tidy cxx/*.cxx` /* uses `.clang_tidy` options */
Expand Down
200 changes: 121 additions & 79 deletions cxx/Macros.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#ifndef INCLUDES_cxx_Macros_hxx
#define INCLUDES_cxx_Macros_hxx
/* Miscellaneous macros */
#include <cassert> /* assert static_assert */
#include <iostream> /* std::cerr std::endl */
namespace Susuwu { /* namespaces do not affect macros. Is just standard practice to wrap all of a project's contents with namespaces. */
/* To printout default preprocessor definitions:
* for X={clang, clang++, gcc, g++, hipcc, icc}: `$X -dM -E -x c++ /dev/null`
* replace `/dev/null` with a file (such as `cxx/Macros.hxx`) to printout actual preprocessor definitions
Expand All @@ -14,87 +11,132 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
* to `clang`/`clang++`/`gcc`/`g++`/Intel(`icc`): `-DUSE_CONTRACTS=true`
* to MSVC(`cl`): `\DUSE_CONTRACTS=true`
*/
#ifdef __cplusplus
# include <cassert> /* assert static_assert */
/* `clang-tidy` off: NOLINTBEGIN(cppcoreguidelines-macro-usage); https://github.com/SwuduSusuwu/SubStack/issues/3 more simple with macros, plus some of the `constexpr` versions require `__cplusplus >= 202002` */
# define IF_SUSUWU_CPLUSPLUS(TRUE, FALSE) TRUE
#else /* !(defined __cplusplus */
# include <assert.h> /* assert */
# define IF_SUSUWU_CPLUSPLUS(TRUE, FALSE) FALSE
# define SUSUWU_SH_PREFER_STDIO
#endif /* !(defined __cplusplus */
#ifdef SUSUWU_SH_PREFER_STDIO /* `-DSUSUWU_SH_PREFER_STDIO` to force this. Replaces `std::cXXX << x << std::endl;` with `fprintf(stdXXX, "%s\n", x);` */
# include <stdio.h> /* fprintf stderr stdout */
#else
# include <iostream> /* std::cerr std::cout std::endl */
#endif
namespace Susuwu { /* namespaces do not affect macros. Is just standard practice to wrap all of a project's contents with namespaces. */
#define SUSUWU_GLUE2(S, U) S##U /* concatanates 2 macro constants */
#define SUSUWU_GLUE(S, U) SUSUWU_GLUE2(S, U) /* concatanates 2 macro functions or constants */
#define SUSUWU_COMMA , /* to pass to macro functions whose `__VA_ARGS__` is conditional */
#if (!defined _POSIX_VERSION) && (defined _POSIX_C_SOURCE)
# define _POSIX_VERSION _POSIX_C_SOURCE /* "Error: ... ndef _POSIX_VERSION" fix. Now, you can just do `#ifdef _POSIX_VERSION` for POSIX code paths */
#endif /* (!defined _POSIX_VERSION) && (defined _POSIX_C_SOURCE) */

/* `#pragma S` in macro functions is `_Pragma(S)`, but without this wrap gives `error: _Pragma takes a parenthesized string literal`/`expected string literal in pragma message`. Use as `SUSUWU_PRAGMA(message("Message"))` */
#define SUSUWU_PRAGMA(S) _Pragma(#S)

#if !defined(SUSUWU_SH_SKIP_BRACKETS) || SUSUWU_SH_SKIP_BRACKETS == false /* overridable with `-DSUSUWU_SH_SKIP_BRACKETS true` (which you can set to mimic `g++`/`clang++` syntax for outputs) */
# define IF_SUSUWU_SH_BRACKETS(TRUE, FALSE) TRUE
# define IF_SUSUWU_SH_BRACKETS(TRUE, FALSE) TRUE
#else
# define IF_SUSUWU_SH_BRACKETS(TRUE, FALSE) FALSE
# define IF_SUSUWU_SH_BRACKETS(TRUE, FALSE) FALSE
#endif

#if (!defined(SUSUWU_SH_FILE) && !defined(NDEBUG)) || SUSUWU_SH_FILE /* overridable with `-DSUSUWU_SH_FILE true/false` */
# define SUSUWU_SH_USE_FILE /* affix `__FILE__ ":"` to `stderr`/`cerr` printout */
# define SUSUWU_SH_USE_FILE /* affix `__FILE__ ":"` to `stderr`/`cerr` printout */
#endif
#if (!defined(SUSUWU_SH_LINE) && !defined(NDEBUG)) || SUSUWU_SH_LINE /* overridable with `-DSUSUWU_SH_LINE true/false` */
# define SUSUWU_SH_USE_LINE /* affix `__LINE__ ":"` to `stderr`/`cerr` printout */
# define SUSUWU_SH_USE_LINE /* affix `__LINE__ ":"` to `stderr`/`cerr` printout */
#endif
#if defined(SUSUWU_SH_FUNC) && SUSUWU_SH_FUNC /* overridable with `-DSUSUWU_SH_FUNC true/false` */
# define SUSUWU_SH_USE_FUNC /* affix `__func__ ":"` to `stderr`/`cerr` printout */
# define SUSUWU_SH_USE_FUNC /* affix `__func__ ":"` to `stderr`/`cerr` printout */
#endif
#ifdef SUSUWU_SH_USE_FILE
# define IF_SUSUWU_SH_FILE(U /* wrap clauses which print __FILE__ to `cerr`/`cout` */) U /* printout */
# define IF_SUSUWU_SH_FILE(U /* wrap clauses which print __FILE__ to `cerr`/`cout` */) U /* printout */
#else
# define IF_SUSUWU_SH_FILE(U) /* don't printout */
# define IF_SUSUWU_SH_FILE(U) /* don't printout */
#endif
#ifdef SUSUWU_SH_USE_LINE
# define IF_SUSUWU_SH_LINE(U /* wrap clauses which print __LINE__ to `cerr`/`cout` */) U /* printout */
# define IF_SUSUWU_SH_LINE(U /* wrap clauses which print __LINE__ to `cerr`/`cout` */) U /* printout */
#else
# define IF_SUSUWU_SH_LINE(U) /* don't printout */
# define IF_SUSUWU_SH_LINE(U) /* don't printout */
#endif
#ifdef SUSUWU_SH_USE_FUNC
# define IF_SUSUWU_SH_FUNC(U /* wrap clauses which print __func__ to `cerr`/`cout` */) U /* printout */
# define IF_SUSUWU_SH_FUNC(U /* wrap clauses which print __func__ to `cerr`/`cout` */) U /* printout */
#else
# define IF_SUSUWU_SH_FUNC(U) /* don't printout */
# define IF_SUSUWU_SH_FUNC(U) /* don't printout */
#endif
#if defined(SUSUWU_SH_USE_FILE) || defined(SUSUWU_SH_USE_LINE) || defined(SUSUWU_SH_USE_FUNC)
# define IF_SUSUWU_SH_FILE_LINE_OR_FUNC(U /* wrap clauses common to `__FILE__`, `__LINE__`, `__func__` use */) U /* printout */
# define IF_SUSUWU_SH_FILE_LINE_OR_FUNC(U /* wrap clauses common to `__FILE__`, `__LINE__`, `__func__` use */) U /* printout */
#else
# define IF_SUSUWU_SH_FILE_LINE_OR_FUNC(U) /* don't printout */
# define IF_SUSUWU_SH_FILE_LINE_OR_FUNC(U) /* don't printout */
#endif

#define SUSUWU_SH_COLORS_UNSUPPORTED __MINGW32__ /* MinGW + WINE shows the literal ANSI color codes in console */
#if SUSUWU_SH_COLORS_UNSUPPORTED && !defined SUSUWU_SH_SKIP_COLORS
# define SUSUWU_SH_SKIP_COLORS true /* you can use `-DSUSUWU_SH_SKIP_COLORS=false` to force unsupported color use (such as if _MinGW_ will target _Windows_ but the executables aren't for use on _WINE_/`localhost`) */
#ifdef SUSUWU_SH_RUNTIME_COLORS
# pragma message("[Notice: SUSUWU_SH_RUNTIME_COLORS is TODO; https://github.com/SwuduSusuwu/SubStack/issues/17 to contribute]")
#endif /* #elif !defined(_POSIX_VERSION) TODO */
#ifdef SUSUWU_SH_RUNTIME_OSC
# pragma message("[Notice: SUSUWU_SH_RUNTIME_OSC is TODO; https://github.com/SwuduSusuwu/SubStack/issues/17 to contribute]")
#endif /* #elif !defined(_POSIX_VERSION) TODO */
#if !defined(_POSIX_VERSION)
# define SUSUWU_SH_COLORS_UNSUPPORTED /* assume "dumb terminals" (such as __WIN32__ or __MINGW32__ often has) if built without runtime tests for color attributes, on non-POSIX systems */
# define SUSUWU_SH_OSC_UNSUPPORTED /* assume "dumb terminals" (such as __WIN32__ or __MINGW32__ often has) if built without runtime tests for Operating System Commands, on non-POSIX systems */
#endif
#if defined(SUSUWU_SH_OSC_UNSUPPORTED) && !defined(SUSUWU_SH_SKIP_OSC)
# define SUSUWU_SH_SKIP_OSC true /* you can use `-DSUSUWU_SH_SKIP_OSC=false` to force unsupported Operating System Command use (such as if build is __WIN32__ but you assume Win10+ `xterm` support) */
#endif /* SUSUWU_SH_OSC_UNSUPPORTED && !defined SUSUWU_SH_SKIP_OSC */
#ifdef SUSUWU_SH_SKIP_OSC /* `-DSUSUWU_SH_SKIP_OSC=true` to turn Operating System Commands off */
# define SUSUWU_SH_TO_CLIPBOARD_PREFIX ""
# define SUSUWU_SH_TO_CLIPBOARD_SUFFIX ""
# define SUSUWU_SH_TO_CLIPBOARD(base64_str) (true)/* skip */
#else /* def SUSUWU_SH_SKIP_OSC else */
# define SUSUWU_SH_TO_CLIPBOARD_PREFIX SUSUWU_SH_OSC "52;c;" /* Command to put BASE64 str into clipboard */
# define SUSUWU_SH_TO_CLIPBOARD_SUFFIX "\a"
# define SUSUWU_SH_TO_CLIPBOARD(base64_str) fprintf(stdout, SUSUWU_SH_TO_CLIPBOARD_PREFIX "%s" SUSUWU_SH_TO_CLIPBOARD_SUFFIX, IF_SUSUWU_CPLUSPLUS(std::string(base64_str).c_str(), base64_str)); /* TODO: `std::cout` version of this */
#endif /* def SUSUWU_SH_SKIP_OSC else */
#if defined(SUSUWU_SH_COLORS_UNSUPPORTED) && !defined(SUSUWU_SH_SKIP_COLORS)
# define SUSUWU_SH_SKIP_COLORS true /* you can use `-DSUSUWU_SH_SKIP_COLORS=false` to force unsupported color use (such as if build is for __WIN32__ but you assume Win10+ `xterm` support) */
#endif /* SUSUWU_SH_COLORS_UNSUPPORTED && !defined SUSUWU_SH_SKIP_COLORS */
# define SUSUWU_SH_BEL "\07" /* Bell sound */
# define SUSUWU_SH_ESC "\033" /* Escape */
# define SUSUWU_SH_OSC SUSUWU_SH_ESC "]" /* Operating System Command */
# define SUSUWU_SH_ST SUSUWU_SH_ESC "\\" /* String Terminator of commands */ /* `BEL` reduces this to 1 byte but just functions as `ST` on `xterm` */
# define SUSUWU_SH_CSI SUSUWU_SH_ESC "[" /* Control Sequence Introducer */
#if SUSUWU_SH_SKIP_COLORS /* `-DSUSUWU_SH_SKIP_COLORS=true` to turn colors off */
# define SUSUWU_SH_DEFAULT ""
# define SUSUWU_SH_BLACK ""
# define SUSUWU_SH_DARK_GRAY ""
# define SUSUWU_SH_RED ""
# define SUSUWU_SH_LIGHT_RED ""
# define SUSUWU_SH_GREEN ""
# define SUSUWU_SH_LIGHT_GREEN ""
# define SUSUWU_SH_BROWN ""
# define SUSUWU_SH_YELLOW ""
# define SUSUWU_SH_BLUE ""
# define SUSUWU_SH_LIGHT_BLUE ""
# define SUSUWU_SH_PURPLE ""
# define SUSUWU_SH_LIGHT_PURPLE ""
# define SUSUWU_SH_CYAN ""
# define SUSUWU_SH_LIGHT_CYAN ""
# define SUSUWU_SH_LIGHT_GRAY ""
# define SUSUWU_SH_WHITE ""
# define SUSUWU_SH_DEFAULT ""
# define SUSUWU_SH_BLACK ""
# define SUSUWU_SH_DARK_GRAY ""
# define SUSUWU_SH_RED ""
# define SUSUWU_SH_LIGHT_RED ""
# define SUSUWU_SH_GREEN ""
# define SUSUWU_SH_LIGHT_GREEN ""
# define SUSUWU_SH_BROWN ""
# define SUSUWU_SH_YELLOW ""
# define SUSUWU_SH_BLUE ""
# define SUSUWU_SH_LIGHT_BLUE ""
# define SUSUWU_SH_PURPLE ""
# define SUSUWU_SH_LIGHT_PURPLE ""
# define SUSUWU_SH_CYAN ""
# define SUSUWU_SH_LIGHT_CYAN ""
# define SUSUWU_SH_LIGHT_GRAY ""
# define SUSUWU_SH_WHITE ""
#else /* !SUSUWU_SH_SKIP_COLORS */
# define SUSUWU_SH_DEFAULT "\033[0m"
# define SUSUWU_SH_BLACK "\033[0;30m"
# define SUSUWU_SH_DARK_GRAY "\033[1;30m"
# define SUSUWU_SH_RED "\033[0;31m"
# define SUSUWU_SH_LIGHT_RED "\033[1;31m"
# define SUSUWU_SH_GREEN "\033[0;32m"
# define SUSUWU_SH_LIGHT_GREEN "\033[1;32m"
# define SUSUWU_SH_BROWN "\033[0;33m"
# define SUSUWU_SH_YELLOW "\033[1;33m"
# define SUSUWU_SH_BLUE "\033[0;34m"
# define SUSUWU_SH_LIGHT_BLUE "\033[1;34m"
# define SUSUWU_SH_PURPLE "\033[0;35m"
# define SUSUWU_SH_LIGHT_PURPLE "\033[1;35m"
# define SUSUWU_SH_CYAN "\033[0;36m"
# define SUSUWU_SH_LIGHT_CYAN "\033[1;36m"
# define SUSUWU_SH_LIGHT_GRAY "\033[0;37m"
# define SUSUWU_SH_WHITE "\033[1;37m"
# define SUSUWU_SH_DEFAULT SUSUWU_SH_CSI "0m"
# define SUSUWU_SH_BLACK SUSUWU_SH_CSI "0;30m"
# define SUSUWU_SH_DARK_GRAY SUSUWU_SH_CSI "1;30m"
# define SUSUWU_SH_RED SUSUWU_SH_CSI "0;31m"
# define SUSUWU_SH_LIGHT_RED SUSUWU_SH_CSI "1;31m"
# define SUSUWU_SH_GREEN SUSUWU_SH_CSI "0;32m"
# define SUSUWU_SH_LIGHT_GREEN SUSUWU_SH_CSI "1;32m"
# define SUSUWU_SH_BROWN SUSUWU_SH_CSI "0;33m"
# define SUSUWU_SH_YELLOW SUSUWU_SH_CSI "1;33m"
# define SUSUWU_SH_BLUE SUSUWU_SH_CSI "0;34m"
# define SUSUWU_SH_LIGHT_BLUE SUSUWU_SH_CSI "1;34m"
# define SUSUWU_SH_PURPLE SUSUWU_SH_CSI "0;35m"
# define SUSUWU_SH_LIGHT_PURPLE SUSUWU_SH_CSI "1;35m"
# define SUSUWU_SH_CYAN SUSUWU_SH_CSI "0;36m"
# define SUSUWU_SH_LIGHT_CYAN SUSUWU_SH_CSI "1;36m"
# define SUSUWU_SH_LIGHT_GRAY SUSUWU_SH_CSI "0;37m"
# define SUSUWU_SH_WHITE SUSUWU_SH_CSI "1;37m"
#endif /* !SUSUWU_SH_SKIP_COLORS */
#define SUSUWU_SH_FILE __FILE__ ":"
#define SUSUWU_SH_PREFIX IF_SUSUWU_SH_BRACKETS("[", "") SUSUWU_SH_WHITE
Expand All @@ -115,16 +157,10 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
#define SUSUWU_CERR(WARN_LEVEL, x) std::cerr << SUSUWU_SH_PREFIX IF_SUSUWU_SH_FILE(<< std::string(SUSUWU_SH_FILE)) IF_SUSUWU_SH_LINE(<< std::to_string(__LINE__) << ":") IF_SUSUWU_SH_FUNC(<< std::string(__func__) << ":") IF_SUSUWU_SH_FILE_LINE_OR_FUNC(<< ' ') << SUSUWU_CERR_IMP(WARN_LEVEL, x) << SUSUWU_SH_POSTFIX << std::endl
#define SUSUWU_STDERR(WARN_LEVEL, x) SUSUWU_STDERR_IMP(WARN_LEVEL, SUSUWU_SH_PREFIX IF_SUSUWU_SH_FILE(SUSUWU_SH_FILE) IF_SUSUWU_SH_LINE("%i:") IF_SUSUWU_SH_FUNC("%s:") IF_SUSUWU_SH_FILE_LINE_OR_FUNC(" "), SUSUWU_SH_POSTFIX "\n", x, IF_SUSUWU_SH_LINE(__LINE__ SUSUWU_COMMA) IF_SUSUWU_SH_FUNC(__func__ SUSUWU_COMMA))
/* Use this to do C versus C++ agnostic code */
#ifdef __cplusplus
# define IF_SUSUWU_CPLUSPLUS(TRUE, FALSE) TRUE
#else /* !(defined __cplusplus */
# define IF_SUSUWU_CPLUSPLUS(TRUE, FALSE) FALSE
# define SUSUWU_USE_STDERR
#endif /* !(defined __cplusplus */
#ifdef SUSUWU_USE_STDERR
# define SUSUWU_PRINT(LEVEL, x) SUSUWU_STDERR(LEVEL, x)
#ifdef SUSUWU_SH_PREFER_STDIO
# define SUSUWU_PRINT(LEVEL, x) SUSUWU_STDERR(LEVEL, x)
#else
# define SUSUWU_PRINT(LEVEL, x) SUSUWU_CERR(LEVEL, x)
# define SUSUWU_PRINT(LEVEL, x) SUSUWU_CERR(LEVEL, x)
#endif
#define SUSUWU_ERROR(x) SUSUWU_PRINT(ERROR, x)
#define SUSUWU_WARNING(x) SUSUWU_PRINT(WARNING, x)
Expand All @@ -133,13 +169,13 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice

/* Use this to limit notices/diagnostics to release builds (+ do conditional execution) */
#ifdef NDEBUG
# define SUSUWU_NOTICE(x) (true)/* skip */
# define SUSUWU_DEBUG(x) (true)/* skip */
# define SUSUWU_DEBUGEXECUTE(x) (true)/*skip*/
# define SUSUWU_NOTICE(x) (true)/* skip */
# define SUSUWU_DEBUG(x) (true)/* skip */
# define SUSUWU_DEBUGEXECUTE(x) (true)/*skip*/
#else /* !(defined NDEBUG) */
# define SUSUWU_NOTICE(x) SUSUWU_PRINT(NOTICE, x)
# define SUSUWU_DEBUG(x) SUSUWU_PRINT(DEBUG, x)
# define SUSUWU_DEBUGEXECUTE(x) x
# define SUSUWU_NOTICE(x) SUSUWU_PRINT(NOTICE, x)
# define SUSUWU_DEBUG(x) SUSUWU_PRINT(DEBUG, x)
# define SUSUWU_DEBUGEXECUTE(x) x
#endif /* !(defined NDEBUG) */

/* Use this to reduce (conditional) print + (unconditional) execute into single statement */
Expand All @@ -153,38 +189,39 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
#define SUSUWU_DEBUG_DEBUGEXECUTE(x) ((SUSUWU_DEBUG(#x)), SUSUWU_DEBUGEXECUTE(x))

#ifndef __has_feature
# define __has_feature(X) false /* `gcc` "error: missing binary operator before token \"(\"" fix */
# define __has_feature(X) false /* `gcc` "error: missing binary operator before token \"(\"" fix */
#endif /* ndef __has_feature */
#if (!defined _POSIX_VERSION) && (_POSIX_C_SOURCE)
# define _POSIX_VERSION _POSIX_C_SOURCE /* "Error: ... ndef _POSIX_VERSION" fix */
# define _POSIX_VERSION _POSIX_C_SOURCE /* "Error: ... ndef _POSIX_VERSION" fix */
#endif /* (!defined _POSIX_VERSION) && (_POSIX_C_SOURCE) */
#if (defined __cplusplus && 201102 < __cplusplus)
# define SUSUWU_CXX11
# define SUSUWU_CXX11
#endif /* if (defined __cplusplus && 201402 <= __cplusplus) */
#if (defined __cplusplus && 201402 <= __cplusplus)
# define SUSUWU_CXX14
# define SUSUWU_CXX14
#endif /* if (defined __cplusplus && 201402 < __cplusplus) */
#if (defined __cplusplus && 201702 < __cplusplus)
# define SUSUWU_CXX17
# define SUSUWU_CXX17
#endif /* if (defined __cplusplus && 201702 < __cplusplus) */
#if (defined __cplusplus && 202002 <= __cplusplus)
# define SUSUWU_CXX20
# define SUSUWU_CXX20
#endif /* if (defined __cplusplus && 202002 <= __cplusplus) */

#if (!defined __WIN32__) && (defined _WIN32 /* || defined __CYGWIN__ should use "#ifdef _POSIX_VERSION" path */ || __MSC_VER)
# define __WIN32__ /* https://stackoverflow.com/questions/430424/are-there-any-macros-to-determine-if-my-code-is-being-compiled-to-windows/430435#430435 says that __WIN32__ is not always defined on Windows targets */
# define __WIN32__ /* https://stackoverflow.com/questions/430424/are-there-any-macros-to-determine-if-my-code-is-being-compiled-to-windows/430435#430435 says that __WIN32__ is not always defined on Windows targets */
#endif

/* `UNREACHABLE` is close to `ASSUME(false)` */
#if (!defined NDEBUG) && (defined static_assert)
#if (!defined NDEBUG) && (defined(SUSUWU_CXX11))
/* [https://stackoverflow.com/questions/2249282/c-c-portable-way-to-detect-debug-release] [https://stackoverflow.com/questions/2290509/debug-vs-ndebug] */
/* Debug: Promises unreachable, for static analysis */
# define UNREACHABLE static_assert(false)
#elif (!defined NDEBUG)
# define UNREACHABLE assert(false)
#else
}; /* namespace Susuwu */
# include <version> /* __cpp_lib_unreachable */ /* [https://en.cppreference.com/w/cpp/feature_test] */
# if __cpp_lib_unreachable
# if defined(__cpp_lib_unreachable) && __cpp_lib_unreachable
/* Release: Promises executable can not reach this spot, for compiler which optimizes this. Warning: `UNREACHABLE && UB (undefined behaviour)` */
# include <utility> /* std::unreachable() */
# define UNREACHABLE std::unreachable()
Expand All @@ -193,6 +230,7 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
# else /* else (!def NDEBUG) && (!supports unreachable) */
# define UNREACHABLE /* No-op */
# endif /* __cpp_lib_unreachable elif IS_GCC ...*/
namespace Susuwu {
#endif /* #elif (!defined NDEBUG) ... #else */

#ifdef USE_CONTRACTS /* Pass `-DUSE_CONTRACTS` once compiler has C++26 (Contracts) */
Expand All @@ -209,7 +247,11 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
/* TODO: choose best of [various possible ASSUME macros](https://stackoverflow.com/questions/44054078/how-to-guide-gcc-optimizations-based-on-assertions-without-runtime-cost) */
#ifndef NDEBUG
/* Debug: Promises `(true == (X))`, for static analysis */
# define ASSUME(X) assert(X)
# ifdef SUSUWU_CXX11
# define ASSUME(X) static_assert(X)
# else /* def SUSUWU_CXX11 else */
# define ASSUME(X) assert(X)
# endif /* def SUSUWU_CXX11 else */
#elif (!defined USE_ASSUME) || USE_ASSUME /* Default: if(!NDEBUG) USE_ASSUME=true; pass `-DUSE_ASSUME=false` to disable this */
/* Release: Promises `(true == (X))`, for compiler which optimizes this. Warning: `if(!(X)) {UB (undefined behaviour)}` */
# ifdef IS_MSVC
Expand Down
Loading

0 comments on commit c97fa94

Please sign in to comment.