MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / ti_read_macro

Function ti_read_macro

src/mcp/test_impact_source.c:796–857  ·  view source on GitHub ↗

One `#define` starting at *line; *line ends on its last continuation line. * The body is the text after the name (and '('), then each continuation * line, joined with one blank. */

Source from the content-addressed store, hash-verified

794 * The body is the text after the name (and '('), then each continuation
795 * line, joined with one blank. */
796static bool ti_read_macro(cbm_ti_source_t *s, int file, int *line, cbm_ti_span_t name,
797 size_t body) {
798 size_t len = 0;
799 const char *text = cbm_ti_source_line(s, file, *line, &len);
800 size_t cap = len - body + 1;
801 int last = *line;
802 const char *tail = text + body;
803 size_t tail_len = len - body;
804 while (ti_line_continues(tail, tail_len) && last < s->files[file].line_count) {
805 last++;
806 tail = cbm_ti_source_line(s, file, last, &tail_len);
807 cap += tail_len + 1;
808 }
809 char *joined = cbm_arena_alloc(&s->arena, cap + 1);
810 char *macro_name = cbm_arena_strndup(&s->arena, text + name.start, name.len);
811 if (!joined || !macro_name) {
812 return false;
813 }
814 size_t used = len - body;
815 memcpy(joined, text + body, used);
816 for (int l = *line + 1; l <= last; l++) {
817 const char *more = cbm_ti_source_line(s, file, l, &tail_len);
818 joined[used++] = ' ';
819 memcpy(joined + used, more, tail_len);
820 used += tail_len;
821 }
822 joined[used] = '\0';
823 ti_strs_t tokens = {0};
824 ti_strs_t pre = {0};
825 ti_strs_t suf = {0};
826 bool ok = ti_scan_tokens(&s->arena, joined, used, macro_name, &tokens) &&
827 ti_scan_pastes(&s->arena, joined, used, &pre, &suf) &&
828 ti_grow((void **)&s->macros, &s->macro_cap, s->macro_count, sizeof(*s->macros));
829 if (ok && tokens.count > 1) {
830 qsort(tokens.items, (size_t)tokens.count, sizeof(*tokens.items), ti_str_compare);
831 }
832 const char **kept[3] = {NULL, NULL, NULL};
833 ti_strs_t *lists[3] = {&tokens, &pre, &suf};
834 for (int k = 0; ok && k < 3; k++) {
835 size_t bytes = (size_t)(lists[k]->count ? lists[k]->count : 1) * sizeof(char *);
836 kept[k] = cbm_arena_alloc(&s->arena, bytes);
837 ok = kept[k] != NULL;
838 if (ok && lists[k]->count) {
839 memcpy(kept[k], lists[k]->items, (size_t)lists[k]->count * sizeof(char *));
840 }
841 }
842 if (ok) {
843 s->macros[s->macro_count++] = (cbm_ti_macro_t){.name = macro_name,
844 .file = file,
845 .tokens = kept[0],
846 .token_count = tokens.count,
847 .paste_prefixes = kept[1],
848 .paste_prefix_count = pre.count,
849 .paste_suffixes = kept[2],
850 .paste_suffix_count = suf.count};
851 }
852 for (int k = 0; k < 3; k++) {
853 cbm_free(CBM_MEM_CLASS_EXTRACT, lists[k]->items);

Callers 1

ti_build_macrosFunction · 0.85

Calls 8

cbm_ti_source_lineFunction · 0.85
ti_line_continuesFunction · 0.85
ti_scan_tokensFunction · 0.85
ti_scan_pastesFunction · 0.85
ti_growFunction · 0.85
cbm_freeFunction · 0.85
cbm_arena_allocFunction · 0.50
cbm_arena_strndupFunction · 0.50

Tested by

no test coverage detected