1 /* This plugin exercises diagnostic_metadata. */
3 #include "gcc-plugin.h"
9 #include "stringpool.h"
11 #include "basic-block.h"
12 #include "hash-table.h"
15 #include "basic-block.h"
16 #include "tree-ssa-alias.h"
17 #include "internal-fn.h"
19 #include "gimple-iterator.h"
20 #include "gimple-fold.h"
22 #include "gimple-expr.h"
25 #include "tree-pass.h"
27 #include "plugin-version.h"
28 #include "diagnostic.h"
30 #include "gcc-rich-location.h"
31 #include "diagnostic-metadata.h"
33 int plugin_is_GPL_compatible
;
35 const pass_data pass_data_test_metadata
=
37 GIMPLE_PASS
, /* type */
38 "test_metadata", /* name */
39 OPTGROUP_NONE
, /* optinfo_flags */
41 PROP_ssa
, /* properties_required */
42 0, /* properties_provided */
43 0, /* properties_destroyed */
44 0, /* todo_flags_start */
45 0, /* todo_flags_finish */
48 class pass_test_metadata
: public gimple_opt_pass
51 pass_test_metadata(gcc::context
*ctxt
)
52 : gimple_opt_pass(pass_data_test_metadata
, ctxt
)
55 /* opt_pass methods: */
56 bool gate (function
*) { return true; }
57 virtual unsigned int execute (function
*);
59 }; // class pass_test_metadata
61 /* Determine if STMT is a call with NUM_ARGS arguments to a function
63 If so, return STMT as a gcall *. Otherwise return NULL. */
66 check_for_named_call (gimple
*stmt
,
67 const char *funcname
, unsigned int num_args
)
69 gcc_assert (funcname
);
71 gcall
*call
= dyn_cast
<gcall
*> (stmt
);
75 tree fndecl
= gimple_call_fndecl (call
);
79 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl
)), funcname
))
82 if (gimple_call_num_args (call
) != num_args
)
84 error_at (stmt
->location
, "expected number of args: %i (got %i)",
85 num_args
, gimple_call_num_args (call
));
92 /* Exercise diagnostic_metadata. */
95 pass_test_metadata::execute (function
*fun
)
97 gimple_stmt_iterator gsi
;
100 FOR_EACH_BB_FN (bb
, fun
)
101 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
103 gimple
*stmt
= gsi_stmt (gsi
);
105 /* Example of CWE: complain about uses of gets. */
106 if (gcall
*call
= check_for_named_call (stmt
, "gets", 1))
108 gcc_rich_location
richloc (gimple_location (call
));
109 diagnostic_metadata m
;
111 /* CWE-242: Use of Inherently Dangerous Function. */
114 /* Example of a diagnostic_metadata::rule. */
115 diagnostic_metadata::precanned_rule
116 test_rule ("STR34-C", "https://example.com/");
117 m
.add_rule (test_rule
);
119 warning_meta (&richloc
, m
, 0,
120 "never use %qs", "gets");
128 plugin_init (struct plugin_name_args
*plugin_info
,
129 struct plugin_gcc_version
*version
)
131 struct register_pass_info pass_info
;
132 const char *plugin_name
= plugin_info
->base_name
;
133 int argc
= plugin_info
->argc
;
134 struct plugin_argument
*argv
= plugin_info
->argv
;
136 if (!plugin_default_version_check (version
, &gcc_version
))
139 pass_info
.pass
= new pass_test_metadata (g
);
140 pass_info
.reference_pass_name
= "ssa";
141 pass_info
.ref_pass_instance_number
= 1;
142 pass_info
.pos_op
= PASS_POS_INSERT_AFTER
;
143 register_callback (plugin_name
, PLUGIN_PASS_MANAGER_SETUP
, NULL
,