History

Added a gcc preprocessor options -P, -P1

tcc -E -P do not output a #line directive, a gcc compatible option tcc -E -P1 don't follow a gcc preprocessor style and do output a standard #line directive. In such case we don't lose a location info when we going to compile a resulting file wtith a compiler not understanding a gnu style line info.

seyko committed 11 years ago

Commit 50cdccf1 parent 40418f84 files · +46 −10Browse files

Unified · Split

4 files
  1. libtcc.c +5 −0
  2. tcc.c +2 −0
  3. tcc.h +5 −0
  4. tccpp.c +34 −10
libtcc.c+5−0
@@ -1647,6 +1647,7 @@ enum { · 1646 unmodified lines
16471647 TCC_OPTION_I,
16481648 TCC_OPTION_D,
16491649 TCC_OPTION_U,
1650+ TCC_OPTION_P,
16501651 TCC_OPTION_L,
16511652 TCC_OPTION_B,
16521653 TCC_OPTION_l,
@@ -1695,6 +1696,7 @@ static const TCCOption tcc_options[] = { · 42 unmodified lines
16951696 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
16961697 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
16971698 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1699+ { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
16981700 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
16991701 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
17001702 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
@@ -1930,6 +1932,9 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) · 229 unmodified lines
19301932 tcc_warning("-E: some compiler action already specified (%d)", s->output_type);
19311933 s->output_type = TCC_OUTPUT_PREPROCESS;
19321934 break;
1935+ case TCC_OPTION_P:
1936+ s->Pflag = atoi(optarg) + 1;
1937+ break;
19331938 case TCC_OPTION_MD:
19341939 s->gen_deps = 1;
19351940 break;
tcc.c+2−0
@@ -45,6 +45,8 @@ static void help(void) · 44 unmodified lines
4545 " -Idir add include path 'dir'\n"
4646 " -Dsym[=val] define 'sym' with value 'val'\n"
4747 " -Usym undefine 'sym'\n"
48+ " -P do not output a #line directive\n"
49+ " -P1 use a #line directive in output instead of the gcc style\n"
4850 "Linker options:\n"
4951 " -Ldir add library path 'dir'\n"
5052 " -llib link with dynamic or static library 'lib'\n"
tcc.h+5−0
@@ -656,6 +656,11 @@ struct TCCState { · 655 unmodified lines
656656
657657 /* output file for preprocessing (-E) */
658658 FILE *ppfp;
659+ enum {
660+ LINE_MACRO_OUTPUT_FORMAT_GCC,
661+ LINE_MACRO_OUTPUT_FORMAT_NONE,
662+ LINE_MACRO_OUTPUT_FORMAT_STD,
663+ } Pflag;
659664
660665 /* for -MD/-MF: collected dependencies for this compilation */
661666 char **target_deps;
tccpp.c+34−10
@@ -3133,6 +3133,26 @@ ST_FUNC void preprocess_new(void) · 3132 unmodified lines
31333133 }
31343134 }
31353135
3136+static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3137+{
3138+ switch (s1->Pflag) {
3139+ case LINE_MACRO_OUTPUT_FORMAT_STD:
3140+ /* "tcc -E -P1" case */
3141+ fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3142+ break;
3143+
3144+ case LINE_MACRO_OUTPUT_FORMAT_NONE:
3145+ /* "tcc -E -P" case: don't output a line directive */
3146+ break;
3147+
3148+ case LINE_MACRO_OUTPUT_FORMAT_GCC:
3149+ default:
3150+ /* "tcc -E" case: a gcc standard by default */
3151+ fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3152+ break;
3153+ }
3154+}
3155+
31363156 /* Preprocess the current file */
31373157 ST_FUNC int tcc_preprocess(TCCState *s1)
31383158 {
@@ -3158,6 +3178,8 @@ ST_FUNC int tcc_preprocess(TCCState *s1) · 19 unmodified lines
31583178 if (tok == TOK_EOF) {
31593179 break;
31603180 } else if (file != file_ref) {
3181+ if (file_ref)
3182+ line_macro_output(file_ref, "", s1);
31613183 goto print_line;
31623184 } else if (tok == TOK_LINEFEED) {
31633185 if (!token_seen)
@@ -3166,18 +3188,20 @@ ST_FUNC int tcc_preprocess(TCCState *s1) · 2 unmodified lines
31663188 token_seen = 0;
31673189 } else if (!token_seen) {
31683190 d = file->line_num - file->line_ref;
3169− if (file != file_ref || d < 0 || d >= 8) {
3191+ if (file != file_ref || d >= 8) {
31703192 print_line:
3171− iptr_new = s1->include_stack_ptr;
3172− s = iptr_new > iptr ? " 1"
3173− : iptr_new < iptr ? " 2"
3174− : iptr_new > s1->include_stack ? " 3"
3175− : ""
3176− ;
3177− iptr = iptr_new;
3178− fprintf(s1->ppfp, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
3193+ s = "";
3194+ if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3195+ iptr_new = s1->include_stack_ptr;
3196+ s = iptr_new > iptr ? " 1"
3197+ : iptr_new < iptr ? " 2"
3198+ : iptr_new > s1->include_stack ? " 3"
3199+ : ""
3200+ ;
3201+ }
3202+ line_macro_output(file, s, s1);
31793203 } else {
3180− while (d)
3204+ while (d > 0)
31813205 fputs("\n", s1->ppfp), --d;
31823206 }
31833207 file->line_ref = (file_ref = file)->line_num;

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