| 1 |
/* { dg-options "-O" } */ |
| 2 |
|
| 3 |
#include "gcc-plugin.h" |
| 4 |
#include "config.h" |
| 5 |
#include "system.h" |
| 6 |
#include "coretypes.h" |
| 7 |
#include "tm.h" |
| 8 |
#include "tree.h" |
| 9 |
#include "stringpool.h" |
| 10 |
#include "toplev.h" |
| 11 |
#include "basic-block.h" |
| 12 |
#include "hash-table.h" |
| 13 |
#include "vec.h" |
| 14 |
#include "ggc.h" |
| 15 |
#include "basic-block.h" |
| 16 |
#include "tree-ssa-alias.h" |
| 17 |
#include "internal-fn.h" |
| 18 |
#include "gimple.h" |
| 19 |
#include "gimple-iterator.h" |
| 20 |
#include "gimple-fold.h" |
| 21 |
#include "tree-eh.h" |
| 22 |
#include "gimple-expr.h" |
| 23 |
#include "is-a.h" |
| 24 |
#include "tree.h" |
| 25 |
#include "tree-pass.h" |
| 26 |
#include "intl.h" |
| 27 |
#include "plugin-version.h" |
| 28 |
#include "c-family/c-common.h" |
| 29 |
#include "diagnostic.h" |
| 30 |
#include "diagnostic-format-text.h" |
| 31 |
#include "context.h" |
| 32 |
#include "make-unique.h" |
| 33 |
|
| 34 |
int plugin_is_GPL_compatible; |
| 35 |
|
| 36 |
/* A custom pass for emitting dummy warnings from the middle-end. */ |
| 37 |
|
| 38 |
const pass_data pass_data_test_groups = |
| 39 |
{ |
| 40 |
GIMPLE_PASS, /* type */ |
| 41 |
"test_groups", /* name */ |
| 42 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 43 |
TV_NONE, /* tv_id */ |
| 44 |
PROP_ssa, /* properties_required */ |
| 45 |
0, /* properties_provided */ |
| 46 |
0, /* properties_destroyed */ |
| 47 |
0, /* todo_flags_start */ |
| 48 |
0, /* todo_flags_finish */ |
| 49 |
}; |
| 50 |
|
| 51 |
class pass_test_groups : public gimple_opt_pass |
| 52 |
{ |
| 53 |
public: |
| 54 |
pass_test_groups(gcc::context *ctxt) |
| 55 |
: gimple_opt_pass(pass_data_test_groups, ctxt) |
| 56 |
{} |
| 57 |
|
| 58 |
/* opt_pass methods: */ |
| 59 |
bool gate (function *) { return true; } |
| 60 |
virtual unsigned int execute (function *); |
| 61 |
|
| 62 |
}; // class pass_test_groups |
| 63 |
|
| 64 |
/* Determine if STMT is a call with NUM_ARGS arguments to a function |
| 65 |
named FUNCNAME. |
| 66 |
If so, return STMT as a gcall *. Otherwise return NULL. */ |
| 67 |
|
| 68 |
static gcall * |
| 69 |
check_for_named_call (gimple *stmt, |
| 70 |
const char *funcname, unsigned int num_args) |
| 71 |
{ |
| 72 |
gcc_assert (funcname); |
| 73 |
|
| 74 |
gcall *call = dyn_cast <gcall *> (stmt); |
| 75 |
if (!call) |
| 76 |
return NULL; |
| 77 |
|
| 78 |
tree fndecl = gimple_call_fndecl (call); |
| 79 |
if (!fndecl) |
| 80 |
return NULL; |
| 81 |
|
| 82 |
if (strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), funcname)) |
| 83 |
return NULL; |
| 84 |
|
| 85 |
if (gimple_call_num_args (call) != num_args) |
| 86 |
{ |
| 87 |
error_at (stmt->location, "expected number of args: %i (got %i)", |
| 88 |
num_args, gimple_call_num_args (call)); |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
|
| 92 |
return call; |
| 93 |
} |
| 94 |
|
| 95 |
/* Emit a warning at LOC. */ |
| 96 |
|
| 97 |
static void |
| 98 |
emit_warning (location_t loc) |
| 99 |
{ |
| 100 |
source_range src_range = get_range_from_loc (line_table, loc); |
| 101 |
warning_at (loc, 0, "range %i:%i-%i:%i", |
| 102 |
LOCATION_LINE (src_range.m_start), |
| 103 |
LOCATION_COLUMN (src_range.m_start), |
| 104 |
LOCATION_LINE (src_range.m_finish), |
| 105 |
LOCATION_COLUMN (src_range.m_finish)); |
| 106 |
} |
| 107 |
|
| 108 |
/* Code for simulating the emission of a warning from the middle-end. |
| 109 |
Emit a warning for each call to a function named "__emit_warning". */ |
| 110 |
|
| 111 |
static void |
| 112 |
test_groups (gimple *stmt) |
| 113 |
{ |
| 114 |
gcall *call = check_for_named_call (stmt, "__emit_warning", 1); |
| 115 |
if (!call) |
| 116 |
return; |
| 117 |
|
| 118 |
/* We expect an ADDR_EXPR with a STRING_CST inside it for the |
| 119 |
initial arg. */ |
| 120 |
tree t_addr_string = gimple_call_arg (call, 0); |
| 121 |
if (TREE_CODE (t_addr_string) != ADDR_EXPR) |
| 122 |
{ |
| 123 |
error_at (call->location, "string literal required for arg 1"); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
tree t_string = TREE_OPERAND (t_addr_string, 0); |
| 128 |
if (TREE_CODE (t_string) != STRING_CST) |
| 129 |
{ |
| 130 |
error_at (call->location, "string literal required for arg 1"); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
{ |
| 135 |
auto_diagnostic_group d; |
| 136 |
if (warning_at (call->location, 0, "%s", call, |
| 137 |
TREE_STRING_POINTER (t_string))) |
| 138 |
{ |
| 139 |
inform (call->location, "message for note"); |
| 140 |
inform (call->location, " some more detail"); |
| 141 |
inform (call->location, " yet more detail"); |
| 142 |
} |
| 143 |
} |
| 144 |
inform (call->location, "an unrelated message"); |
| 145 |
} |
| 146 |
|
| 147 |
/* Call test_groups on every statement within FUN. */ |
| 148 |
|
| 149 |
unsigned int |
| 150 |
pass_test_groups::execute (function *fun) |
| 151 |
{ |
| 152 |
gimple_stmt_iterator gsi; |
| 153 |
basic_block bb; |
| 154 |
|
| 155 |
FOR_EACH_BB_FN (bb, fun) |
| 156 |
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
| 157 |
{ |
| 158 |
gimple *stmt = gsi_stmt (gsi); |
| 159 |
test_groups (stmt); |
| 160 |
} |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
/* Custom diagnostic callback, to avoid having the path in the |
| 166 |
expected output. */ |
| 167 |
|
| 168 |
void |
| 169 |
test_diagnostic_text_starter (diagnostic_text_output_format &text_output, |
| 170 |
const diagnostic_info *diagnostic) |
| 171 |
{ |
| 172 |
pp_set_prefix (text_output.get_printer (), xstrdup ("PREFIX: ")); |
| 173 |
} |
| 174 |
|
| 175 |
/* Custom diagnostic callback, to avoid having the path in the |
| 176 |
expected output. */ |
| 177 |
|
| 178 |
void |
| 179 |
test_diagnostic_start_span_fn (const diagnostic_location_print_policy &, |
| 180 |
pretty_printer *pp, |
| 181 |
expanded_location) |
| 182 |
{ |
| 183 |
pp_string (pp, "START_SPAN_FN: "); |
| 184 |
pp_newline (pp); |
| 185 |
} |
| 186 |
|
| 187 |
/* Custom output format subclass. */ |
| 188 |
|
| 189 |
class test_output_format : public diagnostic_text_output_format |
| 190 |
{ |
| 191 |
public: |
| 192 |
test_output_format (diagnostic_context &context) |
| 193 |
: diagnostic_text_output_format (context) |
| 194 |
{} |
| 195 |
|
| 196 |
void on_begin_group () final override |
| 197 |
{ |
| 198 |
/* Loudly announce a new diagnostic group. */ |
| 199 |
pretty_printer *const pp = get_printer (); |
| 200 |
pp_string (pp, |
| 201 |
"================================= BEGIN GROUP =============================="); |
| 202 |
pp_newline (pp); |
| 203 |
} |
| 204 |
void on_end_group () final override |
| 205 |
{ |
| 206 |
/* Loudly announce the end of a diagnostic group. */ |
| 207 |
pretty_printer *const pp = get_printer (); |
| 208 |
pp_set_prefix (pp, NULL); |
| 209 |
pp_string (pp, |
| 210 |
"---------------------------------- END GROUP -------------------------------"); |
| 211 |
pp_newline_and_flush (pp); |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
/* Entrypoint for the plugin. |
| 216 |
Install custom callbacks into the global_dc. |
| 217 |
Create and register the custom pass. */ |
| 218 |
|
| 219 |
int |
| 220 |
plugin_init (struct plugin_name_args *plugin_info, |
| 221 |
struct plugin_gcc_version *version) |
| 222 |
{ |
| 223 |
struct register_pass_info pass_info; |
| 224 |
const char *plugin_name = plugin_info->base_name; |
| 225 |
int argc = plugin_info->argc; |
| 226 |
struct plugin_argument *argv = plugin_info->argv; |
| 227 |
|
| 228 |
if (!plugin_default_version_check (version, &gcc_version)) |
| 229 |
return 1; |
| 230 |
|
| 231 |
diagnostic_text_starter (global_dc) = test_diagnostic_text_starter; |
| 232 |
diagnostic_start_span (global_dc) = test_diagnostic_start_span_fn; |
| 233 |
global_dc->set_output_format (::make_unique<test_output_format> (*global_dc)); |
| 234 |
|
| 235 |
pass_info.pass = new pass_test_groups (g); |
| 236 |
pass_info.reference_pass_name = "*warn_function_noreturn"; |
| 237 |
pass_info.ref_pass_instance_number = 1; |
| 238 |
pass_info.pos_op = PASS_POS_INSERT_AFTER; |
| 239 |
register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, |
| 240 |
&pass_info); |
| 241 |
|
| 242 |
return 0; |
| 243 |
} |