Namespace “pstr” functions to avoid conflicts

E.g. when linking QuickJS into the same program or library.
This commit is contained in:
Ole André Vadla Ravnås 2020-10-15 22:38:45 +02:00
parent 57e351b2dd
commit c93d276e82
10 changed files with 72 additions and 70 deletions

View File

@ -163,7 +163,7 @@ static void il_type_to_str(char *buf, int buf_size,
bt = t & VT_BTYPE; bt = t & VT_BTYPE;
buf[0] = '\0'; buf[0] = '\0';
if (t & VT_UNSIGNED) if (t & VT_UNSIGNED)
pstrcat(buf, buf_size, "unsigned "); tcc_pstrcat(buf, buf_size, "unsigned ");
switch(bt) { switch(bt) {
case VT_VOID: case VT_VOID:
tstr = "void"; tstr = "void";
@ -192,7 +192,7 @@ static void il_type_to_str(char *buf, int buf_size,
case VT_LDOUBLE: case VT_LDOUBLE:
tstr = "float64"; tstr = "float64";
add_tstr: add_tstr:
pstrcat(buf, buf_size, tstr); tcc_pstrcat(buf, buf_size, tstr);
break; break;
case VT_STRUCT: case VT_STRUCT:
tcc_error("structures not handled yet"); tcc_error("structures not handled yet");
@ -200,28 +200,28 @@ static void il_type_to_str(char *buf, int buf_size,
case VT_FUNC: case VT_FUNC:
s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
il_type_to_str(buf, buf_size, s->t, varstr); il_type_to_str(buf, buf_size, s->t, varstr);
pstrcat(buf, buf_size, "("); tcc_pstrcat(buf, buf_size, "(");
sa = s->next; sa = s->next;
while (sa != NULL) { while (sa != NULL) {
il_type_to_str(buf1, sizeof(buf1), sa->t, NULL); il_type_to_str(buf1, sizeof(buf1), sa->t, NULL);
pstrcat(buf, buf_size, buf1); tcc_pstrcat(buf, buf_size, buf1);
sa = sa->next; sa = sa->next;
if (sa) if (sa)
pstrcat(buf, buf_size, ", "); tcc_pstrcat(buf, buf_size, ", ");
} }
pstrcat(buf, buf_size, ")"); tcc_pstrcat(buf, buf_size, ")");
goto no_var; goto no_var;
case VT_PTR: case VT_PTR:
s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
pstrcpy(buf1, sizeof(buf1), "*"); tcc_pstrcpy(buf1, sizeof(buf1), "*");
if (varstr) if (varstr)
pstrcat(buf1, sizeof(buf1), varstr); tcc_pstrcat(buf1, sizeof(buf1), varstr);
il_type_to_str(buf, buf_size, s->t, buf1); il_type_to_str(buf, buf_size, s->t, buf1);
goto no_var; goto no_var;
} }
if (varstr) { if (varstr) {
pstrcat(buf, buf_size, " "); tcc_pstrcat(buf, buf_size, " ");
pstrcat(buf, buf_size, varstr); tcc_pstrcat(buf, buf_size, varstr);
} }
no_var: ; no_var: ;
} }

View File

@ -132,7 +132,7 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
/********************************************************/ /********************************************************/
/* copy a string and truncate it. */ /* copy a string and truncate it. */
ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s) ST_FUNC char *tcc_pstrcpy(char *buf, int buf_size, const char *s)
{ {
char *q, *q_end; char *q, *q_end;
int c; int c;
@ -152,16 +152,16 @@ ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
} }
/* strcat and truncate. */ /* strcat and truncate. */
ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s) ST_FUNC char *tcc_pstrcat(char *buf, int buf_size, const char *s)
{ {
int len; int len;
len = strlen(buf); len = strlen(buf);
if (len < buf_size) if (len < buf_size)
pstrcpy(buf + len, buf_size - len, s); tcc_pstrcpy(buf + len, buf_size - len, s);
return buf; return buf;
} }
ST_FUNC char *pstrncpy(char *out, const char *in, size_t num) ST_FUNC char *tcc_pstrncpy(char *out, const char *in, size_t num)
{ {
memcpy(out, in, num); memcpy(out, in, num);
out[num] = '\0'; out[num] = '\0';
@ -592,7 +592,7 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
bf->buf_ptr = bf->buffer; bf->buf_ptr = bf->buffer;
bf->buf_end = bf->buffer + initlen; bf->buf_end = bf->buffer + initlen;
bf->buf_end[0] = CH_EOB; /* put eob symbol */ bf->buf_end[0] = CH_EOB; /* put eob symbol */
pstrcpy(bf->filename, sizeof(bf->filename), filename); tcc_pstrcpy(bf->filename, sizeof(bf->filename), filename);
bf->true_filename = bf->filename; bf->true_filename = bf->filename;
bf->line_num = 1; bf->line_num = 1;
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
@ -1347,7 +1347,7 @@ static void copy_linker_arg(char **pp, const char *s, int sep)
if (p && sep) if (p && sep)
p[l = strlen(p)] = sep, ++l; p[l = strlen(p)] = sep, ++l;
skip_linker_arg(&q); skip_linker_arg(&q);
pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s); tcc_pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
} }
/* set linker options */ /* set linker options */

