Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- #include <set>
- #include <utility>
- #include <algorithm>
- // This is the first gcc header to be included
- #include "gcc-plugin.h"
- #include "plugin-version.h"
- #include "cp/cp-tree.h"
- #include "context.h"
- #include "function.h"
- #include "internal-fn.h"
- #include "is-a.h"
- #include "predict.h"
- #include "basic-block.h"
- #include "tree.h"
- #include "tree-ssa-alias.h"
- #include "gimple-expr.h"
- #include "gimple.h"
- #include "gimple-ssa.h"
- #include "tree-pretty-print.h"
- #include "tree-pass.h"
- #include "tree-ssa-operands.h"
- #include "tree-phinodes.h"
- #include "gimple-pretty-print.h"
- #include "gimple-iterator.h"
- #include "gimple-walk.h"
- #include "diagnostic.h"
- #include "stringpool.h"
- #include "attribs.h"
- #include "ssa-iterators.h"
- #include "print-tree.h"
- #include "gimple.h"
- // We must assert that this plugin is GPL compatible
- int plugin_is_GPL_compatible;
- static struct plugin_info ptr_assign_plugin_info =
- { "1.0", "Find pointer assign plugin" };
- namespace {
- const pass_data warn_unused_result_cxx_data =
- {
- GIMPLE_PASS,
- "ptr_assign_pass", /* name */
- OPTGROUP_NONE, /* optinfo_flags */
- TV_NONE, /* tv_id */
- PROP_gimple_any, /* properties_required */
- 0, /* properties_provided */
- 0, /* properties_destroyed */
- 0, /* todo_flags_start */
- 0 /* todo_flags_finish */
- };
- struct find_ptr_assign_cxx : gimple_opt_pass
- {
- find_ptr_assign_cxx(gcc::context *ctx)
- : gimple_opt_pass(warn_unused_result_cxx_data, ctx)
- {
- }
- static bool is_memref(tree type)
- {
- if (TREE_CODE(type) == MEM_REF)
- return true;
- return false;
- }
- static void add_ptr_ptrint_call(void)
- {
- }
- void instrument_ptr_writes(function *fun)
- {
- basic_block bb;
- FOR_ALL_BB_FN(bb, fun) {
- gimple_stmt_iterator gsi;
- for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
- //gimple stmt = gsi_stmt(gsi);
- gimple *stmt = gsi_stmt(gsi);
- switch (gimple_code(stmt)) {
- case GIMPLE_ASSIGN:
- {
- tree lhs = gimple_assign_lhs(stmt);
- //tree rhs1 = gimple_assign_rhs1(stmt);
- //std::cerr << "# Stmt:\n";
- //print_gimple_stmt (stderr, stmt, 0, 0);
- //std::cerr << "# Tree:\n";
- //debug_tree(lhs);
- //std::cerr << "\n";
- //break;
- if (lhs && is_memref(lhs)) {
- location_t loc = gimple_location(stmt);
- warning_at(loc, 0, "Pointer assign lhs");
- print_gimple_stmt (stderr, stmt, 0, 0);
- /* Add call */
- tree function_fn;
- tree function_fn_type;
- function_fn_type = build_function_type_list(
- /* return */ void_type_node,
- /* arg (ptr) */ const_ptr_type_node,
- NULL_TREE);
- function_fn = build_fn_decl("print_ptr",
- function_fn_type);
- TREE_PUBLIC(function_fn) = 1;
- gimple *call = gimple_build_call(function_fn, 1, lhs);
- gimple_stmt_iterator stmt_gsi = gsi_for_stmt(stmt);
- gsi_insert_before(&stmt_gsi, call, GSI_NEW_STMT);
- }
- break;
- }
- default:
- // Do nothing
- break;
- }
- }
- }
- }
- virtual unsigned int execute(function *fun) override
- {
- instrument_ptr_writes(fun);
- basic_block bb;
- FOR_ALL_BB_FN(bb, fun) {
- gimple_stmt_iterator gsi;
- for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
- //gimple stmt = gsi_stmt(gsi);
- gimple *stmt = gsi_stmt(gsi);
- switch (gimple_code(stmt)) {
- case GIMPLE_CALL:
- {
- tree current_fn_decl = gimple_call_fndecl(stmt);
- std::cerr << "# Fn Call Tree:\n";
- debug_tree(current_fn_decl);
- std::cerr << "\n";
- unsigned nargs = gimple_call_num_args(stmt);
- for (unsigned i = 0; i <nargs; i++) {
- tree arg = gimple_call_arg(stmt, i);
- }
- break;
- }
- default:
- // Do nothing
- break;
- }
- }
- }
- return 0;
- }
- virtual find_ptr_assign_cxx *clone() override
- {
- // Do not clone ourselves
- return this;
- }
- };
- }
- int plugin_init(struct plugin_name_args *plugin_info,
- struct plugin_gcc_version *version)
- {
- // We check the current gcc loading this plugin against the gcc we used to
- // created this plugin
- if (!plugin_default_version_check (version, &gcc_version))
- {
- std::cerr << "This GCC plugin is for version " << GCCPLUGIN_VERSION_MAJOR
- << "." << GCCPLUGIN_VERSION_MINOR << "\n";
- return 1;
- }
- std::cout << "Plugin successfully initialized\n";
- register_callback(plugin_info->base_name,
- /* event */ PLUGIN_INFO,
- /* callback */ NULL,
- /* user_data */ &ptr_assign_plugin_info);
- // Register the phase right after cfg
- struct register_pass_info pass_info;
- pass_info.pass = new find_ptr_assign_cxx(g); // "g" is a global gcc::context pointer
- pass_info.reference_pass_name = "cfg";
- //pass_info.reference_pass_name = "ssa";
- pass_info.ref_pass_instance_number = 1;
- pass_info.pos_op = PASS_POS_INSERT_AFTER;
- register_callback(plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
- &pass_info);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement