aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/g++.dg/plugin
diff options
context:
space:
mode:
authorBen Cheng <bccheng@google.com>2014-03-25 22:37:19 -0700
committerBen Cheng <bccheng@google.com>2014-03-25 22:37:19 -0700
commit1bc5aee63eb72b341f506ad058502cd0361f0d10 (patch)
treec607e8252f3405424ff15bc2d00aa38dadbb2518 /gcc-4.9/gcc/testsuite/g++.dg/plugin
parent283a0bf58fcf333c58a2a92c3ebbc41fb9eb1fdb (diff)
downloadtoolchain_gcc-1bc5aee63eb72b341f506ad058502cd0361f0d10.tar.gz
toolchain_gcc-1bc5aee63eb72b341f506ad058502cd0361f0d10.tar.bz2
toolchain_gcc-1bc5aee63eb72b341f506ad058502cd0361f0d10.zip
Initial checkin of GCC 4.9.0 from trunk (r208799).
Change-Id: I48a3c08bb98542aa215912a75f03c0890e497dba
Diffstat (limited to 'gcc-4.9/gcc/testsuite/g++.dg/plugin')
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/attribute_plugin-test-1.C16
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/attribute_plugin.c71
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/decl-plugin-test.C30
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/decl_plugin.c52
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/dumb-plugin-test-1.C53
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/dumb_plugin.c161
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/header-plugin-test.C3
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/header_plugin.c31
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/plugin.exp70
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/pragma_plugin-test-1.C18
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/pragma_plugin.c62
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-1.C50
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-2.C50
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-3.C50
-rw-r--r--gcc-4.9/gcc/testsuite/g++.dg/plugin/selfassign.c402
15 files changed, 1119 insertions, 0 deletions
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/attribute_plugin-test-1.C b/gcc-4.9/gcc/testsuite/g++.dg/plugin/attribute_plugin-test-1.C
new file mode 100644
index 000000000..1ca321d1f
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/attribute_plugin-test-1.C
@@ -0,0 +1,16 @@
+// { dg-warning "Callback to register attributes" "" { target *-*-* } 0 }
+
+void normal_func (char c, char c2);
+void normal_func (char __attribute__((user("param"))) c, char);
+void normal_func (char c, char __attribute__((user("param"))) c2)
+{
+} // { dg-warning "attribute 'user' on param 'c' of function normal_func" }
+// { dg-warning "attribute 'user' on param 'c2' of function normal_func" "" { target *-*-* } 7 }
+
+class Foo {
+ void method (char __attribute__((user("param"))) c);
+};
+
+void Foo::method(char c)
+{
+} // { dg-warning "attribute 'user' on param 'c' of function method" }
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/attribute_plugin.c b/gcc-4.9/gcc/testsuite/g++.dg/plugin/attribute_plugin.c
new file mode 100644
index 000000000..8de5f44cb
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/attribute_plugin.c
@@ -0,0 +1,71 @@
+/* Demonstrates how to add custom attributes */
+
+#include "gcc-plugin.h"
+#include <stdlib.h>
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "tree-pass.h"
+#include "intl.h"
+#include "toplev.h"
+#include "plugin.h"
+#include "diagnostic.h"
+
+int plugin_is_GPL_compatible;
+
+/* Attribute handler callback */
+
+static tree
+handle_user_attribute (tree *node, tree name, tree args,
+ int flags, bool *no_add_attrs)
+{
+ return NULL_TREE;
+}
+
+/* Attribute definition */
+
+static struct attribute_spec user_attr =
+ { "user", 1, 1, false, false, false, handle_user_attribute, false };
+
+/* Plugin callback called during attribute registration */
+
+static void
+register_attributes (void *event_data, void *data)
+{
+ warning (0, G_("Callback to register attributes"));
+ register_attribute (&user_attr);
+}
+
+/* Callback function to invoke before the function body is genericized. */
+
+void
+handle_pre_generic (void *event_data, void *data)
+{
+ tree fndecl = (tree) event_data;
+ tree arg;
+ for (arg = DECL_ARGUMENTS(fndecl); arg; arg = DECL_CHAIN (arg)) {
+ tree attr;
+ for (attr = DECL_ATTRIBUTES (arg); attr; attr = TREE_CHAIN (attr)) {
+ tree attrname = TREE_PURPOSE (attr);
+ tree attrargs = TREE_VALUE (attr);
+ warning (0, G_("attribute '%s' on param '%s' of function %s"),
+ IDENTIFIER_POINTER (attrname),
+ IDENTIFIER_POINTER (DECL_NAME (arg)),
+ IDENTIFIER_POINTER (DECL_NAME (fndecl))
+ );
+ }
+ }
+}
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+ struct plugin_gcc_version *version)
+{
+ const char *plugin_name = plugin_info->base_name;
+ register_callback (plugin_name, PLUGIN_PRE_GENERICIZE,
+ handle_pre_generic, NULL);
+
+ register_callback (plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
+ return 0;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/decl-plugin-test.C b/gcc-4.9/gcc/testsuite/g++.dg/plugin/decl-plugin-test.C
new file mode 100644
index 000000000..08a2ff2aa
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/decl-plugin-test.C
@@ -0,0 +1,30 @@
+extern int global; // { dg-warning "Decl Global global" }
+int global_array[] = { 1, 2, 3 }; // { dg-warning "Decl Global global_array" }
+
+int takes_args(int arg1, int arg2)
+{
+ int local = arg1 + arg2 + global; // { dg-warning "Decl Local local" }
+ return local + 1;
+}
+
+int global = 12; // { dg-warning "Decl Global global" }
+
+struct test_str {
+ int field; // { dg-warning "Decl Field field" }
+};
+
+class test_class {
+ int class_field1; // { dg-warning "Decl Field class_field1" }
+ int class_field2; // { dg-warning "Decl Field class_field2" }
+
+ test_class() // { dg-warning "Decl Function test_class" }
+ : class_field1(0), class_field2(0)
+ {}
+
+ void swap_fields(int bias) // { dg-warning "Decl Function swap_fields" }
+ {
+ int temp = class_field1 + bias; // { dg-warning "Decl Local temp" }
+ class_field1 = class_field2 - bias;
+ class_field2 = temp;
+ }
+};
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/decl_plugin.c b/gcc-4.9/gcc/testsuite/g++.dg/plugin/decl_plugin.c
new file mode 100644
index 000000000..d44c8f9d4
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/decl_plugin.c
@@ -0,0 +1,52 @@
+/* A plugin example that shows which declarations are caught by FINISH_DECL */
+
+#include "gcc-plugin.h"
+#include <stdlib.h>
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "tree-pass.h"
+#include "intl.h"
+#include "diagnostic.h"
+
+int plugin_is_GPL_compatible;
+
+/* Callback function to invoke after GCC finishes a declaration. */
+
+void plugin_finish_decl (void *event_data, void *data)
+{
+ tree decl = (tree) event_data;
+
+ const char *kind = NULL;
+ switch (TREE_CODE(decl)) {
+ case FUNCTION_DECL:
+ kind = "Function"; break;
+ case PARM_DECL:
+ kind = "Parameter"; break;
+ case VAR_DECL:
+ if (DECL_FILE_SCOPE_P(decl))
+ kind = "Global";
+ else
+ kind = "Local";
+ break;
+ case FIELD_DECL:
+ kind = "Field"; break;
+ default:
+ kind = "Unknown";
+ }
+
+ warning (0, G_("Decl %s %s"),
+ kind, IDENTIFIER_POINTER (DECL_NAME (decl)));
+}
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+ struct plugin_gcc_version *version)
+{
+ const char *plugin_name = plugin_info->base_name;
+
+ register_callback (plugin_name, PLUGIN_FINISH_DECL,
+ plugin_finish_decl, NULL);
+ return 0;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/dumb-plugin-test-1.C b/gcc-4.9/gcc/testsuite/g++.dg/plugin/dumb-plugin-test-1.C
new file mode 100644
index 000000000..70101c868
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/dumb-plugin-test-1.C
@@ -0,0 +1,53 @@
+// Test case for the dumb plugin.
+// { dg-do compile }
+// { dg-options "-O -fplugin-arg-dumb_plugin-ref-pass-name=ccp -fplugin-arg-dumb_plugin-ref-pass-instance-num=1" }
+
+class Foo {
+ private:
+ int a_;
+
+ public:
+ Foo() : a_(a_) {} // { dg-warning "Before genericizing function" }
+
+ void setA(int a) {
+ a_ = a_;
+ } // { dg-warning "Before genericizing function" }
+
+ void operator=(Foo& rhs) {
+ this->a_ = rhs.a_;
+ } // { dg-warning "Before genericizing function" }
+}; // { dg-warning "Process struct Foo" }
+
+struct Bar {
+ int b_;
+ int c_;
+}; // { dg-warning "Process struct Bar" }
+
+int g = g;
+Foo foo = foo;
+
+int func()
+{
+ Bar *bar1, bar2;
+ Foo local_foo;
+ int x = x;
+ static int y = y;
+ float *f;
+ Bar bar_array[5];
+ char n;
+ int overflow;
+
+ *f = *f;
+ bar1->b_ = bar1->b_;
+ bar2.c_ = bar2.c_;
+ local_foo = local_foo;
+ foo = foo;
+ foo.setA(5);
+ bar_array[3].c_ = bar_array[3].c_;
+ bar_array[x+g].b_ = bar_array[x+g].b_;
+ y = x;
+ x = y;
+} // { dg-warning "Before genericizing function" }
+
+// { dg-warning "Analyze function" "" { target *-*-* } 50 }
+// { dg-warning "End of compilation unit" "" { target *-*-* } 50 }
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/dumb_plugin.c b/gcc-4.9/gcc/testsuite/g++.dg/plugin/dumb_plugin.c
new file mode 100644
index 000000000..e197d6672
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/dumb_plugin.c
@@ -0,0 +1,161 @@
+/* A trivial (dumb) plugin example that shows how to use the GCC plugin
+ mechanism. */
+
+#include "gcc-plugin.h"
+#include <stdlib.h>
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "tree-pass.h"
+#include "intl.h"
+#include "toplev.h"
+#include "diagnostic.h"
+#include "context.h"
+
+int plugin_is_GPL_compatible;
+
+/* Callback function to invoke after GCC finishes parsing a struct. */
+
+void
+handle_struct (void *event_data, void *data)
+{
+ tree type = (tree) event_data;
+ warning (0, G_("Process struct %s"),
+ IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
+}
+
+/* Callback function to invoke before the function body is genericized. */
+
+void
+handle_pre_generic (void *event_data, void *data)
+{
+ tree fndecl = (tree) event_data;
+ warning (0, G_("Before genericizing function %s"),
+ IDENTIFIER_POINTER (DECL_NAME (fndecl)));
+}
+
+/* Callback function to invoke after GCC finishes the compilation unit. */
+
+void
+handle_end_of_compilation_unit (void *event_data, void *data)
+{
+ warning (0, G_("End of compilation unit"));
+}
+
+
+static unsigned int
+execute_dumb_plugin_example (void)
+{
+ warning (0, G_("Analyze function %s"),
+ IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
+ return 0;
+}
+
+static bool
+gate_dumb_plugin_example (void)
+{
+ return true;
+}
+
+namespace {
+
+const pass_data pass_data_dumb_plugin_example =
+{
+ GIMPLE_PASS, /* type */
+ "dumb_plugin_example", /* name */
+ OPTGROUP_NONE, /* optinfo_flags */
+ true, /* has_gate */
+ true, /* has_execute */
+ TV_NONE, /* tv_id */
+ PROP_cfg, /* properties_required */
+ 0, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ 0, /* todo_flags_finish */
+};
+
+class pass_dumb_plugin_example : public gimple_opt_pass
+{
+public:
+ pass_dumb_plugin_example(gcc::context *ctxt)
+ : gimple_opt_pass(pass_data_dumb_plugin_example, ctxt)
+ {}
+
+ /* opt_pass methods: */
+ bool gate () { return gate_dumb_plugin_example (); }
+ unsigned int execute () { return execute_dumb_plugin_example (); }
+
+}; // class pass_dumb_plugin_example
+
+} // anon namespace
+
+static gimple_opt_pass *
+make_pass_dumb_plugin_example (gcc::context *ctxt)
+{
+ return new pass_dumb_plugin_example (ctxt);
+}
+
+/* Initialization function that GCC calls. This plugin takes an argument
+ that specifies the name of the reference pass and an instance number,
+ both of which determine where the plugin pass should be inserted. */
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+ struct plugin_gcc_version *version)
+{
+ struct register_pass_info pass_info;
+ const char *plugin_name = plugin_info->base_name;
+ int argc = plugin_info->argc;
+ struct plugin_argument *argv = plugin_info->argv;
+ char *ref_pass_name = NULL;
+ int ref_instance_number = 0;
+ int i;
+
+ /* Process the plugin arguments. This plugin takes the following arguments:
+ ref-pass-name=<PASS_NAME> and ref-pass-instance-num=<NUM>. */
+ for (i = 0; i < argc; ++i)
+ {
+ if (!strcmp (argv[i].key, "ref-pass-name"))
+ {
+ if (argv[i].value)
+ ref_pass_name = argv[i].value;
+ else
+ warning (0, G_("option '-fplugin-arg-%s-ref-pass-name'"
+ " requires a pass name"), plugin_name);
+ }
+ else if (!strcmp (argv[i].key, "ref-pass-instance-num"))
+ {
+ if (argv[i].value)
+ ref_instance_number = strtol (argv[i].value, NULL, 0);
+ else
+ warning (0, G_("option '-fplugin-arg-%s-ref-pass-instance-num'"
+ " requires an integer value"), plugin_name);
+ }
+ else
+ warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
+ plugin_name, argv[i].key);
+ }
+
+ if (!ref_pass_name)
+ {
+ error (G_("plugin %qs requires a reference pass name"), plugin_name);
+ return 1;
+ }
+
+ pass_info.pass = make_pass_dumb_plugin_example (g);
+ pass_info.reference_pass_name = ref_pass_name;
+ pass_info.ref_pass_instance_number = ref_instance_number;
+ pass_info.pos_op = PASS_POS_INSERT_AFTER;
+
+ register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &pass_info);
+
+ register_callback (plugin_name, PLUGIN_FINISH_TYPE, handle_struct, NULL);
+
+ register_callback (plugin_name, PLUGIN_PRE_GENERICIZE,
+ handle_pre_generic, NULL);
+
+ register_callback (plugin_name, PLUGIN_FINISH_UNIT,
+ handle_end_of_compilation_unit, NULL);
+ return 0;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/header-plugin-test.C b/gcc-4.9/gcc/testsuite/g++.dg/plugin/header-plugin-test.C
new file mode 100644
index 000000000..bd6aff11f
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/header-plugin-test.C
@@ -0,0 +1,3 @@
+// Test case for the dumb plugin.
+// { dg-do compile }
+
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/header_plugin.c b/gcc-4.9/gcc/testsuite/g++.dg/plugin/header_plugin.c
new file mode 100644
index 000000000..a024194e1
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/header_plugin.c
@@ -0,0 +1,31 @@
+#include "gcc-plugin.h"
+#include <stdlib.h>
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "tree-pass.h"
+#include "intl.h"
+
+/* reqs */
+#include "tm.h"
+
+/* gcc/ headers. */
+#include "cp/cp-tree.h"
+#include "diagnostic.h"
+#include "c-family/c-common.h"
+#include "c-family/c-pretty-print.h"
+#include "tree-iterator.h"
+#include "plugin.h"
+#include "langhooks.h"
+#include "cp/cxx-pretty-print.h"
+#include "cp/name-lookup.h"
+
+int plugin_is_GPL_compatible;
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+ struct plugin_gcc_version *version)
+{
+ return 0;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/plugin.exp b/gcc-4.9/gcc/testsuite/g++.dg/plugin/plugin.exp
new file mode 100644
index 000000000..e97fb76a2
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/plugin.exp
@@ -0,0 +1,70 @@
+# Copyright (C) 2009-2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3. If not see
+# <http://www.gnu.org/licenses/>.
+
+# Test the functionality of the GCC plugin support
+
+load_lib target-supports.exp
+load_lib g++-dg.exp
+
+global TESTING_IN_BUILD_TREE
+global ENABLE_PLUGIN
+
+# The plugin testcases currently only work when the build tree is available.
+# Also check whether the host supports plugins.
+if { ![info exists TESTING_IN_BUILD_TREE] || ![info exists ENABLE_PLUGIN] } {
+ return
+}
+
+# If a testcase doesn't have special options, use these.
+global DEFAULT_CXXFLAGS
+if ![info exists DEFAULT_CXXFLAGS] then {
+ set DEFAULT_CXXFLAGS " -pedantic-errors -Wno-long-long"
+}
+
+# The procedures in plugin-support.exp need these parameters.
+set default_flags $DEFAULT_CXXFLAGS
+
+if $tracelevel then {
+ strace $tracelevel
+}
+
+# Load support procs.
+load_lib plugin-support.exp
+
+# Specify the plugin source file and the associated test files in a list.
+# plugin_test_list={ {plugin1 test1 test2 ...} {plugin2 test1 ...} ... }
+set plugin_test_list [list \
+ { attribute_plugin.c attribute_plugin-test-1.C } \
+ { pragma_plugin.c pragma_plugin-test-1.C } \
+ { selfassign.c self-assign-test-1.C self-assign-test-2.C self-assign-test-3.C } \
+ { dumb_plugin.c dumb-plugin-test-1.C } \
+ { header_plugin.c header-plugin-test.C } \
+ { decl_plugin.c decl-plugin-test.C } ]
+
+foreach plugin_test $plugin_test_list {
+ # Replace each source file with its full-path name
+ for {set i 0} {$i < [llength $plugin_test]} {incr i} {
+ set basename [lindex $plugin_test $i]
+ set plugin_test [lreplace $plugin_test $i $i $srcdir/$subdir/$basename]
+ }
+ set plugin_src [lindex $plugin_test 0]
+ # If we're only testing specific files and this isn't one of them, skip it.
+ if ![runtest_file_p $runtests $plugin_src] then {
+ continue
+ }
+ set plugin_input_tests [lreplace $plugin_test 0 0]
+ plugin-test-execute $plugin_src $plugin_input_tests
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/pragma_plugin-test-1.C b/gcc-4.9/gcc/testsuite/g++.dg/plugin/pragma_plugin-test-1.C
new file mode 100644
index 000000000..3c084208b
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/pragma_plugin-test-1.C
@@ -0,0 +1,18 @@
+// { dg-warning "Callback to register pragmas" "" { target *-*-* } 0 }
+
+int some_func (int c);
+
+#pragma GCCPLUGIN sayhello "here" // { dg-warning "'pragma GCCPLUGIN sayhello' outside of function: here" }
+
+int some_func (const char* s)
+{
+#pragma GCCPLUGIN sayhello "at start" // { dg-warning "'pragma GCCPLUGIN sayhello' from function 'some_func': at start" }
+
+#define DO_PRAGMA(x) _Pragma(#x)
+ if (!s)
+ {
+ DO_PRAGMA(GCCPLUGIN sayhello "in block"); // { dg-warning "'pragma GCCPLUGIN sayhello' from function 'some_func': in block" }
+ return 0;
+ }
+ return 1;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/pragma_plugin.c b/gcc-4.9/gcc/testsuite/g++.dg/plugin/pragma_plugin.c
new file mode 100644
index 000000000..940c302df
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/pragma_plugin.c
@@ -0,0 +1,62 @@
+/* Demonstrates how to add custom pragmas */
+
+#include "gcc-plugin.h"
+#include <stdlib.h>
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "rtl.h"
+#include "tree.h"
+#include "function.h"
+#include "c-family/c-pragma.h"
+#include "cpplib.h"
+#include "tree-pass.h"
+#include "intl.h"
+#include "toplev.h"
+#include "diagnostic.h"
+
+int plugin_is_GPL_compatible;
+
+
+/* handler of #pragma GCCPLUGIN sayhello "message" is quite similar to
+ handler of #pragma GCC message...*/
+
+static void
+handle_pragma_sayhello (cpp_reader *dummy)
+{
+ tree message = 0;
+ if (pragma_lex (&message) != CPP_STRING)
+ {
+ warning (OPT_Wpragmas, "%<#pragma GCCPLUGIN sayhello%> is not a string");
+ return;
+ }
+ if (TREE_STRING_LENGTH (message) > 1)
+ if (cfun)
+ warning (OPT_Wpragmas,
+ "%<pragma GCCPLUGIN sayhello%> from function %qE: %s",
+ cfun->decl, TREE_STRING_POINTER (message));
+ else
+ warning (OPT_Wpragmas,
+ "%<pragma GCCPLUGIN sayhello%> outside of function: %s",
+ TREE_STRING_POINTER (message));
+}
+
+/* Plugin callback called during pragma registration */
+
+static void
+register_my_pragma (void *event_data, void *data)
+{
+ warning (0, G_("Callback to register pragmas"));
+ c_register_pragma ("GCCPLUGIN", "sayhello", handle_pragma_sayhello);
+}
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+ struct plugin_gcc_version *version)
+{
+ const char *plugin_name = plugin_info->base_name;
+
+ register_callback (plugin_name, PLUGIN_PRAGMAS, register_my_pragma, NULL);
+ return 0;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-1.C b/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-1.C
new file mode 100644
index 000000000..607381fb4
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-1.C
@@ -0,0 +1,50 @@
+// Test the self-assignemnt detection plugin.
+// { dg-do compile }
+// { dg-options "-O" }
+
+class Foo {
+ private:
+ int a_;
+
+ public:
+ Foo() : a_(a_) {} // { dg-warning "assigned to itself" }
+
+ void setA(int a) {
+ a_ = a_; // { dg-warning "assigned to itself" }
+ }
+
+ void operator=(Foo& rhs) {
+ this->a_ = rhs.a_;
+ }
+};
+
+struct Bar {
+ int b_;
+ int c_;
+};
+
+int g = g; // { dg-warning "assigned to itself" }
+Foo foo = foo; // { dg-warning "assigned to itself" }
+
+int func()
+{
+ Bar *bar1, bar2;
+ Foo local_foo;
+ int x = x; // { dg-warning "assigned to itself" }
+ static int y = y; // { dg-warning "assigned to itself" }
+ float *f;
+ Bar bar_array[5];
+ char n;
+ int overflow;
+
+ *f = *f; // { dg-warning "assigned to itself" }
+ bar1->b_ = bar1->b_; // { dg-warning "assigned to itself" }
+ bar2.c_ = bar2.c_; // { dg-warning "assigned to itself" }
+ local_foo = local_foo; // { dg-warning "assigned to itself" }
+ foo = foo; // { dg-warning "assigned to itself" }
+ foo.setA(5);
+ bar_array[3].c_ = bar_array[3].c_; // { dg-warning "assigned to itself" }
+ bar_array[x+g].b_ = bar_array[x+g].b_; // { dg-warning "self-assignment detected" }
+ y = x;
+ x = y;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-2.C b/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-2.C
new file mode 100644
index 000000000..35e1fb8f8
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-2.C
@@ -0,0 +1,50 @@
+// Test the self-assignemnt detection plugin without checking of operator-eq.
+// { dg-do compile }
+// { dg-options "-O -fplugin-arg-selfassign-no-check-operator-eq" }
+
+class Foo {
+ private:
+ int a_;
+
+ public:
+ Foo() : a_(a_) {} // { dg-warning "assigned to itself" }
+
+ void setA(int a) {
+ a_ = a_; // { dg-warning "assigned to itself" }
+ }
+
+ void operator=(Foo& rhs) {
+ this->a_ = rhs.a_;
+ }
+};
+
+struct Bar {
+ int b_;
+ int c_;
+};
+
+int g = g; // { dg-warning "assigned to itself" }
+Foo foo = foo; // { dg-warning "assigned to itself" }
+
+int func()
+{
+ Bar *bar1, bar2;
+ Foo local_foo;
+ int x = x; // { dg-warning "assigned to itself" }
+ static int y = y; // { dg-warning "assigned to itself" }
+ float *f;
+ Bar bar_array[5];
+ char n;
+ int overflow;
+
+ *f = *f; // { dg-warning "assigned to itself" }
+ bar1->b_ = bar1->b_; // { dg-warning "assigned to itself" }
+ bar2.c_ = bar2.c_; // { dg-warning "assigned to itself" }
+ local_foo = local_foo; // { dg-bogus "assigned to itself" }
+ foo = foo; // { dg-bogus "assigned to itself" }
+ foo.setA(5);
+ bar_array[3].c_ = bar_array[3].c_; // { dg-warning "assigned to itself" }
+ bar_array[x+g].b_ = bar_array[x+g].b_; // { dg-warning "self-assignment detected" }
+ y = x;
+ x = y;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-3.C b/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-3.C
new file mode 100644
index 000000000..e5b354baf
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/self-assign-test-3.C
@@ -0,0 +1,50 @@
+// Test the self-assignemnt detection plugin with the 'disable' argument.
+// { dg-do compile }
+// { dg-options "-O -fplugin-arg-selfassign-disable" }
+
+class Foo {
+ private:
+ int a_;
+
+ public:
+ Foo() : a_(a_) {} // { dg-bogus "assigned to itself" }
+
+ void setA(int a) {
+ a_ = a_; // { dg-bogus "assigned to itself" }
+ }
+
+ void operator=(Foo& rhs) {
+ this->a_ = rhs.a_;
+ }
+};
+
+struct Bar {
+ int b_;
+ int c_;
+};
+
+int g = g; // { dg-bogus "assigned to itself" }
+Foo foo = foo; // { dg-bogus "assigned to itself" }
+
+int func()
+{
+ Bar *bar1, bar2;
+ Foo local_foo;
+ int x = x; // { dg-bogus "assigned to itself" }
+ static int y = y; // { dg-bogus "assigned to itself" }
+ float *f;
+ Bar bar_array[5];
+ char n;
+ int overflow;
+
+ *f = *f; // { dg-bogus "assigned to itself" }
+ bar1->b_ = bar1->b_; // { dg-bogus "assigned to itself" }
+ bar2.c_ = bar2.c_; // { dg-bogus "assigned to itself" }
+ local_foo = local_foo; // { dg-bogus "assigned to itself" }
+ foo = foo; // { dg-bogus "assigned to itself" }
+ foo.setA(5);
+ bar_array[3].c_ = bar_array[3].c_; // { dg-bogus "assigned to itself" }
+ bar_array[x+g].b_ = bar_array[x+g].b_; // { dg-bogus "self-assignment detected" }
+ y = x;
+ x = y;
+}
diff --git a/gcc-4.9/gcc/testsuite/g++.dg/plugin/selfassign.c b/gcc-4.9/gcc/testsuite/g++.dg/plugin/selfassign.c
new file mode 100644
index 000000000..041f25dce
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/g++.dg/plugin/selfassign.c
@@ -0,0 +1,402 @@
+/* This plugin contains an analysis pass that detects and warns about
+ self-assignment statements. */
+/* { dg-options "-O" } */
+
+#include "gcc-plugin.h"
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "tree.h"
+#include "stringpool.h"
+#include "toplev.h"
+#include "basic-block.h"
+#include "pointer-set.h"
+#include "hash-table.h"
+#include "vec.h"
+#include "ggc.h"
+#include "basic-block.h"
+#include "tree-ssa-alias.h"
+#include "internal-fn.h"
+#include "gimple-fold.h"
+#include "tree-eh.h"
+#include "gimple-expr.h"
+#include "is-a.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
+#include "tree.h"
+#include "tree-pass.h"
+#include "intl.h"
+#include "plugin-version.h"
+#include "diagnostic.h"
+#include "context.h"
+
+int plugin_is_GPL_compatible;
+
+/* Indicate whether to check overloaded operator '=', which is performed by
+ default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq. */
+bool check_operator_eq = true;
+
+/* Given a rhs EXPR of a gimple assign statement, if it is
+ - SSA_NAME : returns its var decl, or, if it is a temp variable,
+ returns the rhs of its SSA def statement.
+ - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
+ returns EXPR itself.
+ - any other expression : returns NULL_TREE. */
+
+static tree
+get_real_ref_rhs (tree expr)
+{
+ switch (TREE_CODE (expr))
+ {
+ case SSA_NAME:
+ {
+ /* Given a self-assign statement, say foo.x = foo.x,
+ the IR (after SSA) looks like:
+
+ D.1797_14 = foo.x;
+ foo.x ={v} D.1797_14;
+
+ So if the rhs EXPR is an SSA_NAME of a temp variable,
+ e.g. D.1797_14, we need to grab the rhs of its SSA def
+ statement (i.e. foo.x). */
+ tree vdecl = SSA_NAME_VAR (expr);
+ if ((!vdecl || DECL_ARTIFICIAL (vdecl))
+ && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
+ {
+ gimple def_stmt = SSA_NAME_DEF_STMT (expr);
+ /* We are only interested in an assignment with a single
+ rhs operand because if it is not, the original assignment
+ will not possibly be a self-assignment. */
+ if (gimple_assign_single_p (def_stmt))
+ return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
+ else
+ return NULL_TREE;
+ }
+ else
+ return vdecl;
+ }
+ case VAR_DECL:
+ case PARM_DECL:
+ case FIELD_DECL:
+ case COMPONENT_REF:
+ case MEM_REF:
+ case ARRAY_REF:
+ return expr;
+ default:
+ return NULL_TREE;
+ }
+}
+
+/* Given an expression tree, EXPR, that may contains SSA names, returns an
+ equivalent tree with the SSA names converted to var/parm/field decls
+ so that it can be used with '%E' format modifier when emitting warning
+ messages.
+
+ This function currently only supports VAR/PARM/FIELD_DECL, reference
+ expressions (COMPONENT_REF, INDIRECT_REF, ARRAY_REF), integer constant,
+ and SSA_NAME. If EXPR contains any other tree nodes (e.g. an arithmetic
+ expression appears in array index), NULL_TREE is returned. */
+
+static tree
+get_non_ssa_expr (tree expr)
+{
+ if (!expr)
+ return NULL_TREE;
+ switch (TREE_CODE (expr))
+ {
+ case VAR_DECL:
+ case PARM_DECL:
+ case FIELD_DECL:
+ {
+ if (DECL_NAME (expr))
+ return expr;
+ else
+ return NULL_TREE;
+ }
+ case COMPONENT_REF:
+ {
+ tree base, orig_base = TREE_OPERAND (expr, 0);
+ tree component, orig_component = TREE_OPERAND (expr, 1);
+ base = get_non_ssa_expr (orig_base);
+ if (!base)
+ return NULL_TREE;
+ component = get_non_ssa_expr (orig_component);
+ if (!component)
+ return NULL_TREE;
+ /* If either BASE or COMPONENT is converted, build a new
+ component reference tree. */
+ if (base != orig_base || component != orig_component)
+ return build3 (COMPONENT_REF, TREE_TYPE (component),
+ base, component, NULL_TREE);
+ else
+ return expr;
+ }
+ case MEM_REF:
+ {
+ tree orig_base = TREE_OPERAND (expr, 0);
+ if (TREE_CODE (orig_base) == SSA_NAME)
+ {
+ tree base = get_non_ssa_expr (orig_base);
+ if (!base)
+ return NULL_TREE;
+ return fold_build2 (MEM_REF, TREE_TYPE (expr),
+ base, TREE_OPERAND (expr, 1));
+ }
+ return expr;
+ }
+ case ARRAY_REF:
+ {
+ tree array, orig_array = TREE_OPERAND (expr, 0);
+ tree index, orig_index = TREE_OPERAND (expr, 1);
+ array = get_non_ssa_expr (orig_array);
+ if (!array)
+ return NULL_TREE;
+ index = get_non_ssa_expr (orig_index);
+ if (!index)
+ return NULL_TREE;
+ /* If either ARRAY or INDEX is converted, build a new array
+ reference tree. */
+ if (array != orig_array || index != orig_index)
+ return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
+ TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
+ else
+ return expr;
+ }
+ case SSA_NAME:
+ {
+ tree vdecl = SSA_NAME_VAR (expr);
+ if ((!vdecl || DECL_ARTIFICIAL (vdecl))
+ && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
+ {
+ gimple def_stmt = SSA_NAME_DEF_STMT (expr);
+ if (gimple_assign_single_p (def_stmt))
+ vdecl = gimple_assign_rhs1 (def_stmt);
+ }
+ return get_non_ssa_expr (vdecl);
+ }
+ case INTEGER_CST:
+ return expr;
+ default:
+ /* Return NULL_TREE for any other kind of tree nodes. */
+ return NULL_TREE;
+ }
+}
+
+/* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
+ they are the same. If so, print a warning message about self-assignment. */
+
+static void
+compare_and_warn (gimple stmt, tree lhs, tree rhs)
+{
+ if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
+ {
+ location_t location;
+ location = (gimple_has_location (stmt)
+ ? gimple_location (stmt)
+ : (DECL_P (lhs)
+ ? DECL_SOURCE_LOCATION (lhs)
+ : input_location));
+ /* If LHS contains any tree node not currently supported by
+ get_non_ssa_expr, simply emit a generic warning without
+ specifying LHS in the message. */
+ lhs = get_non_ssa_expr (lhs);
+ if (lhs)
+ warning_at (location, 0, G_("%qE is assigned to itself"), lhs);
+ else
+ warning_at (location, 0, G_("self-assignment detected"));
+ }
+}
+
+/* Check and warn if STMT is a self-assign statement. */
+
+static void
+warn_self_assign (gimple stmt)
+{
+ tree rhs, lhs;
+
+ /* Check assigment statement. */
+ if (gimple_assign_single_p (stmt))
+ {
+ rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
+ if (!rhs)
+ return;
+
+ lhs = gimple_assign_lhs (stmt);
+ if (TREE_CODE (lhs) == SSA_NAME)
+ {
+ lhs = SSA_NAME_VAR (lhs);
+ if (!lhs || DECL_ARTIFICIAL (lhs))
+ return;
+ }
+
+ compare_and_warn (stmt, lhs, rhs);
+ }
+ /* Check overloaded operator '=' (if enabled). */
+ else if (check_operator_eq && is_gimple_call (stmt))
+ {
+ tree fdecl = gimple_call_fndecl (stmt);
+ if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
+ {
+ /* If 'operator=' takes reference operands, the arguments will be
+ ADDR_EXPR trees. In this case, just remove the address-taken
+ operator before we compare the lhs and rhs. */
+ lhs = gimple_call_arg (stmt, 0);
+ if (TREE_CODE (lhs) == ADDR_EXPR)
+ lhs = TREE_OPERAND (lhs, 0);
+ rhs = gimple_call_arg (stmt, 1);
+ if (TREE_CODE (rhs) == ADDR_EXPR)
+ rhs = TREE_OPERAND (rhs, 0);
+
+ compare_and_warn (stmt, lhs, rhs);
+ }
+ }
+}
+
+/* Entry point for the self-assignment detection pass. */
+
+static unsigned int
+execute_warn_self_assign (void)
+{
+ gimple_stmt_iterator gsi;
+ basic_block bb;
+
+ FOR_EACH_BB_FN (bb, cfun)
+ {
+ for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+ warn_self_assign (gsi_stmt (gsi));
+ }
+
+ return 0;
+}
+
+/* Pass gate function. Currently always returns true. */
+
+static bool
+gate_warn_self_assign (void)
+{
+ return true;
+}
+
+namespace {
+
+const pass_data pass_data_warn_self_assign =
+{
+ GIMPLE_PASS, /* type */
+ "warn_self_assign", /* name */
+ OPTGROUP_NONE, /* optinfo_flags */
+ true, /* has_gate */
+ true, /* has_execute */
+ TV_NONE, /* tv_id */
+ PROP_ssa, /* properties_required */
+ 0, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ 0, /* todo_flags_finish */
+};
+
+class pass_warn_self_assign : public gimple_opt_pass
+{
+public:
+ pass_warn_self_assign(gcc::context *ctxt)
+ : gimple_opt_pass(pass_data_warn_self_assign, ctxt)
+ {}
+
+ /* opt_pass methods: */
+ bool gate () { return gate_warn_self_assign (); }
+ unsigned int execute () { return execute_warn_self_assign (); }
+
+}; // class pass_warn_self_assign
+
+} // anon namespace
+
+static gimple_opt_pass *
+make_pass_warn_self_assign (gcc::context *ctxt)
+{
+ return new pass_warn_self_assign (ctxt);
+}
+
+/* The initialization routine exposed to and called by GCC. The spec of this
+ function is defined in gcc/gcc-plugin.h.
+
+ PLUGIN_NAME - name of the plugin (useful for error reporting)
+ ARGC - the size of the ARGV array
+ ARGV - an array of key-value argument pair
+
+ Returns 0 if initialization finishes successfully.
+
+ Note that this function needs to be named exactly "plugin_init". */
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+ struct plugin_gcc_version *version)
+{
+ struct register_pass_info pass_info;
+ const char *plugin_name = plugin_info->base_name;
+ int argc = plugin_info->argc;
+ struct plugin_argument *argv = plugin_info->argv;
+ bool enabled = true;
+ int i;
+
+ if (!plugin_default_version_check (version, &gcc_version))
+ return 1;
+
+ /* Self-assign detection should happen after SSA is constructed. */
+ pass_info.pass = make_pass_warn_self_assign (g);
+ pass_info.reference_pass_name = "ssa";
+ pass_info.ref_pass_instance_number = 1;
+ pass_info.pos_op = PASS_POS_INSERT_AFTER;
+
+ /* Process the plugin arguments. This plugin takes the following arguments:
+ check-operator-eq, no-check-operator-eq, enable, and disable.
+ By default, the analysis is enabled with 'operator=' checked. */
+ for (i = 0; i < argc; ++i)
+ {
+ if (!strcmp (argv[i].key, "check-operator-eq"))
+ {
+ if (argv[i].value)
+ warning (0, G_("option '-fplugin-arg-%s-check-operator-eq=%s'"
+ " ignored (superfluous '=%s')"),
+ plugin_name, argv[i].value, argv[i].value);
+ else
+ check_operator_eq = true;
+ }
+ else if (!strcmp (argv[i].key, "no-check-operator-eq"))
+ {
+ if (argv[i].value)
+ warning (0, G_("option '-fplugin-arg-%s-no-check-operator-eq=%s'"
+ " ignored (superfluous '=%s')"),
+ plugin_name, argv[i].value, argv[i].value);
+ else
+ check_operator_eq = false;
+ }
+ else if (!strcmp (argv[i].key, "enable"))
+ {
+ if (argv[i].value)
+ warning (0, G_("option '-fplugin-arg-%s-enable=%s' ignored"
+ " (superfluous '=%s')"),
+ plugin_name, argv[i].value, argv[i].value);
+ else
+ enabled = true;
+ }
+ else if (!strcmp (argv[i].key, "disable"))
+ {
+ if (argv[i].value)
+ warning (0, G_("option '-fplugin-arg-%s-disable=%s' ignored"
+ " (superfluous '=%s')"),
+ plugin_name, argv[i].value, argv[i].value);
+ else
+ enabled = false;
+ }
+ else
+ warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
+ plugin_name, argv[i].key);
+ }
+
+ /* Register this new pass with GCC if the analysis is enabled. */
+ if (enabled)
+ register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
+ &pass_info);
+
+ return 0;
+}