Skip to content
GitLab
  • Menu
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • sac2c sac2c
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 396
    • Issues 396
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 12
    • Merge requests 12
  • Deployments
    • Deployments
    • Releases
  • Wiki
    • Wiki
  • External wiki
    • External wiki
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • sac-group
  • sac2csac2c
  • Merge requests
  • !439

Add runtime colors

  • Review changes

  • Download
  • Email patches
  • Plain diff
Open Quinten Cabo requested to merge runtime-colors into develop Oct 10, 2025
  • Overview 0
  • Commits 5
  • Changes 5

This mr adds support for colors into the runtime of SaC programs. With runtime I am talking about the c files inside like those in src/libsac/essentials that are included into SaC binaries.

How does this work?

I want to follow the same design as the static colors for static messages (see !399 (merged) and !409 (merged)) and make this experience available to the runtime.

After merging this mr, the runtime code of SaC can do this:

// essentials/somefile.c
#include "color.h"
printf("%This text is red%s", COLOR_NOTE, COLOR_RESET);

You can use the COLOR_ macros to format your strings. These macros expand into a pointer into the global SAC_COLORS array. The values of this array are strings that hold the magic values that, when printed, add color to terminals.

The available colors are the same as the values of static diagnostic message colors during compile time, These static colors are found in src/libsac2c/support/color.c. In fact, the colors you use when compiling your program will be baked into the compiled program.

Design Considerations:

The easiest solution would be to just provide SAC_COLORS array of type const char *const with hardcoded colors. Basically, hard code the colors. But what makes color challenging is that it might not be supported, or unwanted, or wanted even if unsupported. That means that before using a color somewhere, the "are colors wanted" question has to be answered. We could leave this to the runtime programmers (something like SAC_COLOR_WANTED() ? COLOR_RED : "") but that makes using colors annoying and also slower by adding conditionals. No, instead we should check if colors are wanted once at the start of the program and assume the answer to this question does not change during the computation.

To this end gen_startup_code.c adds the following code to the main function that:

  1. uses the SAC_COLOR_WANTED() function from the new essentials/color.h to figure out if colors are wanted.
  2. if SAC_COLOR_WANTED() returns true, then the strings in SAC_COLORS are set to the values of the static COLORS array from compile time (from sac2c/src/libsac2c/support/color.h) one by one.

The code added to main looks like this:

     if (SAC_COLOR_WANTED()){
         if (%lu < SAC_COLOR_COUNT) COLORS[%lu] = %s;
     }

One of these if (%lu < SAC_COLOR_COUNT) COLORS[%lu] = %s;\n lines is generated for each static color. Where the %lu is the current color index and %s the color value from compile time.

Setting the SAC_COLORS at runtime from the compile time COLORS has cool benefits. Namely, that if you use custom colors to compile your program, the compiled program will also use these colors. It also means that if you compile with -cti-no-colors that the colors are also disabled at runtime, because even if supported, the SAC_COLORS is just set to empty strings because the compile time colors are set to empty strings.

The generated code inside the main function also checks if there is enough room in the runtime using (SAC_COLOR_COUNT) to to store each static color from compile time.

(I made this for the stack trace system but I am commiting it seperatly)

Edited Oct 13, 2025 by Quinten Cabo
Assignee
Assign to
Reviewer
Request review from
Time tracking
Source branch: runtime-colors