[cfe-users] Is there a way to make clang warn on this code?

David Blaikie dblaikie at gmail.com
Sat Mar 15 19:01:10 PDT 2014

On Fri, Mar 14, 2014 at 7:05 PM, Richard Trieu <rtrieu at google.com> wrote:
> Quentin,
> Clang does not have a warning for uninitialized fields in class methods.
> I've looked before to add a warning to flag any unused fields at the end of
> the a constructor.  However, there were good reasons to have uninitialized
> fields, such as having an Init() method later that initializes the fields,
> or a guard variable to protect against uninitialized use.  Also,
> -Wuninitialized typically warns on the use of an uninitialized variable, not
> the mere presence of one.
> The other idea is to do cross method analysis, either using the control flow
> of a program to determine which methods are used in which order, or testing
> methods against constructors.  Such an analysis would likely be too
> expensive, and would be more suited for static analysis.

We already have at least one cross-method analysis which could
actually be used as the basis for the warning under discussion here:

-Wunused-member-variable. This warning only fires if there are no
friends and a private member in a situation where all member functions
are defined in one translation unit.  So we could use this to show
that there's no initialization of the variable anywhere, but there are
uses of it. (essentially the same as -Wunused-member-variable, except
allow reads as well)

> So there's no warning for this yet, and probably won't be unless we find a
> better way to detect it.
> Richard
> On Fri, Mar 14, 2014 at 5:15 PM, Eric Christopher <echristo at gmail.com>
> wrote:
>> Adding Richard explicitly here. :)
>> On Mar 14, 2014 4:32 PM, "Quentin Colombet" <qcolombet at apple.com> wrote:
>>> Hi,
>>> I recently found a non-deterministic behavior in my application that was
>>> due to a missing field initialization.
>>> Here is a snippet of the actual problem:
>>> struct foo {
>>>   foo() {}; // <-- bar is not initialized here.
>>>   int getBar() const {
>>>     return bar; // <-- this returns an uninitialized value.
>>>   }
>>> private:
>>>   int bar;
>>> };
>>> I've compiled the attached cpp file that contains this snippet with -Wall
>>> -Wextra -Wuninitialized, but no warning is issued (trunk 203678).
>>> Is there an option to make clang warn about that?
>>> If not, should I file a PR?
>>> Thanks,
>>> -Quentin
>>> _______________________________________________
>>> cfe-users mailing list
>>> cfe-users at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users
> _______________________________________________
> cfe-users mailing list
> cfe-users at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users

More information about the cfe-users mailing list