2 * smatch/check_macros.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 static int same_pos(struct position
*pos1
, struct position
*pos2
)
16 if (pos1
->stream
!= pos2
->stream
)
18 if (pos1
->line
!= pos2
->line
)
20 if (pos1
->pos
!= pos2
->pos
)
25 static void match_inside(struct expression
*expr
, struct position
*pos
)
30 if (same_pos(&expr
->pos
, pos
))
32 if (same_pos(&expr
->unop
->pos
, pos
))
36 name
= get_macro_name(pos
);
39 sm_msg("warn: the '%s' macro might need parens", name
);
42 static void match_one_side(struct expression
*expr
, struct position
*pos
, int op
)
47 if ((op
== '+' || op
== '*' || op
== '|' || op
== '&') && expr
->op
== op
)
49 if (same_pos(&expr
->right
->pos
, pos
))
51 if (same_pos(&expr
->left
->pos
, pos
))
55 name
= get_macro_name(pos
);
58 if (option_project
== PROJ_WINE
&& !strcmp("BEGIN", name
))
60 sm_msg("warn: the '%s' macro might need parens", name
);
63 static void match_join(struct expression
*expr
)
65 if (expr
->left
->type
== EXPR_PREOP
)
66 match_inside(expr
->left
, &expr
->pos
);
67 if (expr
->right
->type
== EXPR_POSTOP
)
68 match_inside(expr
->right
, &expr
->pos
);
70 if (expr
->left
->type
== EXPR_BINOP
)
71 match_one_side(expr
->left
, &expr
->pos
, expr
->op
);
72 if (expr
->right
->type
== EXPR_BINOP
)
73 match_one_side(expr
->right
, &expr
->pos
, expr
->op
);
76 void check_macros(int id
)
79 add_hook(&match_join
, BINOP_HOOK
);
80 add_hook(&match_join
, LOGIC_HOOK
);