Cthulhu  0.2.10
Cthulhu compiler collection
notify.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "common_extra.h"
4 
5 #include "format/notify2.h"
6 
7 #include "io/io.h"
8 
9 #include "std/typed/vector.h"
10 #include "std/vector.h"
11 
12 #include "base/panic.h"
13 
14 typedef struct notify_config_t
15 {
18 
19 // TODO: all this
20 
21 #define COLOUR_PATH eColourBlue
22 
23 static void print_notify_segment_simple(notify_config_t *config, const segment_t *segment)
24 {
25  print_notify_t options = config->config;
26  print_options_t base = options.options;
27 
28  format_context_t format_context = format_context_make(base);
29 
30  source_config_t source_config = {
31  .context = format_context,
32  .colour = COLOUR_PATH,
33  .heading_style = options.heading,
35  };
36 
37  char *path = fmt_node_location(source_config, &segment->node);
38 
39  io_printf(base.io, "%s: %s\n", path, segment->message);
40 }
41 
42 static void print_notify_note_simple(notify_config_t *config, const char *path, const char *note)
43 {
44  print_notify_t options = config->config;
45  print_options_t base = options.options;
46 
47  io_printf(base.io, "%s note: %s\n", path, note);
48 }
49 
50 static void print_notify_simple(notify_config_t *config, const event_t *event)
51 {
52  print_notify_t options = config->config;
53  print_options_t base = options.options;
54 
55  format_context_t format_context = format_context_make(base);
56 
57  source_config_t source_config = {
58  .context = format_context,
59  .colour = COLOUR_PATH,
60  .heading_style = options.heading,
62  };
63 
64  const diagnostic_t *diagnostic = event->diagnostic;
65  severity_t severity = diagnostic->severity;
66  const char *id = diagnostic->id;
67 
68  const char *severity_name = get_severity_name(severity);
69  colour_t severity_colour = get_severity_colour(severity);
70 
71  char *path = fmt_node_location(source_config, &event->node);
72 
73  char *level = colour_format(format_context, severity_colour, "%s %s:", severity_name, id);
74 
75  io_printf(base.io, "%s: %s %s\n", path, level, event->message);
76 
77  if (event->segments != NULL)
78  {
79  size_t len = typevec_len(event->segments);
80  for (size_t i = 0; i < len; i++)
81  {
82  const segment_t *segment = typevec_offset(event->segments, i);
83  print_notify_segment_simple(config, segment);
84  }
85  }
86 
87  if (event->notes != NULL)
88  {
89  size_t len = vector_len(event->notes);
90  for (size_t i = 0; i < len; i++)
91  {
92  const char *note = vector_get(event->notes, i);
93  print_notify_note_simple(config, path, note);
94  }
95  }
96 }
97 
99 void print_notify(print_notify_t config, const event_t *event)
100 {
101  CTASSERT(event != NULL);
102 
103  notify_config_t notify_config = {
104  .config = config,
105  };
106 
107  print_notify_simple(&notify_config, event);
108 }
109 
110 STA_DECL
111 void print_notify_many(print_notify_t config, const typevec_t *events)
112 {
113  CTASSERT(events != NULL);
114 
115  size_t len = typevec_len(events);
116  for (size_t i = 0; i < len; i++)
117  {
118  const event_t *event = typevec_offset(events, i);
119  print_notify(config, event);
120  }
121 }
STA_DECL char * colour_format(format_context_t context, colour_t idx, const char *fmt,...)
Definition: colour.c:65
colour_t get_severity_colour(severity_t severity)
Definition: common_extra.c:52
const char * get_severity_name(severity_t severity)
Definition: common_extra.c:38
char * fmt_node_location(source_config_t config, const node_t *node)
Definition: common_extra.c:530
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
colour_t
a colour code
Definition: colour.h:23
STA_DECL void print_notify(print_notify_t config, const event_t *event)
format a single event for printing
Definition: notify.c:99
STA_DECL void print_notify_many(print_notify_t config, const typevec_t *events)
format many events for printing
Definition: notify.c:111
CT_IO_API size_t io_printf(io_t *io, STA_FORMAT_STRING const char *fmt,...)
printf to an io object
severity_t
the default severity of a diagnostic
Definition: diagnostic.h:18
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
CT_NODISCARD CT_PUREFN CT_STD_API size_t typevec_len(const typevec_t *vec)
get the length of a vector
Definition: vector.c:120
CT_NODISCARD CT_PUREFN CT_STD_API void * typevec_offset(const typevec_t *vec, size_t index)
get a pointer to the value at the given index
Definition: vector.c:191
CT_NODISCARD CT_PUREFN CT_STD_API void * vector_get(const vector_t *vector, size_t index)
get a value from a vector
Definition: vector.c:134
CT_NODISCARD CT_PUREFN CT_STD_API size_t vector_len(const vector_t *vector)
get the length of a vector
Definition: vector.c:152
a diagnostic
Definition: diagnostic.h:27
const char * id
the id of the diagnostic should be in the format [A-Z]{2,3}[0-9]{4} e.g. CLI0001 this is not enforced...
Definition: diagnostic.h:34
severity_t severity
the severity of the diagnostic
Definition: diagnostic.h:29
an event handle TODO: make this and segment_t opaque
Definition: notify.h:36
node_t node
the primary node that this event is attached to
Definition: notify.h:41
vector_t * notes
extra notes that this event is attached to
Definition: notify.h:52
STA_FIELD_STRING char * message
the primary message
Definition: notify.h:44
typevec_t * segments
extra segments that this event is attached to
Definition: notify.h:48
generic print options
Definition: format.h:35
io_t * io
io buffer
Definition: format.h:40
a formatting context when using colours
Definition: colour.h:46
print_notify_t config
Definition: notify.c:16
notification formatting options
Definition: notify2.h:38
print_options_t options
base print options
Definition: notify2.h:40
bool zero_indexed_lines
is the first line in a file 0 or 1
Definition: notify2.h:49
a segment inside an event
Definition: notify.h:57
node_t node
the related node
Definition: notify.h:59
STA_FIELD_STRING char * message
the message associated with this segment
Definition: notify.h:62
format_context_t context
Definition: common.h:31
A vector with a fixed type size.
Definition: vector.h:24
format_context_t format_context_make(print_options_t options)
Definition: common.c:114
#define COLOUR_PATH
Definition: notify.c:21