New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 21689 - missing-field-initializers too aggressive for C's {0}
Summary: missing-field-initializers too aggressive for C's {0}
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: Frontend (show other bugs)
Version: trunk
Hardware: PC All
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
: 32156 (view as bug list)
Depends on:
Blocks:
 
Reported: 2014-11-28 15:33 PST by Dan Albert
Modified: 2018-12-09 09:43 PST (History)
10 users (show)

See Also:
Fixed By Commit(s): r314499


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dan Albert 2014-11-28 15:33:23 PST
Clang will warn on `{0}` as -Wmissing-field-initializers even in C code. The language provides this to explicitly say "initialize everything to zero". Some discussion here: http://clang-developers.42468.n3.nabble.com/Wmissing-field-initializers-td3761260.html

I'm fine with John's solution of only relaxing this for C code.
Comment 1 Askar Safin 2015-02-06 18:15:36 PST
Same (fixed) bug in gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
Related bug in POSIX: http://austingroupbugs.net/view.php?id=918

And I think this bug should be fixed even in C++, just because we should have some uniform way to initialize addrinfo and mbstate_t in both C and C++
Comment 2 Bob Wilson 2016-02-04 00:27:19 PST
This is also tracked as rdar://problem/24498474
Comment 3 Simon Marchi 2017-12-04 06:59:04 PST
Here's an example of false positive in the real world:

/home/emaisin/src/binutils-gdb/bfd/elfnn-riscv.c:3145:36: error: missing field 'hi_addend' initializer [-Werror,-Wmissing-field-initializers]
  riscv_pcgp_hi_reloc hi_reloc = {0};
                                   ^

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=bfd/elfnn-riscv.c;h=47d2c477036c909c799b7f452d4acba2b8145977;hb=HEAD#l3145
Comment 4 Joshua Saxby 2018-01-08 07:55:26 PST
Here's an additional example of a false-positive I encountered in one of my own projects (when tested on Clang v5.0):

/home/travis/build/saxbophone/libsxbp/sxbp/begin_figure.c:150:34: error: 
      missing field 'y' initializer [-Werror,-Wmissing-field-initializers]
    sxbp_co_ord_t location = { 0 }; // where the end of the last line is
                                 ^
/home/travis/build/saxbophone/libsxbp/sxbp/begin_figure.c:151:32: error: 
      missing field 'x_max' initializer [-Werror,-Wmissing-field-initializers]
    sxbp_bounds_t bounds = { 0 }; // the bounds of the line traced so far

The code that triggers the warning can be found at:
https://github.com/saxbophone/libsxbp/blob/7e68075/sxbp/begin_figure.c#L150-L151

And a failing build showing that the bug occurs in Clang v5.0 but not in GCC v5.4:
https://travis-ci.org/saxbophone/libsxbp/builds/326179760
Comment 5 Peter Wu 2018-11-18 04:24:17 PST
This has been fixed with r314499 (https://reviews.llvm.org/D28148) which ended up in Clang 6 (and by extension, Xcode 10).

As a workaround, include -Wno-missing-field-initializers for older compilers:
- GCC < 4.7 (CMAKE_C_COMPILER_ID "GNU")
- Clang < 6.0
- AppleClang < 10.0
Comment 6 Peter Wu 2018-12-09 09:43:43 PST
*** Bug 32156 has been marked as a duplicate of this bug. ***