forked from error27/smatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_macros.c
78 lines (69 loc) · 2.05 KB
/
check_macros.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
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (C) 2010 Dan Carpenter.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
static int my_id;
static void match_inside(struct expression *expr, struct position pos)
{
char *name;
int matched = 0;
if (positions_eq(expr->pos, pos))
matched++;
if (positions_eq(expr->unop->pos, pos))
matched++;
if (matched != 1)
return;
name = get_macro_name(pos);
if (!name)
return;
sm_warning("the '%s' macro might need parens", name);
}
static void match_one_side(struct expression *expr, struct position pos, int op)
{
char *name;
int matched = 0;
if ((op == '+' || op == '*' || op == '|' || op == '&') && expr->op == op)
return;
if (positions_eq(expr->right->pos, pos))
matched++;
if (positions_eq(expr->left->pos, pos))
matched++;
if (matched != 1)
return;
name = get_macro_name(pos);
if (!name)
return;
if (option_project == PROJ_WINE && !strcmp("BEGIN", name))
return;
sm_warning("the '%s' macro might need parens", name);
}
static void match_join(struct expression *expr)
{
if (expr->left->type == EXPR_PREOP)
match_inside(expr->left, expr->pos);
if (expr->right->type == EXPR_POSTOP)
match_inside(expr->right, expr->pos);
if (expr->left->type == EXPR_BINOP)
match_one_side(expr->left, expr->pos, expr->op);
if (expr->right->type == EXPR_BINOP)
match_one_side(expr->right, expr->pos, expr->op);
}
void check_macros(int id)
{
my_id = id;
add_hook(&match_join, BINOP_HOOK);
add_hook(&match_join, LOGIC_HOOK);
}