1 /* -Wnonnull-compare warning support.
2 Copyright (C) 2016-2020 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "tree-pass.h"
29 #include "diagnostic-core.h"
32 /* Warn about comparison of nonnull_arg_p argument initial values
36 do_warn_nonnull_compare (function
*fun
, tree arg
)
38 if (!POINTER_TYPE_P (TREE_TYPE (arg
))
39 && TREE_CODE (TREE_TYPE (arg
)) != OFFSET_TYPE
)
42 if (!nonnull_arg_p (arg
))
45 tree d
= ssa_default_def (fun
, arg
);
50 imm_use_iterator iter
;
52 FOR_EACH_IMM_USE_FAST (use_p
, iter
, d
)
54 gimple
*stmt
= USE_STMT (use_p
);
56 location_t loc
= gimple_location (stmt
);
57 if (gimple_code (stmt
) == GIMPLE_COND
)
58 switch (gimple_cond_code (stmt
))
62 if (gimple_cond_lhs (stmt
) == d
)
63 op
= gimple_cond_rhs (stmt
);
68 else if (is_gimple_assign (stmt
))
69 switch (gimple_assign_rhs_code (stmt
))
73 if (gimple_assign_rhs1 (stmt
) == d
)
74 op
= gimple_assign_rhs2 (stmt
);
77 switch (TREE_CODE (gimple_assign_rhs1 (stmt
)))
81 op
= gimple_assign_rhs1 (stmt
);
82 if (TREE_OPERAND (op
, 0) != d
)
87 loc
= EXPR_LOC_OR_LOC (op
, loc
);
88 op
= TREE_OPERAND (op
, 1);
98 && (POINTER_TYPE_P (TREE_TYPE (arg
))
99 ? integer_zerop (op
) : integer_minus_onep (op
))
100 && !gimple_no_warning_p (stmt
))
101 warning_at (loc
, OPT_Wnonnull_compare
,
102 "%<nonnull%> argument %qD compared to NULL", arg
);
108 const pass_data pass_data_warn_nonnull_compare
=
110 GIMPLE_PASS
, /* type */
111 "*nonnullcmp", /* name */
112 OPTGROUP_NONE
, /* optinfo_flags */
114 PROP_ssa
, /* properties_required */
115 0, /* properties_provided */
116 0, /* properties_destroyed */
117 0, /* todo_flags_start */
118 0, /* todo_flags_finish */
121 class pass_warn_nonnull_compare
: public gimple_opt_pass
124 pass_warn_nonnull_compare (gcc::context
*ctxt
)
125 : gimple_opt_pass (pass_data_warn_nonnull_compare
, ctxt
)
128 /* opt_pass methods: */
129 virtual bool gate (function
*) { return warn_nonnull_compare
; }
131 virtual unsigned int execute (function
*);
133 }; // class pass_warn_nonnull_compare
136 pass_warn_nonnull_compare::execute (function
*fun
)
138 if (fun
->static_chain_decl
)
139 do_warn_nonnull_compare (fun
, fun
->static_chain_decl
);
141 for (tree arg
= DECL_ARGUMENTS (cfun
->decl
); arg
; arg
= DECL_CHAIN (arg
))
142 do_warn_nonnull_compare (fun
, arg
);
149 make_pass_warn_nonnull_compare (gcc::context
*ctxt
)
151 return new pass_warn_nonnull_compare (ctxt
);