forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathparse_frame.cpp
More file actions
264 lines (220 loc) · 6.48 KB
/
parse_frame.cpp
File metadata and controls
264 lines (220 loc) · 6.48 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* @file parse_frame.cpp
* Does the parse frame stuff, which is used to handle #ifdef stuff
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "parse_frame.h"
#include "uncrustify_types.h"
#include "prototypes.h"
#include "chunk_list.h"
#include "uncrustify.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
static void pf_log_frms(log_sev_t logsev, const char *txt, parse_frame_t *pf);
//! Logs the entire parse frame stack
static void pf_log_all(log_sev_t logsev);
//! Logs one parse frame
static void pf_log(log_sev_t logsev, parse_frame_t *pf)
{
LOG_FMT(logsev, "[%s] BrLevel=%d Level=%d PseTos=%zu\n",
get_token_name(pf->in_ifdef),
pf->brace_level, pf->level, pf->pse_tos);
LOG_FMT(logsev, " *");
for (size_t idx = 1; idx <= pf->pse_tos; idx++)
{
LOG_FMT(logsev, " [%s-%d]",
get_token_name(pf->pse[idx].type),
(int)pf->pse[idx].stage);
}
LOG_FMT(logsev, "\n");
}
static void pf_log_frms(log_sev_t logsev, const char *txt, parse_frame_t *pf)
{
LOG_FMT(logsev, "%s Parse Frames(%d):", txt, cpd.frame_count);
for (int idx = 0; idx < cpd.frame_count; idx++)
{
LOG_FMT(logsev, " [%s-%d]",
get_token_name(cpd.frames[idx].in_ifdef),
cpd.frames[idx].ref_no);
}
LOG_FMT(logsev, "-[%s-%d]\n", get_token_name(pf->in_ifdef), pf->ref_no);
}
static void pf_log_all(log_sev_t logsev)
{
LOG_FMT(logsev, "##=- Parse Frame : %d entries\n", cpd.frame_count);
for (int idx = 0; idx < cpd.frame_count; idx++)
{
LOG_FMT(logsev, "## <%d> ", idx);
pf_log(logsev, &cpd.frames[idx]);
}
LOG_FMT(logsev, "##=-\n");
}
void pf_copy(parse_frame_t *dst, const parse_frame_t *src)
{
memcpy(dst, src, sizeof(parse_frame_t));
}
void pf_push(parse_frame_t *pf)
{
static int ref_no = 1;
if (cpd.frame_count < static_cast<int> ARRAY_SIZE(cpd.frames))
{
pf_copy(&cpd.frames[cpd.frame_count], pf);
cpd.frame_count++;
pf->ref_no = ref_no++;
}
LOG_FMT(LPF, "%s(%d): count = %d\n", __func__, __LINE__, cpd.frame_count);
}
void pf_push_under(parse_frame_t *pf)
{
LOG_FMT(LPF, "%s(%d): before count = %d\n", __func__, __LINE__, cpd.frame_count);
if ( cpd.frame_count < static_cast<int> ARRAY_SIZE(cpd.frames)
&& cpd.frame_count >= 1)
{
parse_frame_t *npf1 = &cpd.frames[cpd.frame_count - 1];
parse_frame_t *npf2 = &cpd.frames[cpd.frame_count];
pf_copy(npf2, npf1);
pf_copy(npf1, pf);
cpd.frame_count++;
}
LOG_FMT(LPF, "%s(%d): after count = %d\n", __func__, __LINE__, cpd.frame_count);
}
void pf_copy_tos(parse_frame_t *pf)
{
if (cpd.frame_count > 0)
{
pf_copy(pf, &cpd.frames[cpd.frame_count - 1]);
}
LOG_FMT(LPF, "%s(%d): count = %d\n", __func__, __LINE__, cpd.frame_count);
}
void pf_copy_2nd_tos(parse_frame_t *pf)
{
if (cpd.frame_count > 1)
{
pf_copy(pf, &cpd.frames[cpd.frame_count - 2]);
}
LOG_FMT(LPF, "%s(%d): count = %d\n", __func__, __LINE__, cpd.frame_count);
}
void pf_trash_tos(void)
{
if (cpd.frame_count > 0)
{
cpd.frame_count--;
}
LOG_FMT(LPF, "%s(%d): count = %d\n", __func__, __LINE__, cpd.frame_count);
}
void pf_pop(parse_frame_t *pf)
{
if (cpd.frame_count > 0)
{
pf_copy_tos(pf);
pf_trash_tos();
}
}
int pf_check(parse_frame_t *frm, chunk_t *pc)
{
int in_ifdef = frm->in_ifdef;
int b4_cnt = cpd.frame_count;
int pp_level = cpd.pp_level;
if (pc->type != CT_PREPROC)
{
return(pp_level);
}
chunk_t *next = chunk_get_next(pc);
if (next == nullptr)
{
return(pp_level);
}
if (pc->parent_type != next->type)
{
LOG_FMT(LNOTE, "%s(%d): Preproc parent not set correctly on line %zu: got %s expected %s\n",
__func__, __LINE__, pc->orig_line, get_token_name(pc->parent_type),
get_token_name(next->type));
set_chunk_parent(pc, next->type);
}
LOG_FMT(LPFCHK, "%s(%d): %zu] %s\n",
__func__, __LINE__, pc->orig_line, get_token_name(pc->parent_type));
pf_log_frms(LPFCHK, "TOP", frm);
const char *txt = nullptr;
if (pc->flags & PCF_IN_PREPROC)
{
LOG_FMT(LPF, " <In> ");
pf_log(LPF, frm);
if (pc->parent_type == CT_PP_IF)
{
// An #if pushes a copy of the current frame on the stack
cpd.pp_level++;
pf_push(frm);
frm->in_ifdef = CT_PP_IF;
txt = "if-push";
}
else if (pc->parent_type == CT_PP_ELSE)
{
pp_level--;
/*
* For #else of #elif, we want to keep the #if part and throw out the
* else parts.
* We check to see what the top type is to see if we just push or
* pop and then push.
* We need to use the copy right before the if.
*/
if (frm->in_ifdef == CT_PP_IF)
{
// we have [...] [base]-[if], so push an [else]
pf_push(frm);
frm->in_ifdef = CT_PP_ELSE;
}
// we have [...] [base] [if]-[else], copy [base] over [else]
pf_copy_2nd_tos(frm);
frm->in_ifdef = CT_PP_ELSE;
txt = "else-push";
}
else if (pc->parent_type == CT_PP_ENDIF)
{
/*
* we may have [...] [base] [if]-[else] or [...] [base]-[if].
* Throw out the [else].
*/
cpd.pp_level--;
pp_level--;
if (frm->in_ifdef == CT_PP_ELSE)
{
/*
* We have: [...] [base] [if]-[else]
* We want: [...]-[if]
*/
pf_copy_tos(frm); // [...] [base] [if]-[if]
frm->in_ifdef = cpd.frames[cpd.frame_count - 2].in_ifdef;
pf_trash_tos(); // [...] [base]-[if]
pf_trash_tos(); // [...]-[if]
txt = "endif-trash/pop";
}
else if (frm->in_ifdef == CT_PP_IF)
{
/*
* We have: [...] [base] [if]
* We want: [...] [base]
*/
pf_pop(frm);
txt = "endif-pop";
}
else
{
txt = "???";
}
}
}
if (txt != nullptr)
{
LOG_FMT(LPF, "%s(%d): %zu> %s: %s in_ifdef=%d/%d counts=%d/%d\n", __func__, __LINE__,
pc->orig_line, get_token_name(pc->parent_type), txt,
in_ifdef, frm->in_ifdef, b4_cnt, cpd.frame_count);
pf_log_all(LPF);
LOG_FMT(LPF, " <Out>");
pf_log(LPF, frm);
}
pf_log_frms(LPFCHK, "END", frm);
return(pp_level);
} // pf_check