Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm][Support] Support bright colors in raw_ostream #80017

Merged
merged 1 commit into from
Jan 31, 2024

Conversation

tbaederr
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 30, 2024

@llvm/pr-subscribers-platform-windows

@llvm/pr-subscribers-llvm-support

Author: Timm Baeder (tbaederr)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/80017.diff

4 Files Affected:

  • (modified) llvm/include/llvm/Support/raw_ostream.h (+16)
  • (modified) llvm/lib/Support/Process.cpp (+27-12)
  • (modified) llvm/lib/Support/Unix/Process.inc (+1-1)
  • (modified) llvm/lib/Support/Windows/Process.inc (+1-1)
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 42663a9adf2e5..696290a5d99cf 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -102,6 +102,14 @@ class raw_ostream {
     MAGENTA,
     CYAN,
     WHITE,
+    BRIGHT_BLACK,
+    BRIGHT_RED,
+    BRIGHT_GREEN,
+    BRIGHT_YELLOW,
+    BRIGHT_BLUE,
+    BRIGHT_MAGENTA,
+    BRIGHT_CYAN,
+    BRIGHT_WHITE,
     SAVEDCOLOR,
     RESET,
   };
@@ -114,6 +122,14 @@ class raw_ostream {
   static constexpr Colors MAGENTA = Colors::MAGENTA;
   static constexpr Colors CYAN = Colors::CYAN;
   static constexpr Colors WHITE = Colors::WHITE;
+  static constexpr Colors BRIGHT_BLACK = Colors::BRIGHT_BLACK;
+  static constexpr Colors BRIGHT_RED = Colors::BRIGHT_RED;
+  static constexpr Colors BRIGHT_GREEN = Colors::BRIGHT_GREEN;
+  static constexpr Colors BRIGHT_YELLOW = Colors::BRIGHT_YELLOW;
+  static constexpr Colors BRIGHT_BLUE = Colors::BRIGHT_BLUE;
+  static constexpr Colors BRIGHT_MAGENTA = Colors::BRIGHT_MAGENTA;
+  static constexpr Colors BRIGHT_CYAN = Colors::BRIGHT_CYAN;
+  static constexpr Colors BRIGHT_WHITE = Colors::BRIGHT_WHITE;
   static constexpr Colors SAVEDCOLOR = Colors::SAVEDCOLOR;
   static constexpr Colors RESET = Colors::RESET;
 
diff --git a/llvm/lib/Support/Process.cpp b/llvm/lib/Support/Process.cpp
index f81c13970d521..f1ab8c7b9ebd8 100644
--- a/llvm/lib/Support/Process.cpp
+++ b/llvm/lib/Support/Process.cpp
@@ -70,20 +70,35 @@ Process::FindInEnvPath(StringRef EnvName, StringRef FileName,
 
 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
 
-#define ALLCOLORS(FGBG,BOLD) {\
-    COLOR(FGBG, "0", BOLD),\
-    COLOR(FGBG, "1", BOLD),\
-    COLOR(FGBG, "2", BOLD),\
-    COLOR(FGBG, "3", BOLD),\
-    COLOR(FGBG, "4", BOLD),\
-    COLOR(FGBG, "5", BOLD),\
-    COLOR(FGBG, "6", BOLD),\
-    COLOR(FGBG, "7", BOLD)\
+#define ALLCOLORS(FGBG, BRIGHT, BOLD) \
+  {                           \
+    COLOR(FGBG, "0", BOLD),   \
+    COLOR(FGBG, "1", BOLD),   \
+    COLOR(FGBG, "2", BOLD),   \
+    COLOR(FGBG, "3", BOLD),   \
+    COLOR(FGBG, "4", BOLD),   \
+    COLOR(FGBG, "5", BOLD),   \
+    COLOR(FGBG, "6", BOLD),   \
+    COLOR(FGBG, "7", BOLD),   \
+    COLOR(BRIGHT, "0", BOLD), \
+    COLOR(BRIGHT, "1", BOLD), \
+    COLOR(BRIGHT, "2", BOLD), \
+    COLOR(BRIGHT, "3", BOLD), \
+    COLOR(BRIGHT, "4", BOLD), \
+    COLOR(BRIGHT, "5", BOLD), \
+    COLOR(BRIGHT, "6", BOLD), \
+    COLOR(BRIGHT, "7", BOLD), \
   }
 
-static const char colorcodes[2][2][8][10] = {
- { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
- { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
+//                           bg
+//                           |  bold
+//                           |  |
+//                           |  |   codes
+//                           |  |   |
+//                           |  |   |
+static const char colorcodes[2][2][16][11] = {
+    { ALLCOLORS("3", "9", ""), ALLCOLORS("3", "9", "1;"),},
+    { ALLCOLORS("4", "10", ""), ALLCOLORS("4", "10", "1;")}
 };
 
 // A CMake option controls wheter we emit core dumps by default. An application
diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc
index 551f0d7f0f029..f94eec6963c18 100644
--- a/llvm/lib/Support/Unix/Process.inc
+++ b/llvm/lib/Support/Unix/Process.inc
@@ -417,7 +417,7 @@ bool Process::ColorNeedsFlush() {
 }
 
 const char *Process::OutputColor(char code, bool bold, bool bg) {
-  return colorcodes[bg ? 1 : 0][bold ? 1 : 0][code & 7];
+  return colorcodes[bg ? 1 : 0][bold ? 1 : 0][code & 15];
 }
 
 const char *Process::OutputBold(bool bg) { return "\033[1m"; }
diff --git a/llvm/lib/Support/Windows/Process.inc b/llvm/lib/Support/Windows/Process.inc
index a54c06d46870b..6b4b723d4744a 100644
--- a/llvm/lib/Support/Windows/Process.inc
+++ b/llvm/lib/Support/Windows/Process.inc
@@ -376,7 +376,7 @@ const char *Process::OutputBold(bool bg) {
 
 const char *Process::OutputColor(char code, bool bold, bool bg) {
   if (UseANSI)
-    return colorcodes[bg ? 1 : 0][bold ? 1 : 0][code & 7];
+    return colorcodes[bg ? 1 : 0][bold ? 1 : 0][code & 15];
 
   WORD current = DefaultColors::GetCurrentColor();
   WORD colors;

Copy link

github-actions bot commented Jan 30, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@tbaederr
Copy link
Contributor Author

I am aware the code is not formatted, because the output looks so bad.

@dwblaikie
Copy link
Collaborator

Is there any test coverage for the existing colours that can/should be extended for these?

I am aware the code is not formatted, because the output looks so bad.

There's format disabling comments you can put around code that is manually formatted for better readability.

@tbaederr
Copy link
Contributor Author

Is there any test coverage for the existing colours that can/should be extended for these?

I could't find any.

@tbaederr tbaederr merged commit 24a8041 into llvm:main Jan 31, 2024
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants