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
|
/**
* @file parens.cpp
* Adds or removes parens.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "parens.h"
#include "log_rules.h"
using namespace uncrustify;
//! Add an open parenthesis after first and add a close parenthesis before the last
static void add_parens_between(chunk_t *first, chunk_t *last);
/**
* Scans between two parens and adds additional parens if needed.
* This function is recursive. If it hits another open paren, it'll call itself
* with the new bounds.
*
* Adds optional parens in an IF or SWITCH conditional statement.
*
* This basically just checks for a CT_COMPARE that isn't surrounded by parens.
* The edges for the compare are the open, close and any CT_BOOL tokens.
*
* This only handles VERY simple patterns:
* (!a && b) => (!a && b) -- no change
* (a && b == 1) => (a && (b == 1))
* (a == 1 || b > 2) => ((a == 1) || (b > 2))
*
* FIXME: we really should bail if we transition between a preprocessor and
* a non-preprocessor
*/
static void check_bool_parens(chunk_t *popen, chunk_t *pclose, int nest);
void do_parens(void)
{
constexpr static auto LCURRENT = LPARADD;
LOG_FUNC_ENTRY();
log_rule_B("mod_full_paren_if_bool");
if (options::mod_full_paren_if_bool())
{
chunk_t *pc = chunk_get_head();
while ((pc = chunk_get_next_ncnnl(pc)) != nullptr)
{
if ( pc->type != CT_SPAREN_OPEN
|| ( get_chunk_parent_type(pc) != CT_IF
&& get_chunk_parent_type(pc) != CT_ELSEIF
&& get_chunk_parent_type(pc) != CT_SWITCH))
{
continue;
}
// Grab the close sparen
chunk_t *pclose = chunk_get_next_type(pc, CT_SPAREN_CLOSE, pc->level, scope_e::PREPROC);
if (pclose != nullptr)
{
check_bool_parens(pc, pclose, 0);
pc = pclose;
}
}
}
}
static void add_parens_between(chunk_t *first, chunk_t *last)
{
LOG_FUNC_ENTRY();
LOG_FMT(LPARADD, "%s: line %zu between %s [lvl=%zu] and %s [lvl=%zu]\n",
__func__, first->orig_line,
first->text(), first->level,
last->text(), last->level);
// Don't do anything if we have a bad sequence, ie "&& )"
chunk_t *first_n = chunk_get_next_ncnnl(first);
if (first_n == last)
{
return;
}
chunk_t pc;
set_chunk_type(&pc, CT_PAREN_OPEN);
pc.orig_line = first_n->orig_line;
pc.orig_col = first_n->orig_col;
pc.str = "(";
pc.flags = first_n->flags & PCF_COPY_FLAGS;
pc.level = first_n->level;
pc.pp_level = first_n->pp_level;
pc.brace_level = first_n->brace_level;
chunk_add_before(&pc, first_n);
chunk_t *last_p = chunk_get_prev_ncnnl(last, scope_e::PREPROC);
set_chunk_type(&pc, CT_PAREN_CLOSE);
pc.orig_line = last_p->orig_line;
pc.orig_col = last_p->orig_col;
pc.str = ")";
pc.flags = last_p->flags & PCF_COPY_FLAGS;
pc.level = last_p->level;
pc.pp_level = last_p->pp_level;
pc.brace_level = last_p->brace_level;
chunk_add_after(&pc, last_p);
for (chunk_t *tmp = first_n;
tmp != last_p;
tmp = chunk_get_next_ncnnl(tmp))
{
tmp->level++;
}
last_p->level++;
} // add_parens_between
static void check_bool_parens(chunk_t *popen, chunk_t *pclose, int nest)
{
LOG_FUNC_ENTRY();
chunk_t *ref = popen;
bool hit_compare = false;
LOG_FMT(LPARADD, "%s(%d): popen on %zu, col %zu, pclose on %zu, col %zu, level=%zu\n",
__func__, nest,
popen->orig_line, popen->orig_col,
pclose->orig_line, pclose->orig_col,
popen->level);
chunk_t *pc = popen;
while ( (pc = chunk_get_next_ncnnl(pc)) != nullptr
&& pc != pclose)
{
if (pc->flags.test(PCF_IN_PREPROC))
{
LOG_FMT(LPARADD2, " -- bail on PP %s [%s] at line %zu col %zu, level %zu\n",
get_token_name(pc->type),
pc->text(), pc->orig_line, pc->orig_col, pc->level);
return;
}
if ( chunk_is_token(pc, CT_BOOL)
|| chunk_is_token(pc, CT_QUESTION)
|| chunk_is_token(pc, CT_COND_COLON)
|| chunk_is_token(pc, CT_COMMA))
{
LOG_FMT(LPARADD2, " -- %s [%s] at line %zu col %zu, level %zu\n",
get_token_name(pc->type),
pc->text(), pc->orig_line, pc->orig_col, pc->level);
if (hit_compare)
{
hit_compare = false;
if (!language_is_set(LANG_CS))
{
add_parens_between(ref, pc);
}
}
ref = pc;
}
else if (chunk_is_token(pc, CT_COMPARE))
{
LOG_FMT(LPARADD2, " -- compare [%s] at line %zu col %zu, level %zu\n",
pc->text(), pc->orig_line, pc->orig_col, pc->level);
hit_compare = true;
}
else if (chunk_is_paren_open(pc))
{
chunk_t *next = chunk_skip_to_match(pc);
if (next != nullptr)
{
check_bool_parens(pc, next, nest + 1);
pc = next;
}
}
else if ( chunk_is_token(pc, CT_BRACE_OPEN)
|| chunk_is_token(pc, CT_SQUARE_OPEN)
|| chunk_is_token(pc, CT_ANGLE_OPEN))
{
// Skip [], {}, and <>
pc = chunk_skip_to_match(pc);
}
}
if ( hit_compare
&& ref != popen
&& !language_is_set(LANG_CS))
{
add_parens_between(ref, pclose);
}
} // check_bool_parens
|