Migrate static STRING_MAX_SIZE buffers to CString instances for large macros expansion

master
Vlad Vissoultchev 2016-03-13 04:26:45 +02:00
parent d715ebdae0
commit 32755dbea9
1 changed files with 23 additions and 14 deletions

37
tccpp.c
View File

@ -277,16 +277,16 @@ ST_FUNC TokenSym *tok_alloc(const char *str, int len)
/* XXX: float tokens */ /* XXX: float tokens */
ST_FUNC const char *get_tok_str(int v, CValue *cv) ST_FUNC const char *get_tok_str(int v, CValue *cv)
{ {
static char buf[STRING_MAX_SIZE + 1];
static CString cstr_buf; static CString cstr_buf;
char *p; char *p;
int i, len; int i, len;
/* NOTE: to go faster, we give a fixed buffer for small strings */ /* first time preallocate static cstr_buf, next time only reset position to start */
cstr_reset(&cstr_buf); if (!cstr_buf.data_allocated)
cstr_buf.data = buf; cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
cstr_buf.size_allocated = sizeof(buf); else
p = buf; cstr_buf.size = 0;
p = cstr_buf.data;
switch(v) { switch(v) {
case TOK_CINT: case TOK_CINT:
@ -333,17 +333,20 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv)
case TOK_CFLOAT: case TOK_CFLOAT:
cstr_cat(&cstr_buf, "<float>"); cstr_cat(&cstr_buf, "<float>");
cstr_ccat(&cstr_buf, '\0');
break; break;
case TOK_CDOUBLE: case TOK_CDOUBLE:
cstr_cat(&cstr_buf, "<double>"); cstr_cat(&cstr_buf, "<double>");
cstr_ccat(&cstr_buf, '\0');
break; break;
case TOK_CLDOUBLE: case TOK_CLDOUBLE:
cstr_cat(&cstr_buf, "<long double>"); cstr_cat(&cstr_buf, "<long double>");
cstr_ccat(&cstr_buf, '\0');
break; break;
case TOK_LINENUM: case TOK_LINENUM:
cstr_cat(&cstr_buf, "<linenumber>"); cstr_cat(&cstr_buf, "<linenumber>");
cstr_ccat(&cstr_buf, '\0');
break; break;
//return NULL; /* should not happen */
/* above tokens have value, the ones below don't */ /* above tokens have value, the ones below don't */
@ -368,13 +371,13 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv)
*p++ = q[0]; *p++ = q[0];
*p++ = q[1]; *p++ = q[1];
*p = '\0'; *p = '\0';
return buf; return cstr_buf.data;
} }
q += 3; q += 3;
} }
if (v >= 127) { if (v >= 127) {
sprintf(buf, "<%02x>", v); sprintf(cstr_buf.data, "<%02x>", v);
return buf; return cstr_buf.data;
} }
addv: addv:
*p++ = v; *p++ = v;
@ -1048,14 +1051,20 @@ static int tok_last(const int *str0, const int *str1)
static int macro_is_equal(const int *a, const int *b) static int macro_is_equal(const int *a, const int *b)
{ {
char buf[STRING_MAX_SIZE + 1]; static CString cstr_buf;
CValue cv; CValue cv;
int t; int t;
while (*a && *b) { while (*a && *b) {
/* first time preallocate static cstr_buf, next time only reset position to start */
if (!cstr_buf.data_allocated)
cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
else
cstr_buf.size = 0;
TOK_GET(&t, &a, &cv); TOK_GET(&t, &a, &cv);
pstrcpy(buf, sizeof buf, get_tok_str(t, &cv)); cstr_cat(&cstr_buf, get_tok_str(t, &cv));
cstr_ccat(&cstr_buf, '\0');
TOK_GET(&t, &b, &cv); TOK_GET(&t, &b, &cv);
if (strcmp(buf, get_tok_str(t, &cv))) if (strcmp(cstr_buf.data, get_tok_str(t, &cv)))
return 0; return 0;
} }
return !(*a || *b); return !(*a || *b);
@ -2785,7 +2794,7 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
&& 0 == check_space(t, &spc)) { && 0 == check_space(t, &spc)) {
const char *s = get_tok_str(t, &cval); const char *s = get_tok_str(t, &cval);
while (*s) { while (*s) {
if (t == TOK_PPSTR && *s != '\'') if (/*t == TOK_PPSTR &&*/ *s != '\'')
add_char(&cstr, *s); add_char(&cstr, *s);
else else
cstr_ccat(&cstr, *s); cstr_ccat(&cstr, *s);