libtcc: cleanup -x<filetype> switch code

Abusing filename[0] as type is just too much of a hack.
-- From 0536407204
master
grischka 2016-10-01 20:04:58 +02:00
parent e630113771
commit 09a487eb2b
4 changed files with 34 additions and 39 deletions

View File

@ -1304,7 +1304,8 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags,
parse_flags = 0;
#ifdef CONFIG_TCC_ASM
/* if .S file, define __ASSEMBLER__ like gcc does */
if ((filetype == TCC_FILETYPE_ASM) || (filetype == TCC_FILETYPE_ASM_PP)) {
if (filetype == TCC_FILETYPE_ASM
|| filetype == TCC_FILETYPE_ASM_PP) {
tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
parse_flags = PARSE_FLAG_ASM_FILE;
}
@ -2048,33 +2049,29 @@ static void parse_option_D(TCCState *s1, const char *optarg)
static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
{
int len = strlen(filename);
char *p = tcc_malloc(len + 2);
if (filetype) {
*p = filetype;
}
else {
struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
if (filetype == 0) {
/* use a file extension to detect a filetype */
const char *ext = tcc_fileextension(filename);
if (ext[0]) {
ext++;
if (!strcmp(ext, "S"))
*p = TCC_FILETYPE_ASM_PP;
filetype = TCC_FILETYPE_ASM_PP;
else if (!strcmp(ext, "s"))
filetype = TCC_FILETYPE_ASM;
else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
filetype = TCC_FILETYPE_C;
else
if (!strcmp(ext, "s"))
*p = TCC_FILETYPE_ASM;
else
if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
*p = TCC_FILETYPE_C;
else
*p = TCC_FILETYPE_BINARY;
}
else {
*p = TCC_FILETYPE_C;
filetype = TCC_FILETYPE_BINARY;
} else {
filetype = TCC_FILETYPE_C;
}
}
strcpy(p+1, filename);
dynarray_add((void ***)&s->files, &s->nb_files, p);
f->type = filetype;
strcpy(f->name, filename);
dynarray_add((void ***)&s->files, &s->nb_files, f);
}
ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv)
@ -2158,7 +2155,7 @@ ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv)
tcc_set_lib_path(s, optarg);
break;
case TCC_OPTION_l:
args_parser_add_file(s, r, TCC_FILETYPE_BINARY);
args_parser_add_file(s, optarg, 'l');
s->nb_libraries++;
break;
case TCC_OPTION_pthread:

View File

@ -49,10 +49,10 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym);
/* add a file (C file, dll, object, library, ld script). Return -1 if error. */
LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype);
#define TCC_FILETYPE_BINARY 1
#define TCC_FILETYPE_C 2
#define TCC_FILETYPE_ASM 3
#define TCC_FILETYPE_ASM_PP 4
#define TCC_FILETYPE_BINARY 'b'
#define TCC_FILETYPE_C 'c'
#define TCC_FILETYPE_ASM 's'
#define TCC_FILETYPE_ASM_PP 'S'
/* compile a string containing a C source. Return -1 if error. */
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);

20
tcc.c
View File

@ -307,23 +307,17 @@ int main(int argc, char **argv)
/* compile or add each files or library */
for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
int filetype = *(unsigned char *)s->files[i];
const char *filename = s->files[i] + 1;
if (filename[0] == '-' && filename[1] == 'l') {
if (tcc_add_library(s, filename + 2) < 0) {
/* don't fail on -lm as it's harmless to skip math lib */
if (strcmp(filename + 2, "m")) {
tcc_error_noabort("cannot find library 'lib%s'", filename + 2);
ret = 1;
}
}
struct filespec *f = s->files[i];
if (f->type == 'l') {
if (tcc_add_library_err(s, f->name) < 0)
ret = 1;
} else {
if (1 == s->verbose)
printf("-> %s\n", filename);
if (tcc_add_file(s, filename, filetype) < 0)
printf("-> %s\n", f->name);
if (tcc_add_file(s, f->name, f->type) < 0)
ret = 1;
if (!first_file)
first_file = filename;
first_file = f->name;
}
}

6
tcc.h
View File

@ -850,7 +850,7 @@ struct TCCState {
#endif
/* used by main and tcc_parse_args only */
char **files; /* files seen on command line */
struct filespec **files; /* files seen on command line */
int nb_files; /* number thereof */
int nb_libraries; /* number of libs thereof */
char *outfile; /* output filename */
@ -863,6 +863,10 @@ struct TCCState {
ParseArgsState *parse_args_state;
};
struct filespec {
char type, name[1];
};
/* The current value can be: */
#define VT_VALMASK 0x003f /* mask for value location, register or: */
#define VT_CONST 0x0030 /* constant in vc (must be first non register value) */