forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemicolons.cpp
More file actions
135 lines (122 loc) · 3.86 KB
/
semicolons.cpp
File metadata and controls
135 lines (122 loc) · 3.86 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
/**
* @file semicolons.cpp
* Removes extra semicolons
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "uncrustify_types.h"
#include "chunk_list.h"
#include "ChunkStack.h"
#include "prototypes.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include "unc_ctype.h"
#include <cassert>
static void check_unknown_brace_close(chunk_t *semi, chunk_t *brace_close);
static void remove_semicolon(chunk_t *pc)
{
LOG_FUNC_ENTRY();
LOG_FMT(LDELSEMI, "%s: Removed semicolon at line %d, col %d",
__func__, pc->orig_line, pc->orig_col);
log_func_stack_inline(LDELSEMI);
/* TODO: do we want to shift stuff back a column? */
chunk_del(pc);
}
/**
* Removes superfluous semicolons:
* - after brace close whose parent is IF, ELSE, SWITCH, WHILE, FOR, NAMESPACE
* - after another semicolon where parent is not FOR
* - (D) after brace close whose parent is ENUM/STRUCT/UNION
* - after an open brace
* - when not in a #DEFINE
*/
void remove_extra_semicolons(void)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
chunk_t *next;
chunk_t *prev;
pc = chunk_get_head();
while (pc != NULL)
{
next = chunk_get_next_ncnl(pc);
if ((pc->type == CT_SEMICOLON) && !(pc->flags & PCF_IN_PREPROC) &&
((prev = chunk_get_prev_ncnl(pc)) != NULL))
{
LOG_FMT(LSCANSEMI, "Semi on %d:%d parent=%s, prev = '%s' [%s/%s]\n",
pc->orig_line, pc->orig_col, get_token_name(pc->parent_type),
prev->str.c_str(),
get_token_name(prev->type), get_token_name(prev->parent_type));
if (pc->parent_type == CT_TYPEDEF)
{
/* keep it */
}
else if ((prev->type == CT_BRACE_CLOSE) &&
((prev->parent_type == CT_IF) ||
(prev->parent_type == CT_ELSEIF) ||
(prev->parent_type == CT_ELSE) ||
(prev->parent_type == CT_SWITCH) ||
(prev->parent_type == CT_WHILE) ||
(prev->parent_type == CT_USING_STMT) ||
(prev->parent_type == CT_FOR) ||
(prev->parent_type == CT_FUNC_DEF) ||
(prev->parent_type == CT_OC_MSG_DECL) ||
(prev->parent_type == CT_FUNC_CLASS_DEF) ||
(prev->parent_type == CT_NAMESPACE)))
{
LOG_FUNC_CALL();
remove_semicolon(pc);
}
else if ((prev->type == CT_BRACE_CLOSE) &&
(prev->parent_type == CT_NONE))
{
check_unknown_brace_close(pc, prev);
}
else if ((prev->type == CT_SEMICOLON) &&
(prev->parent_type != CT_FOR))
{
LOG_FUNC_CALL();
remove_semicolon(pc);
}
else if ((cpd.lang_flags & LANG_D) &&
((prev->parent_type == CT_ENUM) ||
(prev->parent_type == CT_UNION) ||
(prev->parent_type == CT_STRUCT)))
{
LOG_FUNC_CALL();
remove_semicolon(pc);
}
else if (prev->type == CT_BRACE_OPEN)
{
LOG_FUNC_CALL();
remove_semicolon(pc);
}
}
pc = next;
}
}
/**
* We are on a semicolon that is after an unidentified brace close.
* Check for what is before the brace open.
* Do not remove if it is a square close, word, type, or paren close.
*/
static void check_unknown_brace_close(chunk_t *semi, chunk_t *brace_close)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
pc = chunk_get_prev_type(brace_close, CT_BRACE_OPEN, brace_close->level);
pc = chunk_get_prev_ncnl(pc);
if ((pc != NULL) &&
(pc->type != CT_RETURN) &&
(pc->type != CT_WORD) &&
(pc->type != CT_TYPE) &&
(pc->type != CT_SQUARE_CLOSE) &&
(pc->type != CT_TSQUARE) &&
!chunk_is_paren_close(pc))
{
remove_semicolon(semi);
}
}