-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcc.c
148 lines (128 loc) · 3.3 KB
/
cc.c
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
#include "cc.h"
static int printed_line = 1;
static int last_line = 1;
static int dirty_line_p;
const char *main_input_filename;
const char *main_output_filename;
static void maybe_print_lines(int diff)
{
if (diff > 5) {
if (dirty_line_p) {
dirty_line_p = 0;
fputs("\n", stdout);
}
printed_line += diff;
printf("# %d\n", printed_line);
} else if (diff > 0) {
while (diff) {
fputs("\n", stdout);
printed_line++;
diff--;
}
dirty_line_p = 0;
}
}
static void cb_file_change(int line, int reason)
{
const char *oname;
int oline;
switch (reason) {
case FC_ENTER:
if (line > 1) {
cpp_resolve_location(line - 1, NULL, &oline);
maybe_print_lines(oline - printed_line);
}
/* fall thru */
case FC_LEAVE:
if (dirty_line_p) {
dirty_line_p = 0;
fputs("\n", stdout);
}
cpp_resolve_location(line, &oname, &oline);
printed_line = oline;
printf("# %d \"%s\"\n", oline, oname);
break;
case FC_RENAME:
if (dirty_line_p) {
dirty_line_p = 0;
fputs("\n", stdout);
}
printed_line = hist->offset;
if (hist->name)
printf("# %d \"%s\"\n", printed_line, hist->name);
else
printf("# %d\n", printed_line);
break;
}
last_line = line;
}
static void cb_line_change(int line, struct token *tok)
{
int d;
if (tok->id == TOK_EOF)
return;
d = line - last_line;
if (d > 0)
maybe_print_lines(d);
last_line = line;
}
static void preprocess(void)
{
struct token result;
do {
cpp_get_token(&result);
if (result.id == TOK_EOF) {
if (dirty_line_p) {
dirty_line_p = 0;
fputs("\n", stdout);
}
break;
}
if (result.flags & TOK_FLAG_PREV_WHITE)
fputs(" ", stdout);
fputs(tok2s(&result), stdout);
dirty_line_p = 1;
} while (1);
}
int cc1(const char *ifile, const char *ofile, char **opts)
{
char *opt;
/* stdin as input */
if (ifile && ifile[0] == '-' && ifile[1] == 0)
ifile = "";
/* stdout as output */
if (ofile && ofile[0] == '-' && ofile[1] == 0)
ofile = "";
main_input_filename = ifile;
main_output_filename = ofile;
/* set callbacks for standalone mode */
if (options.preprocess_only) {
cpp_callbacks.file_change = cb_file_change;
cpp_callbacks.line_change = cb_line_change;
}
fmtinit();
tokens_init();
types_init();
cpp_init(ifile);
for (; (opt = *opts); opts++)
if (opt[1] == 'D')
cpp_define(opt + 2);
else if (opt[1] == 'U')
cpp_undef(opt + 2);
else if (opt[1] == 'I')
cpp_add_include(opt + 2, 0);
/* GNU C Common Predefined Macros */
if (options.optimize > 0)
cpp_define("__OPTIMIZE__");
/* FIXME: auto set -fPIC when -DPIC */
if (cpp_macro_defined_p("PIC"))
options.pic = 1;
if (options.preprocess_only) {
preprocess();
} else {
ginit();
parse();
gfini();
}
return errors > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}