<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>

#include "../util/util.h"
#include "../util/alloc.h"
#include "../util/dynarray.h"

#include "macro.h"
#include "main.h"

#define VA_ARGS_STR "__VA_ARGS__"

macro **macros = NULL;

macro *macro_find(const char *sp)
{
	macro **i;
	for(i = macros; i && *i; i++)
		if(!strcmp((*i)->nam, sp))
			return *i;
	return NULL;
}

macro *macro_add(const char *nam, const char *val)
{
	macro *m;

	if(!strcmp(nam, DEFINED_STR))
		CPP_DIE("\"" DEFINED_STR "\" can't be used as a macro name");

	m = macro_find(nam);

	if(m){
		CPP_WARN("cpp: warning: redefining \"%s\"", nam);
		free(m->nam);
		free(m->val);
	}else{
		m = umalloc(sizeof *m);
		dynarray_add(&macros, m);
	}

	m->nam = ustrdup(nam);
	m->val = val ? ustrdup(val) : NULL;
	m->type = MACRO;

	return m;
}

macro *macro_add_func(const char *nam, const char *val, char **args, int variadic)
{
	macro *m  = macro_add(nam, val);
	m->args = args;
	m->type = variadic ? VARIADIC : FUNC;
	return m;
}

void macro_remove(const char *nam)
{
	macro *m = macro_find(nam);
<commit_msg>Free macro args if redefining, don't bother strdup()ing .nam<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>

#include "../util/util.h"
#include "../util/alloc.h"
#include "../util/dynarray.h"

#include "macro.h"
#include "main.h"

#define VA_ARGS_STR "__VA_ARGS__"

macro **macros = NULL;

macro *macro_find(const char *sp)
{
	macro **i;
	for(i = macros; i && *i; i++)
		if(!strcmp((*i)->nam, sp))
			return *i;
	return NULL;
}

macro *macro_add(const char *nam, const char *val)
{
	macro *m;

	if(!strcmp(nam, DEFINED_STR))
		CPP_DIE("\"" DEFINED_STR "\" can't be used as a macro name");

	m = macro_find(nam);

	if(m){
		CPP_WARN("cpp: warning: redefining \"%s\"", nam);
		free(m->val);
	}else{
		m = umalloc(sizeof *m);
		dynarray_add(&macros, m);

		m->nam = ustrdup(nam);
	}

	m->val = val ? ustrdup(val) : NULL;
	m->type = MACRO;

	return m;
}

macro *macro_add_func(const char *nam, const char *val, char **args, int variadic)
{
	macro *m  = macro_add(nam, val);
	if(m->args)
		dynarray_free(char **, &m->args, free);
	m->args = args;
	m->type = variadic ? VARIADIC : FUNC;
	return m;
}

void macro_remove(const char *nam)
{
	macro *m = macro_find(nam);

{'fraction_non_alphanumeric': 0.1947502116850127, 'fraction_numerical': 0.0, 'mean_word_length': 4.00635593220339, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1152539', 'n_tokens_mistral': 1061, 'n_tokens_neox': 997, 'n_words': 249}