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
|
/**
* @file calculate_closing_brace_position.cpp
*
* @author Guy Maurel
* @license GPL v2+
*/
#include "calculate_closing_brace_position.h"
#include "chunk_list.h"
using namespace uncrustify;
chunk_t *calculate_closing_brace_position(const chunk_t *cl_colon, chunk_t *pc)
{
LOG_FMT(LMCB, "%s(%d): cl_colon->text() is '%s', orig_line %zu, orig_col is %zu, level is %zu\n",
__func__, __LINE__, cl_colon->text(), cl_colon->orig_line, cl_colon->orig_col, cl_colon->level);
LOG_FMT(LMCB, "%s(%d): pc->text() is '%s', orig_line %zu, orig_col is %zu, level is %zu\n",
__func__, __LINE__, pc->text(), pc->orig_line, pc->orig_col, pc->level);
// end of block is reached
// look back over newline, preprocessor BUT NOT #endif
// Issue #3058
// examine going back the tokens: look for a "brace closing" or a "semi colon" until the colon
// look back over comment, newline, preprocessor BUT NOT #endif
size_t check_level = 0;
if (chunk_is_token(pc, CT_BRACE_CLOSE))
{
check_level = pc->level + 1;
}
else
{
check_level = pc->level;
}
size_t erst_found = 0;
chunk_t *is_brace_close = nullptr;
chunk_t *is_semicolon = nullptr;
chunk_t *is_comment = nullptr;
chunk_t *back = chunk_get_prev_nnl(pc);
while (back != nullptr)
{
if (back == cl_colon)
{
break;
}
if (erst_found != 0)
{
break;
}
if (back->level == check_level)
{
if ( chunk_is_token(back, CT_BRACE_CLOSE)
|| chunk_is_token(back, CT_VBRACE_CLOSE))
{
// brace_close found
is_brace_close = back;
LOG_FMT(LMCB, "%s(%d): BRACE_CLOSE: line is %zu, col is %zu, level is %zu\n",
__func__, __LINE__, is_brace_close->orig_line, is_brace_close->orig_col, is_brace_close->level);
erst_found = 3;
}
if (chunk_is_token(back, CT_SEMICOLON))
{
// semicolon found
is_semicolon = back;
LOG_FMT(LMCB, "%s(%d): SEMICOLON: line is %zu, col is %zu, level is %zu\n",
__func__, __LINE__, is_semicolon->orig_line, is_semicolon->orig_col, is_semicolon->level);
erst_found = 4;
}
if (chunk_is_comment(back))
{
// comment found
is_comment = back;
LOG_FMT(LMCB, "%s(%d): COMMENT: line is %zu, col is %zu, level is %zu\n",
__func__, __LINE__, back->orig_line, back->orig_col, back->level);
}
}
back = chunk_get_prev(back);
}
LOG_FMT(LMCB, "%s(%d): erst_found is %zu\n",
__func__, __LINE__, erst_found);
chunk_t *last = nullptr;
if ( erst_found == 3
|| erst_found == 4)
{
if (is_comment != nullptr)
{
chunk_t *second = nullptr;
if (erst_found == 3)
{
second = is_brace_close;
}
else
{
// erst_found == 4
second = is_semicolon;
}
if (second != nullptr)
{
if (is_comment->orig_line == second->orig_line)
{
last = is_comment;
if (cl_colon->orig_line == is_comment->orig_line)
{
last = chunk_get_next(is_comment);
}
}
else
{
last = chunk_get_prev_ncnnl(pc);
}
}
else
{
LOG_FMT(LMCB, "\n\n%s(%d):\n", __func__, __LINE__);
fprintf(stderr, "FATAL: second is nullptr\n");
fprintf(stderr, "Please make a report.\n");
exit(EX_SOFTWARE);
}
}
else
{
last = chunk_get_prev_ncnnl(pc);
}
}
else
{
LOG_FMT(LMCB, "\n\n%s(%d):\n", __func__, __LINE__);
fprintf(stderr, "FATAL: erst_found is not 3 or 4\n");
fprintf(stderr, "Please make a report.\n");
exit(EX_SOFTWARE);
}
if (chunk_is_token(last, CT_COMMENT_CPP)) // Issue #3058
{
last = chunk_get_next(last);
}
LOG_FMT(LMCB, "%s(%d): last->text() is '%s', orig_line %zu, orig_col is %zu\n",
__func__, __LINE__, last->text(), last->orig_line, last->orig_col);
if (chunk_is_preproc(last))
{
// we have a preprocessor token
while (last != nullptr)
{
LOG_FMT(LMCB, "%s(%d): text() is '%s', orig_line %zu, orig_col is %zu\n",
__func__, __LINE__, last->text(), last->orig_line, last->orig_col);
if (chunk_is_token(last, CT_PP_ENDIF))
{
// look for the parent
chunk_t *parent_last = last->parent;
// compare the positions
int comp = chunk_compare_position(parent_last, cl_colon);
LOG_FMT(LMCB, "%s(%d): comp is %d\n",
__func__, __LINE__, comp);
if (comp == -1)
{
// cl_colon is after parent_last ==>
// the closing brace will be set before #endif
chunk_t *pp_start = chunk_get_pp_start(last);
last = chunk_get_prev_nnl(pp_start);
LOG_FMT(LMCB, "%s(%d): text() is '%s', orig_line %zu, orig_col is %zu\n",
__func__, __LINE__, last->text(), last->orig_line, last->orig_col);
}
else if (comp == 1)
{
// cl_colon is before parent_last ==>
// the closing brace will be set after #endif
LOG_FMT(LMCB, "%s(%d): text() is '%s', orig_line %zu, orig_col is %zu\n",
__func__, __LINE__, last->text(), last->orig_line, last->orig_col);
}
break;
}
last = chunk_get_prev_ncnnl(last);
LOG_FMT(LMCB, "%s(%d): text() is '%s', orig_line %zu, orig_col is %zu\n",
__func__, __LINE__, last->text(), last->orig_line, last->orig_col);
if (!chunk_is_preproc(last))
{
break;
}
}
}
return(last);
} // calculate_closing_brace_position
|