public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] correct -Wself-init diagnostic location
@ 2015-11-11 20:07 Martin Sebor
  0 siblings, 0 replies; only message in thread
From: Martin Sebor @ 2015-11-11 20:07 UTC (permalink / raw)
  To: Gcc Patch List; +Cc: Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 926 bytes --]

GCC warns for class members that are initialized with themselves,
and with c++/64667 fixed, this includes references.

Unfortunately, the diagnostic that's printed is inconsistent between
pointers and references, and other members. For the first two kinds,
it's missing the caret, and for others it points to the ctor rather
than to the member-initializer (see bellow).

The attached patch fixes it so that all such diagnostics consistently
point to the member-initializer, and adds a test to verify gcc does
so.

Martin

struct S {
     int m;
     int &r;
     int *p;
     S ():
     m (m),
     r (r),
     p (p)
     { }
};
u.cpp: In constructor ‘S::S()’:
u.cpp:5:5: warning: ‘S::m’ is initialized with itself [-Winit-self]
      S ():
      ^

u.cpp:5:5: warning: ‘S::r’ is initialized with itself [-Winit-self]
u.cpp:5:5: warning: ‘S::p’ is initialized with itself [-Winit-self]

[-- Attachment #2: gcc-68208.patch --]
[-- Type: text/x-patch, Size: 1531 bytes --]

gcc/cp/
2015-11-11  Martin Sebor  <msebor@redhat.com>

	PR c++/68208
	* init.c (perform_member_init): Use location of member-initializer
	in -Wself-init rather than that of the ctor.

gcc/testsuite/
2015-11-11  Martin Sebor  <msebor@redhat.com>

	PR c++/68208
	* b/gcc/testsuite/g++.dg/warn/Winit-self-4.C: New test.

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 2e11acb..055e9d9 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -638,7 +638,7 @@ perform_member_init (tree member, tree init)
 	val = TREE_OPERAND (val, 0);
       if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
 	  && TREE_OPERAND (val, 0) == current_class_ref)
-	warning_at (DECL_SOURCE_LOCATION (current_function_decl),
+	warning_at (EXPR_LOC_OR_LOC (val, input_location),
 		    OPT_Winit_self, "%qD is initialized with itself",
 		    member);
     }
diff --git a/gcc/testsuite/g++.dg/warn/Winit-self-4.C b/gcc/testsuite/g++.dg/warn/Winit-self-4.C
new file mode 100644
index 0000000..0e61abf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Winit-self-4.C
@@ -0,0 +1,16 @@
+// PR c++/68208
+// { dg-options "-Winit-self" }
+
+// Verify that -Winit-self warning issued for reference and pointer
+// members points to the member-initializer and not to the constructor.
+struct S
+{
+    int i;
+    int *p;
+    int &r;
+    S ():
+        i (i),    // { dg-warning "initialized with itself" }
+        p (p),    // { dg-warning "initialized with itself" }
+        r (r)     // { dg-warning "initialized with itself" }
+        { }
+};

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-11-11 20:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-11 20:07 [PATCH] correct -Wself-init diagnostic location Martin Sebor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).