History

initial implementation of -E option

bellard committed 20 years ago

Commit 200b58d1 parent 6ca02992 files · +85 −11Browse files

Unified · Split

2 files
  1. libtcc.h +1 −0
  2. tcc.c +84 −11
libtcc.h+1−0
@@ -60,6 +60,7 @@ int tcc_compile_string(TCCState *s, const char *buf); · 59 unmodified lines
6060 #define TCC_OUTPUT_EXE 1 /* executable file */
6161 #define TCC_OUTPUT_DLL 2 /* dynamic library */
6262 #define TCC_OUTPUT_OBJ 3 /* object file */
63+#define TCC_OUTPUT_PREPROCESS 4 /* preprocessed file (used internally) */
6364 int tcc_set_output_type(TCCState *s, int output_type);
6465
6566 #define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
tcc.c+84−11
@@ -504,6 +504,9 @@ struct TCCState { · 503 unmodified lines
504504 /* pack stack */
505505 int pack_stack[PACK_STACK_SIZE];
506506 int *pack_stack_ptr;
507+
508+ /* output file for preprocessing */
509+ FILE *outfile;
507510 };
508511
509512 /* The current value can be: */
@@ -839,6 +842,7 @@ static int tcc_add_dll(TCCState *s, const char *filename, int flags); · 329 unmodified lines
839842
840843 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
841844 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
845+#define AFF_PREPROCESS 0x0004 /* preprocess file */
842846 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
843847
844848 /* tcccoff.c */
@@ -1675,6 +1679,8 @@ char *get_tok_str(int v, CValue *cv) · 830 unmodified lines
16751679 case TOK_GT:
16761680 v = '>';
16771681 goto addv;
1682+ case TOK_DOTS:
1683+ return strcpy(p, "...");
16781684 case TOK_A_SHL:
16791685 return strcpy(p, "<<=");
16801686 case TOK_A_SAR:
@@ -9279,6 +9285,47 @@ static int tcc_compile(TCCState *s1) · 7598 unmodified lines
92799285 return s1->nb_errors != 0 ? -1 : 0;
92809286 }
92819287
9288+/* Preprocess the current file */
9289+/* XXX: add line and file infos, add options to preserve spaces */
9290+static int tcc_preprocess(TCCState *s1)
9291+{
9292+ Sym *define_start;
9293+ int last_is_space;
9294+
9295+ preprocess_init(s1);
9296+
9297+ define_start = define_stack;
9298+
9299+ ch = file->buf_ptr[0];
9300+ tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9301+ parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9302+ PARSE_FLAG_LINEFEED;
9303+ last_is_space = 1;
9304+ next();
9305+ for(;;) {
9306+ if (tok == TOK_EOF)
9307+ break;
9308+ if (!last_is_space) {
9309+ fputc(' ', s1->outfile);
9310+ }
9311+ fputs(get_tok_str(tok, &tokc), s1->outfile);
9312+ if (tok == TOK_LINEFEED) {
9313+ last_is_space = 1;
9314+ /* XXX: suppress that hack */
9315+ parse_flags &= ~PARSE_FLAG_LINEFEED;
9316+ next();
9317+ parse_flags |= PARSE_FLAG_LINEFEED;
9318+ } else {
9319+ last_is_space = 0;
9320+ next();
9321+ }
9322+ }
9323+
9324+ free_defines(define_start);
9325+
9326+ return 0;
9327+}
9328+
92829329 #ifdef LIBTCC
92839330 int tcc_compile_string(TCCState *s, const char *str)
92849331 {
@@ -9944,7 +9991,9 @@ static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 659 unmodified lines
99449991 goto fail1;
99459992 }
99469993
9947− if (!ext || !strcmp(ext, "c")) {
9994+ if (flags & AFF_PREPROCESS) {
9995+ ret = tcc_preprocess(s1);
9996+ } else if (!ext || !strcmp(ext, "c")) {
99489997 /* C file assumed */
99499998 ret = tcc_compile(s1);
99509999 } else
@@ -10264,7 +10313,7 @@ static int64_t getclock_us(void) · 313 unmodified lines
1026410313
1026510314 void help(void)
1026610315 {
10267− printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2005 Fabrice Bellard\n"
10316+ printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
1026810317 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
1026910318 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
1027010319 " [infile1 infile2...] [-run infile args...]\n"
@@ -10280,6 +10329,7 @@ void help(void) · 9 unmodified lines
1028010329 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
1028110330 " -w disable all warnings\n"
1028210331 "Preprocessor options:\n"
10332+ " -E preprocess only\n"
1028310333 " -Idir add include path 'dir'\n"
1028410334 " -Dsym[=val] define 'sym' with value 'val'\n"
1028510335 " -Usym undefine 'sym'\n"
@@ -10338,6 +10388,7 @@ enum { · 52 unmodified lines
1033810388 TCC_OPTION_v,
1033910389 TCC_OPTION_w,
1034010390 TCC_OPTION_pipe,
10391+ TCC_OPTION_E,
1034110392 };
1034210393
1034310394 static const TCCOption tcc_options[] = {
@@ -10373,6 +10424,7 @@ static const TCCOption tcc_options[] = { · 29 unmodified lines
1037310424 { "v", TCC_OPTION_v, 0 },
1037410425 { "w", TCC_OPTION_w, 0 },
1037510426 { "pipe", TCC_OPTION_pipe, 0},
10427+ { "E", TCC_OPTION_E, 0},
1037610428 { NULL },
1037710429 };
1037810430
@@ -10601,6 +10653,9 @@ int parse_args(TCCState *s, int argc, char **argv) · 222 unmodified lines
1060110653 }
1060210654 }
1060310655 break;
10656+ case TCC_OPTION_E:
10657+ output_type = TCC_OUTPUT_PREPROCESS;
10658+ break;
1060410659 default:
1060510660 if (s->warn_unsupported) {
1060610661 unsupported_option:
@@ -10657,7 +10712,7 @@ int main(int argc, char **argv) · 50 unmodified lines
1065710712 printf("install: %s/\n", tcc_lib_path);
1065810713 return 0;
1065910714 }
10660−
10715+
1066110716 nb_objfiles = nb_files - nb_libraries;
1066210717
1066310718 /* if outfile provided without other options, we output an
@@ -10674,7 +10729,16 @@ int main(int argc, char **argv) · 10 unmodified lines
1067410729 error("cannot specify libraries with -c");
1067510730 }
1067610731
10677− if (output_type != TCC_OUTPUT_MEMORY) {
10732+
10733+ if (output_type == TCC_OUTPUT_PREPROCESS) {
10734+ if (!outfile) {
10735+ s->outfile = stdout;
10736+ } else {
10737+ s->outfile = fopen(outfile, "wb");
10738+ if (!s->outfile)
10739+ error("could not open '%s", outfile);
10740+ }
10741+ } else if (output_type != TCC_OUTPUT_MEMORY) {
1067810742 if (!outfile) {
1067910743 /* compute default outfile name */
1068010744 pstrcpy(objfilename, sizeof(objfilename) - 1,
@@ -10709,13 +10773,18 @@ int main(int argc, char **argv) · 28 unmodified lines
1070910773 const char *filename;
1071010774
1071110775 filename = files[i];
10712− if (filename[0] == '-') {
10713− if (tcc_add_library(s, filename + 2) < 0)
10714− error("cannot find %s", filename);
10776+ if (output_type == TCC_OUTPUT_PREPROCESS) {
10777+ tcc_add_file_internal(s, filename,
10778+ AFF_PRINT_ERROR | AFF_PREPROCESS);
1071510779 } else {
10716− if (tcc_add_file(s, filename) < 0) {
10717− ret = 1;
10718− goto the_end;
10780+ if (filename[0] == '-') {
10781+ if (tcc_add_library(s, filename + 2) < 0)
10782+ error("cannot find %s", filename);
10783+ } else {
10784+ if (tcc_add_file(s, filename) < 0) {
10785+ ret = 1;
10786+ goto the_end;
10787+ }
1071910788 }
1072010789 }
1072110790 }
@@ -10736,7 +10805,11 @@ int main(int argc, char **argv) · 14 unmodified lines
1073610805 total_bytes / total_time / 1000000.0);
1073710806 }
1073810807
10739− if (s->output_type == TCC_OUTPUT_MEMORY) {
10808+ if (s->output_type == TCC_OUTPUT_PREPROCESS) {
10809+ if (outfile)
10810+ fclose(s->outfile);
10811+ ret = 0;
10812+ } else if (s->output_type == TCC_OUTPUT_MEMORY) {
1074010813 ret = tcc_run(s, argc - optind, argv + optind);
1074110814 } else
1074210815 #ifdef TCC_TARGET_PE

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t