• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nickg / nvc / 27743617187

18 Jun 2026 07:23AM UTC coverage: 92.288% (-0.001%) from 92.289%
27743617187

push

github

nickg
Do not expand `include inside false `ifdef

7 of 7 new or added lines in 1 file covered. (100.0%)

137 existing lines in 6 files now uncovered.

79213 of 85832 relevant lines covered (92.29%)

631325.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.54
/src/printf.c
1
//
2
//  Copyright (C) 2025-2026  Nick Gasson
3
//
4
//  This program is free software: you can redistribute it and/or modify
5
//  it under the terms of the GNU General Public License as published by
6
//  the Free Software Foundation, either version 3 of the License, or
7
//  (at your option) any later version.
8
//
9
//  This program is distributed in the hope that it will be useful,
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
//  GNU General Public License for more details.
13
//
14
//  You should have received a copy of the GNU General Public License
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

18
#include "util.h"
19
#include "ident.h"
20
#include "mir/mir-node.h"
21
#include "object.h"
22
#include "printf.h"
23
#include "thread.h"
24
#include "type.h"
25

26
#include <assert.h>
27
#include <errno.h>
28
#include <stdio.h>
29
#include <string.h>
30
#include <stdlib.h>
31
#include <unistd.h>
32
#include <limits.h>
33

34
#define ANSI_RESET      0
35
#define ANSI_BOLD       1
36
#define ANSI_FG_BLACK   30
37
#define ANSI_FG_RED     31
38
#define ANSI_FG_GREEN   32
39
#define ANSI_FG_YELLOW  33
40
#define ANSI_FG_BLUE    34
41
#define ANSI_FG_MAGENTA 35
42
#define ANSI_FG_CYAN    36
43
#define ANSI_FG_WHITE   37
44

45
#define MAX_ARGS 25
46

47
typedef struct _printf_state printf_state_t;
48
typedef struct _printf_arg printf_arg_t;
49

50
typedef union {
51
   long long  ll;
52
   long       l;
53
   size_t     z;
54
   double     f;
55
   int        i;
56
   void      *p;
57
} printf_value_t;
58

59
typedef int (*fmt_fn_t)(ostream_t *, printf_state_t *, printf_arg_t *);
60

61
typedef struct _printf_arg {
62
   const char     *start;
63
   size_t          len;
64
   fmt_fn_t        fn;
65
   printf_value_t  value;
66
   int             width;
67
   int             precision;
68
} printf_arg_t;
69

70
typedef struct _printf_state {
71
   printf_arg_t args[MAX_ARGS];
72
   unsigned     nargs;
73
   unsigned     pos;
74
} printf_state_t;
75

76
typedef struct {
77
   const char *name;
78
   int         value;
79
} color_escape_t;
80

81
static const color_escape_t escapes[] = {
82
   { "",        ANSI_RESET },
83
   { "bold",    ANSI_BOLD },
84
   { "black",   ANSI_FG_BLACK },
85
   { "red",     ANSI_FG_RED },
86
   { "green",   ANSI_FG_GREEN },
87
   { "yellow",  ANSI_FG_YELLOW },
88
   { "blue",    ANSI_FG_BLUE },
89
   { "magenta", ANSI_FG_MAGENTA },
90
   { "cyan",    ANSI_FG_CYAN },
91
   { "white",   ANSI_FG_WHITE },
92
};
93

94
static int printf_interpret(ostream_t *os, printf_state_t *state,
95
                            const char *fmt, const char *end);
96

97
int ostream_write(ostream_t *os, const char *buf, size_t len)
2,605,004✔
98
{
99
   (*os->callback)(buf, len, os->context);
2,605,004✔
100
   return len;
2,605,004✔
101
}
102

103
int ostream_putc(ostream_t *os, char ch)
816,447✔
104
{
105
   char buf[1] = { ch };
816,447✔
106
   return ostream_write(os, buf, 1);
816,447✔
107
}
108

109
int ostream_puts(ostream_t *os, const char *str)
24,103✔
110
{
111
   if (str == NULL)
24,103✔
UNCOV
112
      return ostream_write(os, "(null)", 6);
×
113
   else
114
      return ostream_write(os, str, strlen(str));
24,103✔
115
}
116

117
static int format_ident(ostream_t *os, printf_state_t *state, printf_arg_t *arg)
413✔
118
{
119
   ident_t id = arg->value.p;
413✔
120
   return ostream_write(os, istr(id), ident_len(id));
413✔
121
}
122

123
static int format_ident_toupper(ostream_t *os, printf_state_t *state,
230✔
124
                                printf_arg_t *arg)
125
{
126
   ident_t id = arg->value.p;
230✔
127
   size_t len = ident_len(id);
230✔
128
   const char *str = istr(id);
230✔
129
   int nchars = 0;
230✔
130
   char chunk[32];
230✔
131

132
   for (int i = 0; i < len; i += sizeof(chunk)) {
460✔
133
      const int tocopy = MIN(sizeof(chunk), len - i);
230✔
134
      for (int j = 0; j < tocopy; j++)
1,100✔
135
         chunk[j] = toupper_iso88591(str[i + j]);
870✔
136

137
      nchars += ostream_write(os, chunk, tocopy);
230✔
138
   }
139

140
   return nchars;
230✔
141
}
142

143
static int format_type(ostream_t *os, printf_state_t *state, printf_arg_t *arg)
228✔
144
{
145
   type_t type = arg->value.p;
228✔
146

147
   // Print a fully qualified name if there is another type in the
148
   // argument list with the same simple name
149

150
   for (int i = 0; i < state->nargs; i++) {
443✔
151
      const printf_arg_t *other = &(state->args[i]);
353✔
152
      if (other != arg && other->fn == format_type)
353✔
153
         return ostream_puts(os, type_pp2(type, other->value.p));
138✔
154
   }
155

156
   return ostream_puts(os, type_pp(type));
90✔
157
}
158

UNCOV
159
static int format_object_kind(ostream_t *os, printf_state_t *state,
×
160
                              printf_arg_t *arg)
161
{
162
   object_t *obj = arg->value.p;
×
UNCOV
163
   return ostream_puts(os, object_kind_str(obj));
×
164
}
165

166
static int format_mir(ostream_t *os, printf_state_t *state, printf_arg_t *arg)
16✔
167
{
168
   mir_value_t *value = arg->value.p;
16✔
169
   char buf[64];
16✔
170

171
   switch (value->tag) {
16✔
172
   case MIR_TAG_NODE:
4✔
173
      checked_sprintf(buf, sizeof(buf), "%%%d", value->id);
4✔
174
      break;
4✔
175
   case MIR_TAG_CONST:
12✔
176
      checked_sprintf(buf, sizeof(buf), "#%d",
12✔
177
                      value->id - (1 << (_MIR_ID_BITS - 1)));
12✔
178
      break;
12✔
179
   default:
×
180
      checked_sprintf(buf, sizeof(buf), "{tag:%d, id:%x}",
×
181
                      value->tag, value->id);
×
UNCOV
182
      break;
×
183
   }
184

185
   return ostream_puts(os, buf);
16✔
186
}
187

188
static int delegate(ostream_t *os, printf_state_t *s, printf_arg_t *arg, ...)
870,370✔
189
{
190
   char spec[32];
870,370✔
191
   assert(arg->len + 1 < sizeof(spec));
870,370✔
192
   memcpy(spec, arg->start, arg->len);
870,370✔
193
   spec[arg->len] = '\0';
870,370✔
194

195
   va_list ap, ap2;
870,370✔
196
   va_start(ap, arg);
870,370✔
197
   va_copy(ap2, ap);
870,370✔
198

199
   char small[64];
870,370✔
200
   int req = vsnprintf(small, sizeof(small), spec, ap);
870,370✔
201

202
   if (req + 1 > sizeof(small)) {
870,370✔
203
      char *large = xmalloc(req + 1);
872✔
204
      vsnprintf(large, req + 1, spec, ap2);
872✔
205
      ostream_write(os, large, req);
872✔
206
      free(large);
872✔
207
   }
208
   else
209
      ostream_write(os, small, req);
869,498✔
210

211
   va_end(ap);
870,370✔
212
   va_end(ap2);
870,370✔
213
   return req;
870,370✔
214
}
215

216
static fmt_fn_t get_pointer_formatter(char ch)
892✔
217
{
218
   switch (ch) {
892✔
219
   case 'i': return format_ident;
220
   case 'I': return format_ident_toupper;
230✔
221
   case 'T': return format_type;
228✔
UNCOV
222
   case 'K': return format_object_kind;
×
223
   case 'M': return format_mir;
16✔
224
   default: return NULL;
5✔
225
   }
226
}
227

228
static int ansi_escape(ostream_t *os, printf_state_t *state, const char **fmt)
205,819✔
229
{
230
   bool has_format = false;
205,819✔
231
   const char *start = *fmt;
205,819✔
232
   do {
1,083,460✔
233
      has_format |= (**fmt == '%');
1,083,460✔
234
      (*fmt)++;
1,083,460✔
235
   } while (**fmt != '\0' && **fmt != '$');
1,083,460✔
236

237
   if (**fmt == '\0')
205,819✔
238
      return ostream_write(os, start, *fmt - start);
181✔
239

240
   const char *end = *fmt;
205,638✔
241
   (*fmt)++;   // Advance past final '$'
205,638✔
242

243
   size_t len = len = *fmt - start - 2;
205,638✔
244
   const char *e = e = start + 1;
205,638✔
245
   LOCAL_TEXT_BUF tb = NULL;
411,276✔
246

247
   if (has_format) {
205,638✔
248
      // Expand any embedded formatting inside the ANSI escape
249
      tb = tb_new();
22,273✔
250

251
      ostream_t aos = {
22,273✔
252
         tb_ostream_write,
253
         tb,
254
         os->charset,
22,273✔
255
      };
256

257
      printf_interpret(&aos, state, start + 1, end);
22,273✔
258

259
      e = tb_get(tb);
22,273✔
260
      len = tb_len(tb);
22,273✔
261
   }
262

263
   bool bold;
205,638✔
264
   if ((bold = (*e == '!')))
205,638✔
265
      ++e, --len;
2✔
266

267
   bool bright;
205,638✔
268
   if ((bright = (*e == '+')))
205,638✔
269
      ++e, --len;
39✔
270

271
   if (*e == '#') {
205,638✔
272
      char *eptr;
1✔
273
      int code = strtoul(e + 1, &eptr, 10);
1✔
274
      if (eptr == e + len) {
1✔
275
         char buf[16];
1✔
276
         if (bold)
1✔
UNCOV
277
            checked_sprintf(buf, sizeof(buf), "\033[1;38;5;%dm", code);
×
278
         else
279
            checked_sprintf(buf, sizeof(buf), "\033[38;5;%dm", code);
1✔
280

281
         if (os->flags & OS_COLOR)
1✔
282
            ostream_puts(os, buf);
1✔
283

284
         return 0;
1✔
285
      }
286
   }
287

288
   if (strncmp(e, "link:", 5) == 0) {
205,637✔
289
      const char *bel = strchr(e, '\07');
22,274✔
290

291
#ifndef __MINGW32__    // Winpty doesn't recognise these
292
      if (os->flags & OS_TERMINAL) {
22,274✔
293
         ostream_puts(os, "\033]8;;");
2✔
294
         ostream_write(os, e + 5, len - 5);
2✔
295
         ostream_puts(os, "\033]8;;\07");
2✔
296
         return e + len - bel - 1;
2✔
297
      }
298
#endif
299

300
      return ostream_write(os, bel + 1, e + len - bel - 1);
22,272✔
301
   }
302

303
   for (int i = 0; i < ARRAY_LEN(escapes); i++) {
693,176✔
304
      if (strncmp(e, escapes[i].name, len) == 0) {
693,174✔
305
         int code = escapes[i].value + (bright ? 60 : 0);
183,361✔
306
         char buf[16];
183,361✔
307
         if (bold)
183,361✔
308
            checked_sprintf(buf, sizeof(buf), "\033[1;%dm", code);
1✔
309
         else
310
            checked_sprintf(buf, sizeof(buf), "\033[%dm", code);
183,360✔
311

312
         if (os->flags & OS_COLOR)
183,361✔
313
            ostream_puts(os, buf);
1,839✔
314

315
         return 0;
183,361✔
316
      }
317
   }
318

319
   return ostream_write(os, start, *fmt - start);
2✔
320
}
321

