* PR c++/5645 gcc warns that pure virtual class not explicitly initialized @ 2007-10-27 18:03 Manuel López-Ibáñez 2007-11-04 23:41 ` Mark Mitchell 0 siblings, 1 reply; 13+ messages in thread From: Manuel López-Ibáñez @ 2007-10-27 18:03 UTC (permalink / raw) To: GCC Patches [-- Attachment #1: Type: text/plain, Size: 485 bytes --] :ADDPATCH c++: The patch below avoids this warning by checking for nearly emptiness. I am not sure that this is the correct approach for 100% cases, since I ignore all specific cases that should be warned and that shouldn't. Bootstrapped and regression tested. OK to commit? Cheers, Manuel. 2007-10-27 Manuel Lopez-Ibanez <manu@gcc.gnu.org> PR c++/5645 * init.c (emit_mem_initializers): Don't warn if the base class is nearly empty. testsuite/ * g++.dg/warn/pr5645.C: New. [-- Attachment #2: fix-pr5645.diff --] [-- Type: text/plain, Size: 1679 bytes --] Index: gcc/testsuite/g++.dg/warn/pr5645.C =================================================================== --- gcc/testsuite/g++.dg/warn/pr5645.C (revision 0) +++ gcc/testsuite/g++.dg/warn/pr5645.C (revision 0) @@ -0,0 +1,24 @@ +// PR5645: gcc warns that pure virtual class not explicitly initialized. +// { dg-do compile } +// { dg-options "-Wall -Wextra" } + +class a { +public: + virtual int f() = 0; + virtual int g() = 0; +}; + +class b : public a { +public: + b(); + b(const b& c); + +protected: + int i; +}; + +b::b() {} + +b::b(const b& c) { // { dg-bogus "base class .class a. should be explicitly initialized in the copy constructor" } + i = c.i; +} Index: gcc/cp/init.c =================================================================== --- gcc/cp/init.c (revision 128976) +++ gcc/cp/init.c (working copy) @@ -682,14 +682,15 @@ emit_mem_initializers (tree mem_inits) tree subobject = TREE_PURPOSE (mem_inits); tree arguments = TREE_VALUE (mem_inits); /* If these initializations are taking place in a copy constructor, the base class should probably be explicitly - initialized. */ + initialized unless it is nearly empty. */ if (extra_warnings && !arguments && DECL_COPY_CONSTRUCTOR_P (current_function_decl) - && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject))) + && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject)) + && !CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (subobject))) warning (OPT_Wextra, "%Jbase class %q#T should be explicitly initialized in the " "copy constructor", current_function_decl, BINFO_TYPE (subobject)); /* If an explicit -- but empty -- initializer list was present, ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-10-27 18:03 PR c++/5645 gcc warns that pure virtual class not explicitly initialized Manuel López-Ibáñez @ 2007-11-04 23:41 ` Mark Mitchell 2007-11-05 7:21 ` Nathan Sidwell ` (2 more replies) 0 siblings, 3 replies; 13+ messages in thread From: Mark Mitchell @ 2007-11-04 23:41 UTC (permalink / raw) To: Manuel López-Ibáñez Cc: GCC Patches, Nathan Sidwell, Jason Merrill Manuel López-Ibáñez wrote: > The patch below avoids this warning by checking for nearly emptiness. > I am not sure that this is the correct approach for 100% cases, since > I ignore all specific cases that should be warned and that shouldn't. > > Bootstrapped and regression tested. > > OK to commit? Nathan, Jason, are you agreed that PR c++/5645 is in fact a bug? This is one of these situations where we need to decide if we're warning about things that probably are a problem, or about things that could become a problem. In particular, if the base class has no data, then there's often no actual problem with not invoking the base class copy constructor -- but there certainly might be a problem, and, if the future, the base class were to get data, then there would likely be a problem. (I don't really have an opinion here; my personal coding style would probably be to put in the base class copy constructor call, and let the compiler optimize it away, just to future-proof my code.) Manuel, if we all agree that we don't want to warn in this situation, then I'd suggest that we check that the base class is nearly empty and that the base class copy constructor is not user-defined. I would think we would certainly want to warn about: struct base { base(const base& b); virtual void f() = 0; }; struct derived : public base { derived(const derived &d) {} }; because we don't know what the base class copy constructor does, but the fact that it is there suggests that it's author wants us to call it. -- Mark Mitchell CodeSourcery mark@codesourcery.com (650) 331-3385 x713 ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-04 23:41 ` Mark Mitchell @ 2007-11-05 7:21 ` Nathan Sidwell 2007-11-05 14:39 ` Jason Merrill 2007-11-12 1:33 ` Manuel López-Ibáñez 2 siblings, 0 replies; 13+ messages in thread From: Nathan Sidwell @ 2007-11-05 7:21 UTC (permalink / raw) To: Mark Mitchell Cc: Manuel López-Ibáñez, GCC Patches, Jason Merrill Mark Mitchell wrote: > Nathan, Jason, are you agreed that PR c++/5645 is in fact a bug? This > is one of these situations where we need to decide if we're warning > about things that probably are a problem, or about things that could > become a problem. In particular, if the base class has no data, then > there's often no actual problem with not invoking the base class copy > constructor -- but there certainly might be a problem, and, if the > future, the base class were to get data, then there would likely be a > problem. I think a nearly-empty base class should be {warned/not warned} about exactly the same way as an empty base class {is/is not}. > Manuel, if we all agree that we don't want to warn in this situation, > then I'd suggest that we check that the base class is nearly empty and > that the base class copy constructor is not user-defined. I would think > we would certainly want to warn about: > because we don't know what the base class copy constructor does, but the > fact that it is there suggests that it's author wants us to call it. I agree, iff we warn about such a constructor in the empty base class case. (if we don't then that may be an orthogonal bug) nathan -- Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-04 23:41 ` Mark Mitchell 2007-11-05 7:21 ` Nathan Sidwell @ 2007-11-05 14:39 ` Jason Merrill 2007-11-12 1:33 ` Manuel López-Ibáñez 2 siblings, 0 replies; 13+ messages in thread From: Jason Merrill @ 2007-11-05 14:39 UTC (permalink / raw) To: Mark Mitchell Cc: Manuel López-Ibáñez, GCC Patches, Nathan Sidwell Mark Mitchell wrote: > Manuel, if we all agree that we don't want to warn in this situation, > then I'd suggest that we check that the base class is nearly empty and > that the base class copy constructor is not user-defined. This makes sense to me. I'd still warn if there's any user-defined constructor, but not warn for the testcase in the PR. Jason ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-04 23:41 ` Mark Mitchell 2007-11-05 7:21 ` Nathan Sidwell 2007-11-05 14:39 ` Jason Merrill @ 2007-11-12 1:33 ` Manuel López-Ibáñez 2007-11-12 4:35 ` Mark Mitchell 2007-11-12 22:05 ` Jason Merrill 2 siblings, 2 replies; 13+ messages in thread From: Manuel López-Ibáñez @ 2007-11-12 1:33 UTC (permalink / raw) To: Mark Mitchell; +Cc: GCC Patches, Nathan Sidwell, Jason Merrill On 05/11/2007, Mark Mitchell <mark@codesourcery.com> wrote: > Manuel, if we all agree that we don't want to warn in this situation, > then I'd suggest that we check that the base class is nearly empty and > that the base class copy constructor is not user-defined. I would think > we would certainly want to warn about: > > struct base { > base(const base& b); > virtual void f() = 0; > }; > > struct derived : public base { > derived(const derived &d) {} > }; > > because we don't know what the base class copy constructor does, but the > fact that it is there suggests that it's author wants us to call it. That code doesn't actually compile: gcc/testsuite/g++.dg/warn/pr5645-2.C:13: error: no matching function for call to 'base::base()' gcc/testsuite/g++.dg/warn/pr5645-2.C:8: note: candidates are: base::base(const base&) derived::derived(const derived&) derived::derived(const derived&) And I am not sure how it is possible to check that the base class copy constructor is not user-defined. I wasn't able to find an appropriate function to do this. Cheers, Manu. ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-12 1:33 ` Manuel López-Ibáñez @ 2007-11-12 4:35 ` Mark Mitchell 2007-11-12 22:05 ` Jason Merrill 1 sibling, 0 replies; 13+ messages in thread From: Mark Mitchell @ 2007-11-12 4:35 UTC (permalink / raw) To: Manuel López-Ibáñez Cc: GCC Patches, Nathan Sidwell, Jason Merrill Manuel López-Ibáñez wrote: >> struct base { >> base(const base& b); >> virtual void f() = 0; >> }; >> >> struct derived : public base { >> derived(const derived &d) {} >> }; > That code doesn't actually compile: > > gcc/testsuite/g++.dg/warn/pr5645-2.C:13: error: no matching function > for call to 'base::base()' > gcc/testsuite/g++.dg/warn/pr5645-2.C:8: note: candidates are: > base::base(const base&) > derived::derived(const derived&) derived::derived(const derived&) Oh, that's interesting, yes. I always forget that copy constructors don't implicitly call copy constructors; they implicitly call default constructors. -- Mark Mitchell CodeSourcery mark@codesourcery.com (650) 331-3385 x713 ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-12 1:33 ` Manuel López-Ibáñez 2007-11-12 4:35 ` Mark Mitchell @ 2007-11-12 22:05 ` Jason Merrill 2007-11-13 6:17 ` Manuel López-Ibáñez 1 sibling, 1 reply; 13+ messages in thread From: Jason Merrill @ 2007-11-12 22:05 UTC (permalink / raw) To: Manuel López-Ibáñez Cc: Mark Mitchell, GCC Patches, Nathan Sidwell Manuel López-Ibáñez wrote: > And I am not sure how it is possible to check that the base class copy > constructor is not user-defined. I wasn't able to find an appropriate > function to do this. I was suggesting that we check for any user-defined constructor. Jason ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-12 22:05 ` Jason Merrill @ 2007-11-13 6:17 ` Manuel López-Ibáñez 2007-11-13 19:03 ` Jason Merrill 2008-02-12 15:24 ` Jason Merrill 0 siblings, 2 replies; 13+ messages in thread From: Manuel López-Ibáñez @ 2007-11-13 6:17 UTC (permalink / raw) To: Jason Merrill; +Cc: Mark Mitchell, GCC Patches, Nathan Sidwell On 12/11/2007, Jason Merrill <jason@redhat.com> wrote: > Manuel López-Ibáñez wrote: > > And I am not sure how it is possible to check that the base class copy > > constructor is not user-defined. I wasn't able to find an appropriate > > function to do this. > > I was suggesting that we check for any user-defined constructor. > My question was: is there any function to find out that? For this testcase: class a { public: virtual int f() = 0; virtual int g() = 0; }; class b : public a { public: b(); b(const b& c); protected: int i; }; b::b() {} b::b(const b& c) { // { dg-bogus "base class .class a. should be explicitly initialized in the copy constructor" } i = c.i; } In init.c (emit_mem_initializers) a subobject is a BINFO of the CURRENT_CLASS_TYPE, then: TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (subobject)) is 1. TYPE_HAS_COMPLEX_INIT_REF (BINFO_TYPE (subobject)) is 1. TYPE_HAS_COMPLEX_DFLT (BINFO_TYPE (subobject)) Is 1. which is not what I expected from those functions. I guess I am misunderstanding something here. Cheers, Manuel. ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-13 6:17 ` Manuel López-Ibáñez @ 2007-11-13 19:03 ` Jason Merrill 2007-11-14 0:22 ` Manuel López-Ibáñez 2008-02-12 15:24 ` Jason Merrill 1 sibling, 1 reply; 13+ messages in thread From: Jason Merrill @ 2007-11-13 19:03 UTC (permalink / raw) To: Manuel López-Ibáñez Cc: Mark Mitchell, GCC Patches, Nathan Sidwell Manuel López-Ibáñez wrote: > TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (subobject)) is 1. That seems wrong to me. That's being set in add_implicitly_declared_members, but I don't see why; the macro is supposed to indicate whether or not there are any user-declared constructors. What happens if you remove the line that sets it for the implicit copy ctor? Jason ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-13 19:03 ` Jason Merrill @ 2007-11-14 0:22 ` Manuel López-Ibáñez 2007-11-14 22:56 ` Jason Merrill 0 siblings, 1 reply; 13+ messages in thread From: Manuel López-Ibáñez @ 2007-11-14 0:22 UTC (permalink / raw) To: Jason Merrill; +Cc: Mark Mitchell, GCC Patches, Nathan Sidwell On 13/11/2007, Jason Merrill <jason@redhat.com> wrote: > Manuel López-Ibáñez wrote: > > TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (subobject)) is 1. > > That seems wrong to me. That's being set in > add_implicitly_declared_members, but I don't see why; the macro is > supposed to indicate whether or not there are any user-declared constructors That is not a recent thing: 22869 mmitchel /* Copy constructor. */ 28677 mmitchel if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t)) 22869 mmitchel { 84851 mmitchel TYPE_HAS_INIT_REF (t) = 1; 84851 mmitchel TYPE_HAS_CONST_INIT_REF (t) = !cant_have_const_cctor; 84851 mmitchel CLASSTYPE_LAZY_COPY_CTOR (t) = 1; 84851 mmitchel TYPE_HAS_CONSTRUCTOR (t) = 1; 22869 mmitchel } > What happens if you remove the line that sets it for the > implicit copy ctor? > Bootstrap breaks badly: /bin/sh ../libtool --tag CXX --mode=compile /home/manuel/130148M/build/./gcc/xgcc -shared-libgcc -B/home/manuel/130148M/build/./gcc -nostdinc++ -L/home/manuel/130148M/build/x86_64-unknown-linux-gnu/libstdc++-v3/src -L/home/manuel/130148M/build/x86_64-unknown-linux-gnu/libstdc++-v3/src/.libs -B/home/manuel/./130148M/install/x86_64-unknown-linux-gnu/bin/ -B/home/manuel/./130148M/install/x86_64-unknown-linux-gnu/lib/ -isystem /home/manuel/./130148M/install/x86_64-unknown-linux-gnu/include -isystem /home/manuel/./130148M/install/x86_64-unknown-linux-gnu/sys-include -I/home/manuel/130148M/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/x86_64-unknown-linux-gnu -I/home/manuel/130148M/build/x86_64-unknown-linux-gnu/libstdc++-v3/include -I/home/manuel/src/trunk/libstdc++-v3/libsupc++ -fno-implicit-templates -Wall -Wextra -Wwrite-strings -Wcast-qual -fdiagnostics-show-location=once -ffunction-sections -fdata-sections -g -O2 -D_GNU_SOURCE -c -o codecvt_members.lo codecvt_members.cc libtool: compile: /home/manuel/130148M/build/./gcc/xgcc -shared-libgcc -B/home/manuel/130148M/build/./gcc -nostdinc++ -L/home/manuel/130148M/build/x86_64-unknown-linux-gnu/libstdc++-v3/src -L/home/manuel/130148M/build/x86_64-unknown-linux-gnu/libstdc++-v3/src/.libs -B/home/manuel/./130148M/install/x86_64-unknown-linux-gnu/bin/ -B/home/manuel/./130148M/install/x86_64-unknown-linux-gnu/lib/ -isystem /home/manuel/./130148M/install/x86_64-unknown-linux-gnu/include -isystem /home/manuel/./130148M/install/x86_64-unknown-linux-gnu/sys-include -I/home/manuel/130148M/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/x86_64-unknown-linux-gnu -I/home/manuel/130148M/build/x86_64-unknown-linux-gnu/libstdc++-v3/include -I/home/manuel/src/trunk/libstdc++-v3/libsupc++ -fno-implicit-templates -Wall -Wextra -Wwrite-strings -Wcast-qual -fdiagnostics-show-location=once -ffunction-sections -fdata-sections -g -O2 -D_GNU_SOURCE -c codecvt_members.cc -fPIC -DPIC -o .libs/codecvt_members.o codecvt_members.cc: In member function 'virtual std::codecvt_base::result std::codecvt<wchar_t, char, __mbstate_t>::do_out(mbstate_t&, const wchar_t*, const wchar_t*, const wchar_t*&, char*, char*, char*&) const': codecvt_members.cc:56: error: constructor syntax used, but no constructor declared for type 'mbstate_t' codecvt_members.cc:56: error: cannot convert '__mbstate_t' to 'int' in initialization codecvt_members.cc:56: warning: missing initializer for member '__mbstate_t::__value' codecvt_members.cc: In member function 'virtual std::codecvt_base::result std::codecvt<wchar_t, char, __mbstate_t>::do_in(mbstate_t&, const char*, const char*, const char*&, wchar_t*, wchar_t*, wchar_t*&) const': codecvt_members.cc:121: error: constructor syntax used, but no constructor declared for type 'mbstate_t' codecvt_members.cc:121: error: cannot convert '__mbstate_t' to 'int' in initialization codecvt_members.cc:121: warning: missing initializer for member '__mbstate_t::__value' codecvt_members.cc: In member function 'virtual int std::codecvt<wchar_t, char, __mbstate_t>::do_length(mbstate_t&, const char*, const char*, size_t) const': codecvt_members.cc:190: error: constructor syntax used, but no constructor declared for type 'mbstate_t' codecvt_members.cc:190: error: cannot convert '__mbstate_t' to 'int' in initialization codecvt_members.cc:190: warning: missing initializer for member '__mbstate_t::__value' Ideas? Manu. ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-14 0:22 ` Manuel López-Ibáñez @ 2007-11-14 22:56 ` Jason Merrill 0 siblings, 0 replies; 13+ messages in thread From: Jason Merrill @ 2007-11-14 22:56 UTC (permalink / raw) To: Manuel López-Ibáñez Cc: Mark Mitchell, GCC Patches, Nathan Sidwell Manuel López-Ibáñez wrote: >> What happens if you remove the line that sets it for the >> implicit copy ctor? > > Bootstrap breaks badly: >[...] > Ideas? Change code that checks TYPE_HAS_CONSTRUCTOR to use TYPE_NEEDS_CONSTRUCTING instead? Jason ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2007-11-13 6:17 ` Manuel López-Ibáñez 2007-11-13 19:03 ` Jason Merrill @ 2008-02-12 15:24 ` Jason Merrill 2008-02-13 11:42 ` Manuel López-Ibáñez 1 sibling, 1 reply; 13+ messages in thread From: Jason Merrill @ 2008-02-12 15:24 UTC (permalink / raw) To: Manuel López-Ibáñez Cc: Mark Mitchell, GCC Patches, Nathan Sidwell Manuel López-Ibáñez wrote: > On 12/11/2007, Jason Merrill <jason@redhat.com> wrote: >> I was suggesting that we check for any user-defined constructor. > > My question was: is there any function to find out that? There is now, TYPE_HAS_USER_CONSTRUCTOR. Jason ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: PR c++/5645 gcc warns that pure virtual class not explicitly initialized 2008-02-12 15:24 ` Jason Merrill @ 2008-02-13 11:42 ` Manuel López-Ibáñez 0 siblings, 0 replies; 13+ messages in thread From: Manuel López-Ibáñez @ 2008-02-13 11:42 UTC (permalink / raw) To: Jason Merrill; +Cc: Mark Mitchell, GCC Patches, Nathan Sidwell On 12/02/2008, Jason Merrill <jason@redhat.com> wrote: > Manuel López-Ibáñez wrote: > > On 12/11/2007, Jason Merrill <jason@redhat.com> wrote: > >> I was suggesting that we check for any user-defined constructor. > > > > My question was: is there any function to find out that? > > There is now, TYPE_HAS_USER_CONSTRUCTOR. > I posted the following comment in PR 5645 [1] Created an attachment (id=15136) [edit] patch and testcases This patch contains an attempt to implement the suggestions given here: http://gcc.gnu.org/ml/gcc-patches/2007-11/msg00149.html It also contains all testcases from PR 5645 and PR 11159. The tests in g++.dg/warn/pr5645.C pass. The tests in g++.dg/warn/pr11159.C fail. The fundamental problem is that I don't understand what this warning is warning about. And nobody seems to have a clear idea either. Unless someone can put forward a clear definition that is justified for the testcases above and provides new testcases (either positive or negative), I am not going to work anymore on this (except if asked to remove the warning altogether). [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=5645#c9 ^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2008-02-13 11:29 UTC | newest] Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2007-10-27 18:03 PR c++/5645 gcc warns that pure virtual class not explicitly initialized Manuel López-Ibáñez 2007-11-04 23:41 ` Mark Mitchell 2007-11-05 7:21 ` Nathan Sidwell 2007-11-05 14:39 ` Jason Merrill 2007-11-12 1:33 ` Manuel López-Ibáñez 2007-11-12 4:35 ` Mark Mitchell 2007-11-12 22:05 ` Jason Merrill 2007-11-13 6:17 ` Manuel López-Ibáñez 2007-11-13 19:03 ` Jason Merrill 2007-11-14 0:22 ` Manuel López-Ibáñez 2007-11-14 22:56 ` Jason Merrill 2008-02-12 15:24 ` Jason Merrill 2008-02-13 11:42 ` Manuel López-Ibáñez
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).