[Bug c++/68859] New: Add a less strict/smarter version of -Wreorder

scovich at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri Dec 11 16:19:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68859

            Bug ID: 68859
           Summary: Add a less strict/smarter version of -Wreorder
           Product: gcc
           Version: 5.2.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: scovich at gmail dot com
  Target Milestone: ---

I am working with a large legacy code base that triggers a huge number of
warnings when compiled with -Wreorder (or -Wall, which enables it). 

I am not making any excuses for that code, but still it would be nice to have a
weaker-but-smarter variant of -Wreorder that only triggers when the
initialization order actually matters. For example, in the .cpp below, the
warning triggered by struct `definitely_bad` is helpful and identifies a real
bug. The warning for struct `not_a_problem`, on the other hand, is
significantly less interesting because each member is initialized completely
independently of the others (***). The middle example is evil because it calls
a member function of a partially constructed object, so I don't think it much
matters whether this smarter warning would trip or not in that case.

===== example.cpp ==========
struct definitely_bad {
    int val;
    int *ptr;
    definitely_bad(int *p) : ptr(p), val(*ptr) { }
};
struct bad_for_a_different_reason {
    int val;
    int *ptr;
    bad_for_a_different_reason(int *p)
        : ptr(p), val(do_something()) { }
    int do_something();
};
struct not_a_problem {
    int val;
    int *ptr;
    not_a_problem(int* p, int v) : ptr(p), val(v) { }
};
============================

Basically, I could imagine building a dependency graph that tracks which member
initializers depend on other members, and then trigger the warning only if the
true initialization order is not a valid partial order in that graph.

(***) I realize that members could have constructors with global side effects
(e.g. calls to printf or changes to global variables). I that case changing the
initialization order would still be observable, but this seems like a rare
enough case that the proposed warning could ignore it (leaving the existing
-Wreorder to flag it if the user desires).


More information about the Gcc-bugs mailing list