6
tcc.h
View File

@ -1128,9 +1128,9 @@ ST_DATA int tcc_ext;
ST_DATA struct TCCState *tcc_state; ST_DATA struct TCCState *tcc_state;
/* public functions currently used by the tcc main function */ /* public functions currently used by the tcc main function */
ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s); ST_FUNC char *tcc_pstrcpy(char *buf, int buf_size, const char *s);
ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s); ST_FUNC char *tcc_pstrcat(char *buf, int buf_size, const char *s);
ST_FUNC char *pstrncpy(char *out, const char *in, size_t num); ST_FUNC char *tcc_pstrncpy(char *out, const char *in, size_t num);
PUB_FUNC char *tcc_basename(const char *name); PUB_FUNC char *tcc_basename(const char *name);
PUB_FUNC char *tcc_fileextension (const char *name); PUB_FUNC char *tcc_fileextension (const char *name);

View File

@ -739,9 +739,9 @@ static void asm_parse_directive(TCCState *s1, int global)
next(); next();
if (tok == TOK_STR) if (tok == TOK_STR)
pstrcat(filename, sizeof(filename), tokc.str.data); tcc_pstrcat(filename, sizeof(filename), tokc.str.data);
else else
pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL)); tcc_pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
if (s1->warn_unsupported) if (s1->warn_unsupported)
tcc_warning("ignoring .file %s", filename); tcc_warning("ignoring .file %s", filename);
@ -757,9 +757,9 @@ static void asm_parse_directive(TCCState *s1, int global)
next(); next();
if (tok == TOK_STR) if (tok == TOK_STR)
pstrcat(ident, sizeof(ident), tokc.str.data); tcc_pstrcat(ident, sizeof(ident), tokc.str.data);
else else
pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL)); tcc_pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
if (s1->warn_unsupported) if (s1->warn_unsupported)
tcc_warning("ignoring .ident %s", ident); tcc_warning("ignoring .ident %s", ident);
@ -827,9 +827,9 @@ static void asm_parse_directive(TCCState *s1, int global)
sname[0] = '\0'; sname[0] = '\0';
while (tok != ';' && tok != TOK_LINEFEED && tok != ',') { while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
if (tok == TOK_STR) if (tok == TOK_STR)
pstrcat(sname, sizeof(sname), tokc.str.data); tcc_pstrcat(sname, sizeof(sname), tokc.str.data);
else else
pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL)); tcc_pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
next(); next();
} }
if (tok == ',') { if (tok == ',') {

View File

@ -247,8 +247,8 @@ ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
p = strchr(str, ':'); p = strchr(str, ':');
if (!p) { if (!p) {
pstrcpy(func_name, sizeof(func_name), str); tcc_pstrcpy(func_name, sizeof(func_name), str);
pstrcpy(Func[nFuncs], sizeof(func_name), str); tcc_pstrcpy(Func[nFuncs], sizeof(func_name), str);
} else { } else {
len = p - str; len = p - str;
if (len > sizeof(func_name) - 1) if (len > sizeof(func_name) - 1)
@ -259,8 +259,8 @@ ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
} }
// save the file that it came in so we can sort later // save the file that it came in so we can sort later
pstrcpy(AssociatedFile[nFuncs], sizeof(func_name), tcc_pstrcpy(AssociatedFile[nFuncs], sizeof(func_name),
incl_files[incl_index - 1]); incl_files[incl_index - 1]);
func_addr = sym->n_value; func_addr = sym->n_value;
} }
@ -422,7 +422,7 @@ ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
p = strchr(str, ':'); p = strchr(str, ':');
if (!p) { if (!p) {
pstrcpy(func_name, sizeof(func_name), str); tcc_pstrcpy(func_name, sizeof(func_name), str);
} else { } else {
len = p - str; len = p - str;
if (len > sizeof(func_name) - 1) if (len > sizeof(func_name) - 1)

View File

@ -3079,7 +3079,7 @@ static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
ret = -1; ret = -1;
goto lib_parse_error; goto lib_parse_error;
} }
pstrcpy(libname, sizeof libname, &filename[1]); tcc_pstrcpy(libname, sizeof libname, &filename[1]);
if (s1->static_link) { if (s1->static_link) {
snprintf(filename, sizeof filename, "lib%s.a", libname); snprintf(filename, sizeof filename, "lib%s.a", libname);
} else { } else {

View File

@ -199,7 +199,7 @@ ST_FUNC void tcc_debug_start(TCCState *s1)
#ifdef _WIN32 #ifdef _WIN32
normalize_slashes(buf); normalize_slashes(buf);
#endif #endif
pstrcat(buf, sizeof(buf), "/"); tcc_pstrcat(buf, sizeof(buf), "/");
put_stabs_r(buf, N_SO, 0, 0, put_stabs_r(buf, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym); text_section->data_offset, text_section, section_sym);
put_stabs_r(file->filename, N_SO, 0, 0, put_stabs_r(file->filename, N_SO, 0, 0,
@ -441,7 +441,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, int sh_num,
#endif #endif
if (tcc_state->leading_underscore && can_add_underscore) { if (tcc_state->leading_underscore && can_add_underscore) {
buf1[0] = '_'; buf1[0] = '_';
pstrcpy(buf1 + 1, sizeof(buf1) - 1, name); tcc_pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
name = buf1; name = buf1;
} }
if (sym->asm_label) if (sym->asm_label)
@ -3096,24 +3096,24 @@ static void type_to_str(char *buf, int buf_size,
buf[0] = '\0'; buf[0] = '\0';
if (t & VT_EXTERN) if (t & VT_EXTERN)
pstrcat(buf, buf_size, "extern "); tcc_pstrcat(buf, buf_size, "extern ");
if (t & VT_STATIC) if (t & VT_STATIC)
pstrcat(buf, buf_size, "static "); tcc_pstrcat(buf, buf_size, "static ");
if (t & VT_TYPEDEF) if (t & VT_TYPEDEF)
pstrcat(buf, buf_size, "typedef "); tcc_pstrcat(buf, buf_size, "typedef ");
if (t & VT_INLINE) if (t & VT_INLINE)
pstrcat(buf, buf_size, "inline "); tcc_pstrcat(buf, buf_size, "inline ");
if (t & VT_VOLATILE) if (t & VT_VOLATILE)
pstrcat(buf, buf_size, "volatile "); tcc_pstrcat(buf, buf_size, "volatile ");
if (t & VT_CONSTANT) if (t & VT_CONSTANT)
pstrcat(buf, buf_size, "const "); tcc_pstrcat(buf, buf_size, "const ");
if (((t & VT_DEFSIGN) && bt == VT_BYTE) if (((t & VT_DEFSIGN) && bt == VT_BYTE)
|| ((t & VT_UNSIGNED) || ((t & VT_UNSIGNED)
&& (bt == VT_SHORT || bt == VT_INT || bt == VT_LLONG) && (bt == VT_SHORT || bt == VT_INT || bt == VT_LLONG)
&& !IS_ENUM(t) && !IS_ENUM(t)
)) ))
pstrcat(buf, buf_size, (t & VT_UNSIGNED) ? "unsigned " : "signed "); tcc_pstrcat(buf, buf_size, (t & VT_UNSIGNED) ? "unsigned " : "signed ");
buf_size -= strlen(buf); buf_size -= strlen(buf);
buf += strlen(buf); buf += strlen(buf);
@ -3152,41 +3152,41 @@ static void type_to_str(char *buf, int buf_size,
case VT_LDOUBLE: case VT_LDOUBLE:
tstr = "long double"; tstr = "long double";
add_tstr: add_tstr:
pstrcat(buf, buf_size, tstr); tcc_pstrcat(buf, buf_size, tstr);
break; break;
case VT_STRUCT: case VT_STRUCT:
tstr = "struct "; tstr = "struct ";
if (IS_UNION(t)) if (IS_UNION(t))
tstr = "union "; tstr = "union ";
tstruct: tstruct:
pstrcat(buf, buf_size, tstr); tcc_pstrcat(buf, buf_size, tstr);
v = type->ref->v & ~SYM_STRUCT; v = type->ref->v & ~SYM_STRUCT;
if (v >= SYM_FIRST_ANOM) if (v >= SYM_FIRST_ANOM)
pstrcat(buf, buf_size, "<anonymous>"); tcc_pstrcat(buf, buf_size, "<anonymous>");
else else
pstrcat(buf, buf_size, get_tok_str(v, NULL)); tcc_pstrcat(buf, buf_size, get_tok_str(v, NULL));
break; break;
case VT_FUNC: case VT_FUNC:
s = type->ref; s = type->ref;
buf1[0]=0; buf1[0]=0;
if (varstr && '*' == *varstr) { if (varstr && '*' == *varstr) {
pstrcat(buf1, sizeof(buf1), "("); tcc_pstrcat(buf1, sizeof(buf1), "(");
pstrcat(buf1, sizeof(buf1), varstr); tcc_pstrcat(buf1, sizeof(buf1), varstr);
pstrcat(buf1, sizeof(buf1), ")"); tcc_pstrcat(buf1, sizeof(buf1), ")");
} }
pstrcat(buf1, buf_size, "("); tcc_pstrcat(buf1, buf_size, "(");
sa = s->next; sa = s->next;
while (sa != NULL) { while (sa != NULL) {
char buf2[256]; char buf2[256];
type_to_str(buf2, sizeof(buf2), &sa->type, NULL); type_to_str(buf2, sizeof(buf2), &sa->type, NULL);
pstrcat(buf1, sizeof(buf1), buf2); tcc_pstrcat(buf1, sizeof(buf1), buf2);
sa = sa->next; sa = sa->next;
if (sa) if (sa)
pstrcat(buf1, sizeof(buf1), ", "); tcc_pstrcat(buf1, sizeof(buf1), ", ");
} }
if (s->f.func_type == FUNC_ELLIPSIS) if (s->f.func_type == FUNC_ELLIPSIS)
pstrcat(buf1, sizeof(buf1), ", ..."); tcc_pstrcat(buf1, sizeof(buf1), ", ...");
pstrcat(buf1, sizeof(buf1), ")"); tcc_pstrcat(buf1, sizeof(buf1), ")");
type_to_str(buf, buf_size, &s->type, buf1); type_to_str(buf, buf_size, &s->type, buf1);
goto no_var; goto no_var;
case VT_PTR: case VT_PTR:
@ -3199,19 +3199,19 @@ static void type_to_str(char *buf, int buf_size,
type_to_str(buf, buf_size, &s->type, buf1); type_to_str(buf, buf_size, &s->type, buf1);
goto no_var; goto no_var;
} }
pstrcpy(buf1, sizeof(buf1), "*"); tcc_pstrcpy(buf1, sizeof(buf1), "*");
if (t & VT_CONSTANT) if (t & VT_CONSTANT)
pstrcat(buf1, buf_size, "const "); tcc_pstrcat(buf1, buf_size, "const ");
if (t & VT_VOLATILE) if (t & VT_VOLATILE)
pstrcat(buf1, buf_size, "volatile "); tcc_pstrcat(buf1, buf_size, "volatile ");
if (varstr) if (varstr)
pstrcat(buf1, sizeof(buf1), varstr); tcc_pstrcat(buf1, sizeof(buf1), varstr);
type_to_str(buf, buf_size, &s->type, buf1); type_to_str(buf, buf_size, &s->type, buf1);
goto no_var; goto no_var;
} }
if (varstr) { if (varstr) {
pstrcat(buf, buf_size, " "); tcc_pstrcat(buf, buf_size, " ");
pstrcat(buf, buf_size, varstr); tcc_pstrcat(buf, buf_size, varstr);
} }
no_var: ; no_var: ;
} }
@ -7651,7 +7651,8 @@ static void gen_inline_functions(TCCState *s)
generate its code and convert it to a normal function */ generate its code and convert it to a normal function */
fn->sym = NULL; fn->sym = NULL;
if (file) if (file)
pstrcpy(file->filename, sizeof file->filename, fn->filename); tcc_pstrcpy(file->filename, sizeof file->filename,
fn->filename);
begin_macro(fn->func_str, 1); begin_macro(fn->func_str, 1);
next(); next();
cur_text_section = text_section; cur_text_section = text_section;

View File

@ -951,7 +951,7 @@ static void pe_build_exports(struct pe_info *pe)
#if 1 #if 1
/* automatically write exports to <output-filename>.def */ /* automatically write exports to <output-filename>.def */
pstrcpy(buf, sizeof buf, pe->filename); tcc_pstrcpy(buf, sizeof buf, pe->filename);
strcpy(tcc_fileextension(buf), ".def"); strcpy(tcc_fileextension(buf), ".def");
op = fopen(buf, "wb"); op = fopen(buf, "wb");
if (NULL == op) { if (NULL == op) {
@ -1709,7 +1709,7 @@ static int pe_load_def(TCCState *s1, int fd)
case 0: case 0:
if (0 != strnicmp(p, "LIBRARY", 7)) if (0 != strnicmp(p, "LIBRARY", 7))
goto quit; goto quit;
pstrcpy(dllname, sizeof dllname, trimfront(p+7)); tcc_pstrcpy(dllname, sizeof dllname, trimfront(p+7));
++state; ++state;
continue; continue;

13
tccpp.c
View File

@ -1784,7 +1784,7 @@ ST_FUNC void preprocess(int is_bof)
next(); next();
buf[0] = '\0'; buf[0] = '\0';
while (tok != TOK_LINEFEED) { while (tok != TOK_LINEFEED) {
pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc)); tcc_pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
next(); next();
} }
len = strlen(buf); len = strlen(buf);
@ -1820,17 +1820,17 @@ ST_FUNC void preprocess(int is_bof)
continue; continue;
/* https://savannah.nongnu.org/bugs/index.php?50847 */ /* https://savannah.nongnu.org/bugs/index.php?50847 */
path = file->true_filename; path = file->true_filename;
pstrncpy(buf1, path, tcc_basename(path) - path); tcc_pstrncpy(buf1, path, tcc_basename(path) - path);
} else { } else {
/* search in all the include paths */ /* search in all the include paths */
int j = i - 2, k = j - s1->nb_include_paths; int j = i - 2, k = j - s1->nb_include_paths;
path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k]; path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
pstrcpy(buf1, sizeof(buf1), path); tcc_pstrcpy(buf1, sizeof(buf1), path);
pstrcat(buf1, sizeof(buf1), "/"); tcc_pstrcat(buf1, sizeof(buf1), "/");
} }
pstrcat(buf1, sizeof(buf1), buf); tcc_pstrcat(buf1, sizeof(buf1), buf);
e = search_cached_include(s1, buf1, 0); e = search_cached_include(s1, buf1, 0);
if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) { if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
/* no need to parse the include because the 'ifndef macro' /* no need to parse the include because the 'ifndef macro'
@ -1973,7 +1973,8 @@ include_done:
if (tok == TOK_STR) { if (tok == TOK_STR) {
if (file->true_filename == file->filename) if (file->true_filename == file->filename)
file->true_filename = tcc_strdup(file->filename); file->true_filename = tcc_strdup(file->filename);
pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data); tcc_pstrcpy(file->filename, sizeof(file->filename),
(char *)tokc.str.data);
} else if (parse_flags & PARSE_FLAG_ASM_FILE) } else if (parse_flags & PARSE_FLAG_ASM_FILE)
break; break;
else else

View File

@ -402,7 +402,7 @@ static addr_t rt_printline(addr_t wanted_pc, const char *msg)
str = stab_str + sym->n_strx; str = stab_str + sym->n_strx;
p = strchr(str, ':'); p = strchr(str, ':');
if (!p) { if (!p) {
pstrcpy(func_name, sizeof(func_name), str); tcc_pstrcpy(func_name, sizeof(func_name), str);
} else { } else {
len = p - str; len = p - str;
if (len > sizeof(func_name) - 1) if (len > sizeof(func_name) - 1)
@ -465,8 +465,8 @@ no_stabs:
if (type == STT_FUNC || type == STT_GNU_IFUNC) { if (type == STT_FUNC || type == STT_GNU_IFUNC) {
if (wanted_pc >= sym->st_value && if (wanted_pc >= sym->st_value &&
wanted_pc < sym->st_value + sym->st_size) { wanted_pc < sym->st_value + sym->st_size) {
pstrcpy(last_func_name, sizeof(last_func_name), tcc_pstrcpy(last_func_name, sizeof(last_func_name),
(char *) symtab_section->link->data + sym->st_name); (char *) symtab_section->link->data + sym->st_name);
func_addr = sym->st_value; func_addr = sym->st_value;
goto found; goto found;
} }