History

libtcc: cleanup -x<filetype> switch code

Abusing filename[0] as type is just too much of a hack. -- From 05364072042ff37904a7b0b14cdd85a1ea1ca11d

grischka committed 10 years ago

Commit 09a487e1 parent e6301134 files · +34 −39Browse files

Unified · Split

4 files
  1. libtcc.c +18 −21
  2. libtcc.h +4 −4
  3. tcc.c +7 −13
  4. tcc.h +5 −1
libtcc.c+18−21
@@ -1304,7 +1304,8 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, · 1303 unmodified lines
13041304 parse_flags = 0;
13051305 #ifdef CONFIG_TCC_ASM
13061306 /* 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) {
13081309 tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
13091310 parse_flags = PARSE_FLAG_ASM_FILE;
13101311 }
@@ -2048,33 +2049,29 @@ static void parse_option_D(TCCState *s1, const char *optarg) · 737 unmodified lines
20482049
20492050 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
20502051 {
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) {
20572055 /* use a file extension to detect a filetype */
20582056 const char *ext = tcc_fileextension(filename);
20592057 if (ext[0]) {
20602058 ext++;
20612059 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;
20662065 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;
20742069 }
20752070 }
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);
20782075 }
20792076
20802077 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
21582155 tcc_set_lib_path(s, optarg);
21592156 break;
21602157 case TCC_OPTION_l:
2161− args_parser_add_file(s, r, TCC_FILETYPE_BINARY);
2158+ args_parser_add_file(s, optarg, 'l');
21622159 s->nb_libraries++;
21632160 break;
21642161 case TCC_OPTION_pthread:
libtcc.h+4−4
@@ -49,10 +49,10 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym); · 48 unmodified lines
4949
5050 /* add a file (C file, dll, object, library, ld script). Return -1 if error. */
5151 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'
5656
5757 /* compile a string containing a C source. Return -1 if error. */
5858 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);
tcc.c+7−13
@@ -307,23 +307,17 @@ int main(int argc, char **argv) · 306 unmodified lines
307307
308308 /* compile or add each files or library */
309309 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;
320314 } else {
321315 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)
324318 ret = 1;
325319 if (!first_file)
326− first_file = filename;
320+ first_file = f->name;
327321 }
328322 }
329323
tcc.h+5−1
@@ -850,7 +850,7 @@ struct TCCState { · 849 unmodified lines
850850 #endif
851851
852852 /* 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 */
854854 int nb_files; /* number thereof */
855855 int nb_libraries; /* number of libs thereof */
856856 char *outfile; /* output filename */
@@ -863,6 +863,10 @@ struct TCCState { · 6 unmodified lines
863863 ParseArgsState *parse_args_state;
864864 };
865865
866+struct filespec {
867+ char type, name[1];
868+};
869+
866870 /* The current value can be: */
867871 #define VT_VALMASK 0x003f /* mask for value location, register or: */
868872 #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */