[cfe-dev] warn when ctor-initializer leaves uninitialized data?

Patrick Beard beard at apple.com
Fri Apr 27 11:38:51 PDT 2012

On Apr 23, 2012, at 11:33 AM, Patrick Beard <beard at apple.com> wrote:

> On Apr 10, 2012, at 1:22 PM, Evan Wallace wrote:
>> I guess the warning I was envisioning was more of a stylistic warning. The equivalent warning from gcc is -Weffc++ which includes warnings about violations of several style guidelines from Scott Meyers' book "Effective C++, Second Edition" including this warning (always put all members in the initializer list). It includes a few other warnings and is helpful in preventing mistakes, especially for beginners, but generates a lot of noise when run on large existing projects and unfortunately can't be split into separate, fine-grained warnings. The warning in this patch is much more targeted but makes the choice to reject some valid code instead of silently ignoring invalid code. It was more targeted for beginner to intermediate users of C++ and not for large, highly optimized projects. Maybe this wants to be two warnings, one for initializer lists as it is currently and one that attempts to trace uninitialized members to the end of each constructor but doesn't warn when it isn't sure?
> Having spent a little bit of time trying to track down a bug that was caused by unitialized POD data members, I'd also like to see your patch committed as an opt-in warning.

When I use this patch, it breaks one of the other tests:

FAIL: Clang :: SemaCXX/default-constructor-initializers.cpp (3644 of 4580)                                                      
******************** TEST 'Clang :: SemaCXX/default-constructor-initializers.cpp' FAILED ********************                   
/private/var/tmp/compiler.build/Release+Asserts/bin/clang -cc1 -internal-isystem /private/var/tmp/compiler.build/Release+Asserts
/bin/../lib/clang/3.2/include -fsyntax-only -verify /private/var/tmp/compiler/llvm/tools/clang/test/SemaCXX/default-constructor-
Exit Code: 1
Command Output (stderr):
error: 'note' diagnostics expected but not seen: 
  Line 54: first required here
1 error generated.

Here's the patch I'm using:

If I comment out the change to BuildImplicitMemberInitializer(), I confirm that the output of the default-constructor-initializers.cpp test differs by this line:

/Trees/compiler/llvm/tools/clang/test/SemaCXX/default-constructor-initializers.cpp:54:4: note: implicit default constructor for 'Z1' first required here
Z1 z1; // expected-note {{first required here}}

That is, this note appears if the change is commented out, but isn't there if:

  // Warn if ctor-initializer for a non-POD type leaves uninitialized data.
  if (!Constructor->getParent()->isPOD()) {
    SemaRef.Diag(Constructor->getLocation(), diag::warn_uninit_member)
      << Field->getDeclName();

is included.

- Patrick

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120427/52f5fbe6/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Wuninitialized-member.patch
Type: application/octet-stream
Size: 4200 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120427/52f5fbe6/attachment.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120427/52f5fbe6/attachment-0001.html>

More information about the cfe-dev mailing list