Skip to content

Commit 5d50139

Browse files
committed
fix(cli): avoid Windows ERROR macro collision
1 parent 54a15b5 commit 5d50139

6 files changed

Lines changed: 31 additions & 12 deletions

File tree

‎include/vix/cli/Style.hpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ namespace vix::cli::style
2525
inline constexpr const char *RESET = "\033[0m";
2626
inline constexpr const char *BOLD = "\033[1m";
2727
inline constexpr const char *UNDERLINE = "\033[4m";
28-
inline constexpr const char *ERROR = "\033[1;91m";
28+
// Do not name this ERROR: Windows headers define ERROR as a macro.
29+
inline constexpr const char *ERROR_TEXT = "\033[1;91m";
2930
inline constexpr const char *WARNING = "\033[1;93m";
3031
inline constexpr const char *SUCCESS = "\033[1;92m";
3132
inline constexpr const char *ACCENT = "\033[1;96m";
@@ -37,7 +38,7 @@ namespace vix::cli::style
3738

3839
// Compatibility aliases for existing callers. New diagnostic code should
3940
// prefer the semantic names above.
40-
inline constexpr const char *RED = ERROR;
41+
inline constexpr const char *RED = ERROR_TEXT;
4142
inline constexpr const char *GREEN = SUCCESS;
4243
inline constexpr const char *YELLOW = WARNING;
4344
inline constexpr const char *CYAN = ACCENT;
@@ -47,7 +48,7 @@ namespace vix::cli::style
4748

4849
inline void error(const std::string &msg)
4950
{
50-
std::cerr << PAD << ERROR << "✖ " << msg << RESET << "\n";
51+
std::cerr << PAD << ERROR_TEXT << "✖ " << msg << RESET << "\n";
5152
}
5253

5354
inline void success(const std::string &msg)

‎include/vix/cli/util/Ui.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace vix::cli::util
6767

6868
inline void err_line(std::ostream &os, std::string_view msg)
6969
{
70-
os << " " << ERROR << "✖" << RESET << " " << msg << "\n";
70+
os << " " << ERROR_TEXT << "✖" << RESET << " " << msg << "\n";
7171
}
7272

7373
inline std::string dim(std::string_view s)

‎src/build/BuildStyle.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace vix::cli::build
5454
const char *duration_color(long long milliseconds) noexcept
5555
{
5656
if (milliseconds >= 10000)
57-
return style::ERROR;
57+
return style::ERROR_TEXT;
5858
if (milliseconds >= 3000)
5959
return style::WARNING;
6060
return style::SUCCESS;
@@ -107,7 +107,7 @@ namespace vix::cli::build
107107
static std::string label_color(const std::string &label)
108108
{
109109
if (label == "error:")
110-
return style::ERROR;
110+
return style::ERROR_TEXT;
111111

112112
if (label == "hint:")
113113
return style::WARNING;
@@ -410,7 +410,7 @@ namespace vix::cli::build
410410
case BuildMessageKind::Warning:
411411
return colorize(style::WARNING, "!");
412412
case BuildMessageKind::Error:
413-
return colorize(style::ERROR, "✖");
413+
return colorize(style::ERROR_TEXT, "✖");
414414
default:
415415
return "•";
416416
}
@@ -534,7 +534,7 @@ namespace vix::cli::build
534534
: diagnostic.title;
535535

536536
out << " "
537-
<< style::ERROR
537+
<< style::ERROR_TEXT
538538
<< style::BOLD
539539
<< "✖ "
540540
<< title
@@ -581,7 +581,7 @@ namespace vix::cli::build
581581
if (!pointer.empty())
582582
{
583583
out << " | "
584-
<< colorize(style::ERROR, pointer)
584+
<< colorize(style::ERROR_TEXT, pointer)
585585
<< "\n";
586586
}
587587
}
@@ -738,7 +738,7 @@ namespace vix::cli::build
738738
<< (static_cast<double>(milliseconds) / 1000.0);
739739

740740
out << " "
741-
<< colorize(style::ERROR, "✖")
741+
<< colorize(style::ERROR_TEXT, "✖")
742742
<< " "
743743
<< message
744744
<< " after "

‎src/errors/CodeFrame.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ namespace vix::cli::errors
270270
caret,
271271
opt.maxLineWidth,
272272
prefixPrint,
273-
ERROR,
273+
ERROR_TEXT,
274274
CODE,
275-
ERROR,
275+
ERROR_TEXT,
276276
RESET,
277277
caretPrefix);
278278
}

‎tests/CMakeLists.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ target_include_directories(vix_cli_code_frame_style_tests PRIVATE
5454
)
5555
add_test(NAME vix_cli_code_frame_style_tests COMMAND vix_cli_code_frame_style_tests)
5656

57+
add_executable(vix_cli_style_windows_macro_compile_test StyleWindowsMacroCompileTest.cpp)
58+
target_include_directories(vix_cli_style_windows_macro_compile_test PRIVATE
59+
${CMAKE_CURRENT_SOURCE_DIR}/../include
60+
)
61+
add_test(NAME vix_cli_style_windows_macro_compile_test
62+
COMMAND vix_cli_style_windows_macro_compile_test
63+
)
64+
5765
add_executable(vix_cli_clang_gcc_parser_tests
5866
ClangGccParserTests.cpp
5967
../src/errors/ClangGccParser.cpp
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Windows headers define ERROR as an object-like macro. Keep this simulation
2+
// platform-neutral so the public style header is checked on every platform.
3+
#define ERROR 0
4+
#include <vix/cli/Style.hpp>
5+
6+
int main()
7+
{
8+
static_assert(vix::cli::style::ERROR_TEXT[0] == '\033');
9+
return 0;
10+
}

0 commit comments

Comments
 (0)