Abusing filename[0] as type is just too much of a hack. -- From 05364072042ff37904a7b0b14cdd85a1ea1ca11d
Unified · Split
| @@ -1304,7 +1304,8 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, · 1303 unmodified lines | ||
| 1304 | 1304 | parse_flags = 0; |
| 1305 | 1305 | #ifdef CONFIG_TCC_ASM |
| 1306 | 1306 | /* if .S file, define __ASSEMBLER__ like gcc does */ |
| 1307 | − if ((filetype == TCC_FILETYPE_ASM) || (filetype == TCC_FILETYPE_ASM_PP)) { | |
| 1307 | + if (filetype == TCC_FILETYPE_ASM | |
| 1308 | + || filetype == TCC_FILETYPE_ASM_PP) { | |
| 1308 | 1309 | tcc_define_symbol(s1, "__ASSEMBLER__", NULL); |
| 1309 | 1310 | parse_flags = PARSE_FLAG_ASM_FILE; |
| 1310 | 1311 | } |
| @@ -2048,33 +2049,29 @@ static void parse_option_D(TCCState *s1, const char *optarg) · 737 unmodified lines | ||
| 2048 | 2049 | |
| 2049 | 2050 | static void args_parser_add_file(TCCState *s, const char* filename, int filetype) |
| 2050 | 2051 | { |
| 2051 | − int len = strlen(filename); | |
| 2052 | − char *p = tcc_malloc(len + 2); | |
| 2053 | − if (filetype) { | |
| 2054 | − *p = filetype; | |
| 2055 | − } | |
| 2056 | − else { | |
| 2052 | + struct filespec *f = tcc_malloc(sizeof *f + strlen(filename)); | |
| 2053 | + | |
| 2054 | + if (filetype == 0) { | |
| 2057 | 2055 | /* use a file extension to detect a filetype */ |
| 2058 | 2056 | const char *ext = tcc_fileextension(filename); |
| 2059 | 2057 | if (ext[0]) { |
| 2060 | 2058 | ext++; |
| 2061 | 2059 | if (!strcmp(ext, "S")) |
| 2062 | − *p = TCC_FILETYPE_ASM_PP; | |
| 2063 | − else | |
| 2064 | − if (!strcmp(ext, "s")) | |
| 2065 | − *p = TCC_FILETYPE_ASM; | |
| 2060 | + filetype = TCC_FILETYPE_ASM_PP; | |
| 2061 | + else if (!strcmp(ext, "s")) | |
| 2062 | + filetype = TCC_FILETYPE_ASM; | |
| 2063 | + else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i")) | |
| 2064 | + filetype = TCC_FILETYPE_C; | |
| 2066 | 2065 | else |
| 2067 | − if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i")) | |
| 2068 | − *p = TCC_FILETYPE_C; | |
| 2069 | − else | |
| 2070 | − *p = TCC_FILETYPE_BINARY; | |
| 2071 | − } | |
| 2072 | − else { | |
| 2073 | − *p = TCC_FILETYPE_C; | |
| 2066 | + filetype = TCC_FILETYPE_BINARY; | |
| 2067 | + } else { | |
| 2068 | + filetype = TCC_FILETYPE_C; | |
| 2074 | 2069 | } |
| 2075 | 2070 | } |
| 2076 | − strcpy(p+1, filename); | |
| 2077 | − dynarray_add((void ***)&s->files, &s->nb_files, p); | |
| 2071 | + | |
| 2072 | + f->type = filetype; | |
| 2073 | + strcpy(f->name, filename); | |
| 2074 | + dynarray_add((void ***)&s->files, &s->nb_files, f); | |
| 2078 | 2075 | } |
| 2079 | 2076 | |
| 2080 | 2077 | 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) · 77 unmodified lines | ||
| 2158 | 2155 | tcc_set_lib_path(s, optarg); |
| 2159 | 2156 | break; |
| 2160 | 2157 | case TCC_OPTION_l: |
| 2161 | − args_parser_add_file(s, r, TCC_FILETYPE_BINARY); | |
| 2158 | + args_parser_add_file(s, optarg, 'l'); | |
| 2162 | 2159 | s->nb_libraries++; |
| 2163 | 2160 | break; |
| 2164 | 2161 | case TCC_OPTION_pthread: |
| @@ -49,10 +49,10 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym); · 48 unmodified lines | ||
| 49 | 49 | |
| 50 | 50 | /* add a file (C file, dll, object, library, ld script). Return -1 if error. */ |
| 51 | 51 | LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype); |
| 52 | −#define TCC_FILETYPE_BINARY 1 | |
| 53 | −#define TCC_FILETYPE_C 2 | |
| 54 | −#define TCC_FILETYPE_ASM 3 | |
| 55 | −#define TCC_FILETYPE_ASM_PP 4 | |
| 52 | +#define TCC_FILETYPE_BINARY 'b' | |
| 53 | +#define TCC_FILETYPE_C 'c' | |
| 54 | +#define TCC_FILETYPE_ASM 's' | |
| 55 | +#define TCC_FILETYPE_ASM_PP 'S' | |
| 56 | 56 | |
| 57 | 57 | /* compile a string containing a C source. Return -1 if error. */ |
| 58 | 58 | LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf); |
| @@ -307,23 +307,17 @@ int main(int argc, char **argv) · 306 unmodified lines | ||
| 307 | 307 | |
| 308 | 308 | /* compile or add each files or library */ |
| 309 | 309 | for(i = ret = 0; i < s->nb_files && ret == 0; i++) { |
| 310 | − int filetype = *(unsigned char *)s->files[i]; | |
| 311 | − const char *filename = s->files[i] + 1; | |
| 312 | − if (filename[0] == '-' && filename[1] == 'l') { | |
| 313 | − if (tcc_add_library(s, filename + 2) < 0) { | |
| 314 | − /* don't fail on -lm as it's harmless to skip math lib */ | |
| 315 | − if (strcmp(filename + 2, "m")) { | |
| 316 | − tcc_error_noabort("cannot find library 'lib%s'", filename + 2); | |
| 317 | − ret = 1; | |
| 318 | − } | |
| 319 | − } | |
| 310 | + struct filespec *f = s->files[i]; | |
| 311 | + if (f->type == 'l') { | |
| 312 | + if (tcc_add_library_err(s, f->name) < 0) | |
| 313 | + ret = 1; | |
| 320 | 314 | } else { |
| 321 | 315 | if (1 == s->verbose) |
| 322 | − printf("-> %s\n", filename); | |
| 323 | − if (tcc_add_file(s, filename, filetype) < 0) | |
| 316 | + printf("-> %s\n", f->name); | |
| 317 | + if (tcc_add_file(s, f->name, f->type) < 0) | |
| 324 | 318 | ret = 1; |
| 325 | 319 | if (!first_file) |
| 326 | − first_file = filename; | |
| 320 | + first_file = f->name; | |
| 327 | 321 | } |
| 328 | 322 | } |
| 329 | 323 | |
| @@ -850,7 +850,7 @@ struct TCCState { · 849 unmodified lines | ||
| 850 | 850 | #endif |
| 851 | 851 | |
| 852 | 852 | /* used by main and tcc_parse_args only */ |
| 853 | − char **files; /* files seen on command line */ | |
| 853 | + struct filespec **files; /* files seen on command line */ | |
| 854 | 854 | int nb_files; /* number thereof */ |
| 855 | 855 | int nb_libraries; /* number of libs thereof */ |
| 856 | 856 | char *outfile; /* output filename */ |
| @@ -863,6 +863,10 @@ struct TCCState { · 6 unmodified lines | ||
| 863 | 863 | ParseArgsState *parse_args_state; |
| 864 | 864 | }; |
| 865 | 865 | |
| 866 | +struct filespec { | |
| 867 | + char type, name[1]; | |
| 868 | +}; | |
| 869 | + | |
| 866 | 870 | /* The current value can be: */ |
| 867 | 871 | #define VT_VALMASK 0x003f /* mask for value location, register or: */ |
| 868 | 872 | #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */ |