Mercurial > hg > index.cgi
comparison lwcc/cpp-main.c @ 305:54f213c8fb81 ccdev
Various bugfixes and output tuning
Tuned output of preprocessor to include line markers similar to the ones
added by the gcc preprocessor.
Also, many fixes for various bits of dumbosity leading to misbehaviour and
crashing.
author | William Astle <lost@l-w.ca> |
---|---|
date | Wed, 18 Sep 2013 19:17:52 -0600 |
parents | d85d173ba120 |
children | b08787e5b9f3 |
comparison
equal
deleted
inserted
replaced
304:d85d173ba120 | 305:54f213c8fb81 |
---|---|
25 #include <stdlib.h> | 25 #include <stdlib.h> |
26 #include <string.h> | 26 #include <string.h> |
27 | 27 |
28 #include <lw_stringlist.h> | 28 #include <lw_stringlist.h> |
29 #include <lw_cmdline.h> | 29 #include <lw_cmdline.h> |
30 #include <lw_string.h> | |
30 | 31 |
31 #include "cpp.h" | 32 #include "cpp.h" |
32 | 33 |
33 int process_file(const char *); | 34 int process_file(const char *); |
34 static void do_error(const char *f, ...); | 35 static void do_error(const char *f, ...); |
132 break; | 133 break; |
133 } | 134 } |
134 } | 135 } |
135 lw_stringlist_destroy(input_files); | 136 lw_stringlist_destroy(input_files); |
136 | 137 |
137 // symbol_dump(); | |
138 exit(retval); | 138 exit(retval); |
139 } | |
140 | |
141 static void print_line_marker(FILE *fp, int line, const char *fn, int flag) | |
142 { | |
143 fprintf(fp, "\n# %d \"", line); | |
144 while (*fn) | |
145 { | |
146 if (*fn < 32 || *fn == 34 || *fn > 126) | |
147 { | |
148 fprintf(fp, "\\%03o", *fn); | |
149 } | |
150 else | |
151 { | |
152 fprintf(fp, "%c", *fn); | |
153 } | |
154 fn++; | |
155 } | |
156 fprintf(fp, "\" %d\n", flag); | |
139 } | 157 } |
140 | 158 |
141 int process_file(const char *fn) | 159 int process_file(const char *fn) |
142 { | 160 { |
143 struct preproc_info *pp; | 161 struct preproc_info *pp; |
144 struct token *tok = NULL; | 162 struct token *tok = NULL; |
145 | 163 int last_line = 0; |
164 char *last_fn = NULL; | |
165 | |
146 pp = preproc_init(fn); | 166 pp = preproc_init(fn); |
147 if (!pp) | 167 if (!pp) |
148 return -1; | 168 return -1; |
149 | 169 |
170 print_line_marker(output_fp, 1, fn, 1); | |
171 last_fn = lw_strdup(fn); | |
150 for (;;) | 172 for (;;) |
151 { | 173 { |
152 tok = preproc_next(pp); | 174 tok = preproc_next(pp); |
153 if (tok -> ttype == TOK_EOF) | 175 if (tok -> ttype == TOK_EOF) |
154 break; | 176 break; |
177 if (strcmp(tok -> fn, last_fn) != 0) | |
178 { | |
179 int lt = 1; | |
180 if (tok -> lineno != 1) | |
181 { | |
182 lt = 2; | |
183 } | |
184 lw_free(last_fn); | |
185 last_fn = lw_strdup(tok -> fn); | |
186 last_line = tok -> lineno; | |
187 print_line_marker(output_fp, last_line, last_fn, lt); | |
188 } | |
189 else | |
190 { | |
191 while (tok -> lineno > last_line) | |
192 { | |
193 fprintf(output_fp, "\n"); | |
194 last_line++; | |
195 } | |
196 } | |
155 token_print(tok, output_fp); | 197 token_print(tok, output_fp); |
198 if (tok -> ttype == TOK_EOL) | |
199 last_line++; | |
156 token_free(tok); | 200 token_free(tok); |
157 } | 201 } |
158 token_free(tok); | 202 token_free(tok); |
203 lw_free(last_fn); | |
204 // symtab_dump(pp); | |
159 preproc_finish(pp); | 205 preproc_finish(pp); |
160 return 0; | 206 return 0; |
161 } | 207 } |
162 | 208 |
163 static void do_error(const char *f, ...) | 209 static void do_error(const char *f, ...) |