322
static int format_z(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
159,865✔
323
{
324
   assert(arg->precision == INT_MIN);
159,865✔
325
   return delegate(os, s, arg, arg->value.z);
159,865✔
326
}
327

UNCOV
328
static int format_ll(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
×
329
{
330
   if (arg->precision != INT_MIN)
×
UNCOV
331
      return delegate(os, s, arg, arg->precision, arg->value.ll);
×
332
   else
UNCOV
333
      return delegate(os, s, arg, arg->value.ll);
×
334
}
335

336
static int format_l(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
45,564✔
337
{
338
   if (arg->precision != INT_MIN)
45,564✔
339
      return delegate(os, s, arg, arg->precision, arg->value.l);
11,181✔
340
   else
341
      return delegate(os, s, arg, arg->value.l);
34,383✔
342
}
343

344
static int format_i(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
127,394✔
345
{
346
   if (arg->precision != INT_MIN)
127,394✔
347
      return delegate(os, s, arg, arg->precision, arg->value.i);
13,699✔
348
   else
349
      return delegate(os, s, arg, arg->value.i);
113,695✔
350
}
351

352
static int format_f(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
1,011✔
353
{
354
   if (arg->width != INT_MIN)
1,011✔
355
      return delegate(os, s, arg, arg->width, arg->precision, arg->value.f);
77✔
356
   else if (arg->precision != INT_MIN)
934✔
357
      return delegate(os, s, arg, arg->precision, arg->value.f);
9✔
358
   else
359
      return delegate(os, s, arg, arg->value.f);
925✔
360
}
361

362
static int format_s(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
536,531✔
363
{
364
   if (arg->width != INT_MIN)
536,531✔
365
      return delegate(os, s, arg, arg->width, arg->precision, arg->value.p);
1✔
366
   else if (arg->precision != INT_MIN)
536,530✔
367
      return delegate(os, s, arg, arg->precision, arg->value.p);
56,744✔
368
   else
369
      return delegate(os, s, arg, arg->value.p);
479,786✔
370
}
371

372
static int format_p(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
5✔
373
{
374
   return delegate(os, s, arg, arg->value.p);
5✔
375
}
376

377
static int printf_interpret(ostream_t *os, printf_state_t *state,
686,320✔
378
                            const char *fmt, const char *end)
379
{
380
   int nchars = 0;
686,320✔
381
   for (const char *p = fmt;;) {
686,320✔
382
      const char *start = p;
1,811,071✔
383
      while (p < end && *p != '%' && *p != '$')
4,932,976✔
384
         p++;
3,121,905✔
385

386
      if (start < p)
1,811,071✔
387
         nchars += ostream_write(os, start, p - start);
773,407✔
388

389
      if (p == end)
1,811,071✔
390
         return nchars;
686,320✔
391
      else if (*p == '$') {
1,124,751✔
392
         nchars += ansi_escape(os, state, &p);
205,819✔
393
         continue;
205,819✔
394
      }
395
      else if (*p == '%' && *(p + 1) == '%') {
918,932✔
396
         nchars += ostream_write(os, "%", 1);
47,675✔
397
         p += 2;
47,675✔
398
         continue;
47,675✔
399
      }
400

401
      assert(state->pos < state->nargs);
871,257✔
402

403
      printf_arg_t *arg = &(state->args[state->pos]);
871,257✔
404
      nchars += (*arg->fn)(os, state, arg);
871,257✔
405
      p += arg->len;
871,257✔
406

407
      state->pos++;
871,257✔
408
   }
409
}
410

411
int nvc_vfprintf(ostream_t *os, const char *fmt, va_list ap)
664,047✔
412
{
413
   printf_state_t state = {};
664,047✔
414

415
   const char *end = NULL;
664,047✔
416
   for (const char *p = fmt;;) {
664,047✔
417
      while (*p != '\0' && *p != '%')
5,481,716✔
418
         p++;
3,898,737✔
419

420
      if (*p == '\0') {
1,582,979✔
421
         end = p;
664,047✔
422
         break;
664,047✔
423
      }
424

425
      printf_arg_t arg = {
918,932✔
426
         .start = p,
427
         .width = INT_MIN,
428
         .precision = INT_MIN,
429
      };
430
      bool z_mod = false;
918,932✔
431
      int l_mod = 0;
918,932✔
432
   again:
1,233,728✔
433
      switch (*++p) {
1,233,728✔
434
      case 'l':
45,566✔
435
         l_mod++;
45,566✔
436
         goto again;
45,566✔
437
      case 'z':
159,865✔
438
         z_mod = true;
159,865✔
439
         goto again;
159,865✔
440
      case '-':
27,576✔
441
      case '+':
442
      case '.':
443
      case '0'...'9':
444
         goto again;
27,576✔
445
      case '*':
81,789✔
446
         arg.width = arg.precision;
81,789✔
447
         arg.precision = va_arg(ap, int);
81,789✔
448
         goto again;
81,789✔
449
      case 'd':
331,341✔
450
      case 'i':
451
      case 'x':
452
      case 'u':
453
      case 'o':
454
         if (z_mod) {
331,341✔
455
            arg.value.z = va_arg(ap, size_t);
159,865✔
456
            arg.fn = format_z;
159,865✔
457
         }
458
         else if (l_mod >= 2) {
171,476✔
459
            arg.value.ll = va_arg(ap, long long);
×
UNCOV
460
            arg.fn = format_ll;
×
461
         }
462
         else if (l_mod == 1) {
171,476✔
463
            arg.value.l = va_arg(ap, long);
45,564✔
464
            arg.fn = format_l;
45,564✔
465
         }
466
         else {
467
            arg.value.i = va_arg(ap, int);
125,912✔
468
            arg.fn = format_i;
125,912✔
469
         }
470
         break;
471
      case 'c':
1,482✔
472
         arg.value.i = va_arg(ap, int);
1,482✔
473
         arg.fn = format_i;
1,482✔
474
         break;
1,482✔
475
      case 'e':
1,011✔
476
      case 'f':
477
      case 'g':
478
         arg.value.f = va_arg(ap, double);
1,011✔
479
         arg.fn = format_f;
1,011✔
480
         break;
1,011✔
481
      case 's':
536,531✔
482
         arg.value.p = va_arg(ap, char *);
536,531✔
483
         arg.fn = format_s;
536,531✔
484
         break;
536,531✔
485
      case 'p':
892✔
486
         arg.value.p = va_arg(ap, void *);
892✔
487
         if ((arg.fn = get_pointer_formatter(p[1])))
892✔
488
            p++;
887✔
489
         else
490
            arg.fn = format_p;
491
         break;
492
      case '%':
47,675✔
493
         p++;
47,675✔
494
         continue;
47,675✔
UNCOV
495
      default:
×
496
         fatal_trace("unhandled character '%c' in format", *p);
497
      }
498

499
      arg.len = ++p - arg.start;
871,257✔
500

501
      if (state.nargs == MAX_ARGS)
871,257✔
502
         fatal_trace("maximum of %d printf arguments supported", MAX_ARGS);
503
      else
504
         state.args[state.nargs++] = arg;
871,257✔
505
   }
506

507
   return printf_interpret(os, &state, fmt, end);
1,328,094✔
508
}
509

510
int nvc_vprintf(const char *fmt, va_list ap)
8✔
511
{
512
   return nvc_vfprintf(nvc_stdout(), fmt, ap);
8✔
513
}
514

515
int nvc_printf(const char *fmt, ...)
8✔
516
{
517
   va_list ap;
8✔
518
   va_start(ap, fmt);
8✔
519
   const int nchars = nvc_vprintf(fmt, ap);
8✔
520
   va_end(ap);
8✔
521
   return nchars;
8✔
522
}
523

524
int nvc_fprintf(ostream_t *os, const char *fmt, ...)
180,106✔
525
{
526
   va_list ap;
180,106✔
527
   va_start(ap, fmt);
180,106✔
528
   const int nchars = nvc_vfprintf(os, fmt, ap);
180,106✔
529
   va_end(ap);
180,106✔
530
   return nchars;
180,106✔
531
}
532

533
void stdio_ostream_write(const char *buf, size_t len, void *ctx)
1,222,583✔
534
{
535
   FILE *f = ctx;
1,222,583✔
536
   fwrite(buf, len, 1, f);
1,222,583✔
537
}
1,222,583✔
538

539
static void init_terminal_ostream(ostream_t *os, FILE *f)
7,420✔
540
{
541
   os->callback = stdio_ostream_write;
7,420✔
542
   os->context = f;
7,420✔
543
   os->flags = 0;
7,420✔
544

545
   const int saved_errno = errno;
7,420✔
546

547
   if (isatty(fileno(f))) {
7,420✔
UNCOV
548
      os->charset = utf8_terminal() ? CHARSET_UTF8 : CHARSET_ISO88591;
×
UNCOV
549
      os->flags |= OS_TERMINAL;
×
550
   }
551
   else
552
      os->charset = CHARSET_ISO88591;
7,420✔
553

554
   if (color_terminal())
7,420✔
555
      os->flags |= OS_COLOR;
4✔
556

557
   errno = saved_errno;
7,420✔
558
}
7,420✔
559

560
ostream_t *nvc_stdout(void)
6,810✔
561
{
562
   static ostream_t os;
6,810✔
563
   INIT_ONCE(init_terminal_ostream(&os, stdout));
6,810✔
564
   return &os;
6,810✔
565
}
566

567
ostream_t *nvc_stderr(void)
74,360✔
568
{
569
   static ostream_t os;
74,360✔
570
   INIT_ONCE(init_terminal_ostream(&os, stderr));
74,360✔
571
   return &os;
74,360✔
572
}
573

574
char *color_asprintf(const char *fmt, ...)
31✔
575
{
576
   LOCAL_TEXT_BUF tb = tb_new();
62✔
577
   ostream_t os = { tb_ostream_write, tb, CHARSET_ISO88591, OS_COLOR };
31✔
578

579
   va_list ap;
31✔
580
   va_start(ap, fmt);
31✔
581
   nvc_vfprintf(&os, fmt, ap);
31✔
582
   va_end(ap);
31✔
583

584
   return tb_claim(tb);
31✔
585
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc