1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "../preproc/preproc.h"
#include "../preproc/ptoken.h"
#include "../preproc/pproto.h"
int line_cntrl;
/*
* output - output preprocessed tokens for the current file.
*/
void output(out_file)
FILE *out_file;
{
struct token *t, *t1;
struct token *saved_whsp;
char *fname;
char *s;
int line;
int nxt_line;
int trail_nl; /* flag: trailing character in output is a new-line */
int blank_ln; /* flag: output ends with blank line */
fname = "";
line = -1;
/*
* Suppress an initial new-line in the output.
*/
trail_nl = 1;
blank_ln = 1;
while ((t = preproc()) != NULL) {
if (t->flag & LineChk) {
/*
* This token is significant with respect to outputting #line
* directives.
*/
nxt_line = t->line;
if (fname != t->fname || line != nxt_line) {
/*
* We need a #line directive. Make sure it is preceeded by a
* blank line.
*/
if (!trail_nl) {
putc('\n', out_file);
++line;
trail_nl = 1;
}
if (!blank_ln && (line != nxt_line || fname != t->fname)) {
putc('\n', out_file);
++line;
blank_ln = 1;
}
/*
* Eliminate extra new-lines from the subsequent text before
* inserting line directive. This make the output look better.
* The line number for the directive will change if new-lines
* are eliminated.
*/
saved_whsp = NULL;
s = t->image;
while (t->tok_id == WhiteSpace && (*s == ' ' || *s == '\n' ||
*s == '\t')) {
if (*s == '\n') {
/*
* Discard any white space before the new-line and update
* the line number.
*/
free_t(saved_whsp);
saved_whsp = NULL;
t->image = s + 1;
++t->line;
++nxt_line;
}
++s;
if (*s == '\0') {
/*
* The end of the current white space token has been
* reached, see if the next token is also white space.
*/
free_t(saved_whsp);
t1 = preproc();
if (t1 == NULL) {
/*
* We are at the end of the input. Don't output
* a #line directive, just make sure the output
* ends with a new-line.
*/
free_t(t);
if (!trail_nl)
putc('\n', out_file);
return;
}
/*
* The previous token may contain non-new-line white
* space, if the new token is on the same line, save
* that previous token in case we want to print the
* white space (this will correctly indent the new
* token).
*/
if (*(t->image) != '\0' && t->line == t1->line &&
t->fname == t1->fname)
saved_whsp = t;
else {
free_t(t);
saved_whsp = NULL;
}
t = t1;
s = t->image;
nxt_line = t->line;
}
}
if (line_cntrl) {
/*
* We are supposed to insert #line directives where needed.
* However, one or two blank lines look better when they
* are enough to reestablish the correct line number.
*/
if (fname != t->fname || line > nxt_line ||
line + 2 < nxt_line) {
/*
* Normally a blank line is put after the #line
* directive; However, this requires decrementing
* the line number and a line number of 0 is not
* valid.
*/
if (nxt_line > 1)
fprintf(out_file, "#line %d \"", nxt_line - 1);
else
fprintf(out_file, "#line %d \"", nxt_line);
for (s = t->fname; *s != '\0'; ++s) {
if (*s == '"' || *s == '\\')
putc('\\',out_file);
putc(*s, out_file);
}
fprintf(out_file, "\"\n");
if (nxt_line > 1)
fprintf(out_file, "\n"); /* blank line after directive */
trail_nl = 1;
blank_ln = 1;
}
else /* adjust line number with blank lines */
while (line < nxt_line) {
putc('\n', out_file);
++line;
if (trail_nl)
blank_ln = 1;
trail_nl = 1;
}
}
/*
* See if we need to indent the next token with white space
* saved while eliminating extra new-lines.
*/
if (saved_whsp != NULL) {
fprintf(out_file, "%s", saved_whsp->image);
free_t(saved_whsp);
if (trail_nl) {
blank_ln = 1;
trail_nl = 0;
}
}
line = t->line;
fname = t->fname;
}
}
/*
* Print the image of the token.
*/
if (t->tok_id == WhiteSpace) {
/*
* Keep track of trailing blank lines and new-lines. This
* information is used to make the insertion of #line
* directives more intelligent and to insure that the output
* file ends with a new-line.
*/
for (s = t->image; *s != '\0'; ++s) {
putc(*s, out_file);
switch (*s) {
case '\n':
if (trail_nl)
blank_ln = 1;
trail_nl = 1;
++line;
break;
case ' ':
case '\t':
if (trail_nl)
blank_ln = 1;
trail_nl = 0;
break;
default:
trail_nl = 0;
}
}
}
else {
/*
* Add delimiters to string and character literals.
*/
switch (t->tok_id) {
case StrLit:
fprintf(out_file, "\"%s\"", t->image);
break;
case LStrLit:
fprintf(out_file, "L\"%s\"", t->image);
break;
case CharConst:
fprintf(out_file, "'%s'", t->image);
break;
case LCharConst:
fprintf(out_file, "L'%s'", t->image);
break;
default:
fprintf(out_file, "%s", t->image);
}
trail_nl = 0;
blank_ln = 0;
}
free_t(t);
}
/*
* Make sure output file ends with a new-line.
*/
if (!trail_nl)
putc('\n', out_file);
}
|