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
|
/**
* @file calculate_closing_brace_position.cpp
*
* @author Guy Maurel
* @license GPL v2+
*/
#include "calculate_closing_brace_position.h"
#include "chunk.h"
using namespace uncrustify;
Chunk *calculate_closing_brace_position(const Chunk *cl_colon, Chunk *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->GetOrigLine(), cl_colon->GetOrigCol(), cl_colon->GetLevel());
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->GetOrigLine(), pc->GetOrigCol(), pc->GetLevel());
// 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 (pc->Is(CT_BRACE_CLOSE))
{
check_level = pc->GetLevel() + 1;
}
else
{
check_level = pc->GetLevel();
}
size_t erst_found = 0;
Chunk *is_brace_close = Chunk::NullChunkPtr;
Chunk *is_semicolon = Chunk::NullChunkPtr;
Chunk *is_comment = Chunk::NullChunkPtr;
Chunk *back = pc->GetPrevNnl();
while (back->IsNotNullChunk())
{
if (back == cl_colon)
{
break;
}
if (erst_found != 0)
{
break;
}
if (back->GetLevel() == check_level)
{
if (back->IsBraceClose())
{
// 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->GetOrigLine(), is_brace_close->GetOrigCol(), is_brace_close->GetLevel());
erst_found = 3;
}
if (back->Is(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->GetOrigLine(), is_semicolon->GetOrigCol(), is_semicolon->GetLevel());
erst_found = 4;
}
if (back->IsComment())
{
// comment found
is_comment = back;
LOG_FMT(LMCB, "%s(%d): COMMENT: line is %zu, col is %zu, level is %zu\n",
__func__, __LINE__, back->GetOrigLine(), back->GetOrigCol(), back->GetLevel());
}
}
back = back->GetPrev();
}
LOG_FMT(LMCB, "%s(%d): erst_found is %zu\n",
__func__, __LINE__, erst_found);
Chunk *last = Chunk::NullChunkPtr;
if ( erst_found == 3
|| erst_found == 4)
{
if (is_comment->IsNotNullChunk())
{
Chunk *second = Chunk::NullChunkPtr;
if (erst_found == 3)
{
second = is_brace_close;
}
else
{
// erst_found == 4
second = is_semicolon;
}
if (second->IsNotNullChunk())
{
if (is_comment->GetOrigLine() == second->GetOrigLine())
{
last = is_comment;
if (cl_colon->GetOrigLine() == is_comment->GetOrigLine())
{
last = is_comment->GetNext();
}
}
else
{
last = pc->GetPrevNcNnl();
}
}
else
{
LOG_FMT(LMCB, "\n\n%s(%d): FATAL: second is null chunk\n", __func__, __LINE__);
fprintf(stderr, "FATAL: second is null chunk\n");
fprintf(stderr, "Please make a report.\n");
exit(EX_SOFTWARE);
}
}
else
{
last = pc->GetPrevNcNnl();
}
}
else
{
LOG_FMT(LMCB, "\n\n%s(%d): FATAL: erst_found is not 3 or 4\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 (last->Is(CT_COMMENT_CPP)) // Issue #3058
{
last = last->GetNext();
}
LOG_FMT(LMCB, "%s(%d): last->Text() is '%s', orig line %zu, orig col is %zu\n",
__func__, __LINE__, last->Text(), last->GetOrigLine(), last->GetOrigCol());
if (last->IsPreproc())
{
// we have a preprocessor token
while (last->IsNotNullChunk())
{
LOG_FMT(LMCB, "%s(%d): Text() is '%s', orig line %zu, orig col is %zu\n",
__func__, __LINE__, last->Text(), last->GetOrigLine(), last->GetOrigCol());
if (last->Is(CT_PP_ENDIF))
{
// look for the parent and compare the positions
Chunk *parent_last = last->GetParent();
int comp = parent_last->ComparePosition(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 *pp_start = last->GetPpStart();
last = pp_start->GetPrevNnl();
LOG_FMT(LMCB, "%s(%d): Text() is '%s', orig line %zu, orig col is %zu\n",
__func__, __LINE__, last->Text(), last->GetOrigLine(), last->GetOrigCol());
}
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->GetOrigLine(), last->GetOrigCol());
}
break;
}
last = last->GetPrevNcNnl();
LOG_FMT(LMCB, "%s(%d): Text() is '%s', orig line %zu, orig col is %zu\n",
__func__, __LINE__, last->Text(), last->GetOrigLine(), last->GetOrigCol());
if (!last->IsPreproc())
{
break;
}
}
}
return(last);
} // calculate_closing_brace_position
|