-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-stmt-empty-loop.c
66 lines (53 loc) · 1.34 KB
/
simple-stmt-empty-loop.c
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
#include "simple-stmt-empty-loop.h"
#include "config.h"
#include "clang.h"
#include "lexer.h"
#include "token.h"
static int
sense_empty_loop_braces(const struct token *lbrace, const struct token *rbrace)
{
const struct token *nx;
if (!token_is_moveable(lbrace) || !token_is_moveable(rbrace))
return 0;
nx = token_next(lbrace);
if (nx == rbrace)
return 1;
if (nx->tk_type == TOKEN_SEMI && token_is_moveable(nx) &&
token_next(nx) == rbrace)
return 1;
return 0;
}
static struct token *
insert_continue(struct lexer *lx, struct token *after)
{
return lexer_insert_after(lx, after,
clang_keyword_token(TOKEN_CONTINUE));
}
static struct token *
insert_semi(struct lexer *lx, struct token *after)
{
return lexer_insert_after(lx, after, clang_keyword_token(TOKEN_SEMI));
}
void
simple_stmt_empty_loop_braces(struct lexer *lx)
{
struct token *after, *lbrace, *nx, *rbrace;
if (!lexer_peek_if_pair(lx, TOKEN_LBRACE, TOKEN_RBRACE, &lbrace,
&rbrace))
return;
if (!sense_empty_loop_braces(lbrace, rbrace))
return;
after = insert_continue(lx, lbrace);
nx = token_next(after);
if (nx->tk_type != TOKEN_SEMI)
insert_semi(lx, after);
}
void
simple_stmt_empty_loop_no_braces(struct lexer *lx)
{
struct token *after;
if (!lexer_peek_if(lx, TOKEN_SEMI, NULL) ||
!lexer_back(lx, &after))
return;
insert_continue(lx, after);
}