Bug 63567 - Linux kernel build error due to non-static initializers
Summary: Linux kernel build error due to non-static initializers
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: 5.0
Assignee: Marek Polacek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-10-16 22:46 UTC by Sasha Levin
Modified: 2014-10-19 16:50 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-10-16 00:00:00


Attachments
Preprocessed kernel/smpboot.c (126.17 KB, text/plain)
2014-10-18 17:53 UTC, Sasha Levin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sasha Levin 2014-10-16 22:46:41 UTC
Hi all,

I'm seeing a new build issue with the linux kernel:

  CC      kernel/smpboot.o
In file included from include/linux/mutex.h:15:0,
                 from include/linux/kernfs.h:13,
                 from include/linux/sysfs.h:15,
                 from include/linux/kobject.h:21,
                 from include/linux/device.h:17,
                 from include/linux/node.h:17,
                 from include/linux/cpu.h:16,
                 from kernel/smpboot.c:4:
include/linux/spinlock_types.h:82:2: error: initializer element is not constant
  (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  ^
include/linux/mutex.h:112:18: note: in expansion of macro ‘__SPIN_LOCK_UNLOCKED’
   , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
                  ^
include/linux/mutex.h:118:27: note: in expansion of macro ‘__MUTEX_INITIALIZER’
  struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
                           ^
kernel/smpboot.c:80:8: note: in expansion of macro ‘DEFINE_MUTEX’
 static DEFINE_MUTEX(smpboot_threads_lock);
        ^
include/linux/spinlock_types.h:82:2: note: (near initialization for ‘smpboot_threads_lock.wait_lock’)
  (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  ^
include/linux/mutex.h:112:18: note: in expansion of macro ‘__SPIN_LOCK_UNLOCKED’
   , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
                  ^
include/linux/mutex.h:118:27: note: in expansion of macro ‘__MUTEX_INITIALIZER’
  struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
                           ^
kernel/smpboot.c:80:8: note: in expansion of macro ‘DEFINE_MUTEX’
 static DEFINE_MUTEX(smpboot_threads_lock);
        ^
make[1]: *** [kernel/smpboot.o] Error 1
make: *** [kernel/smpboot.o] Error 2


This was bisected down to:

commit b2601928b5bf34a817b5a9a2a371c476018e634d
Author: mpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Wed Oct 15 10:08:00 2014 +0000

        * doc/invoke.texi: Update to reflect that GNU11 is the default
        mode for C.
        * c-common.h (c_language_kind): Update comment.
    c-family/
        * c-opts.c (c_common_init_options): Make -std=gnu11 the default for C.
    
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@216247 138bc75d-0d04-0410-961f-82ee72b054a4
Comment 1 Andrew Pinski 2014-10-16 22:49:41 UTC
I Just reported this to the mailing list:
https://gcc.gnu.org/ml/gcc-patches/2014-10/msg01640.html

The reduced testcase that shows the difference between GNU 89 and GNU 11/99:
typedef struct {
 volatile unsigned int lock;
} arch_rwlock_t;
typedef struct {
 arch_rwlock_t raw_lock;
} rwlock_t;

static rwlock_t step_hook_lock = (rwlock_t) { .raw_lock = { 0 }, };
Comment 2 Marek Polacek 2014-10-17 05:33:40 UTC
I believe we should just allow initializing objects with static storage duration with compound literals even in gnu99/gnu11.
Comment 3 Marek Polacek 2014-10-17 05:38:36 UTC
(But warn with -pedantic.)
Comment 4 Marek Polacek 2014-10-17 21:03:26 UTC
Author: mpolacek
Date: Fri Oct 17 21:02:54 2014
New Revision: 216416

URL: https://gcc.gnu.org/viewcvs?rev=216416&root=gcc&view=rev
Log:
	PR c/63567
	* c-typeck.c (digest_init): Allow initializing objects with static
	storage duration with compound literals even in C99 and add pedwarn
	for it.

	* gcc.dg/pr61096-1.c: Change dg-error into dg-warning.
	* gcc.dg/pr63567-1.c: New test.
	* gcc.dg/pr63567-2.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr63567-1.c
    trunk/gcc/testsuite/gcc.dg/pr63567-2.c
Modified:
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-typeck.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/pr61096-1.c
Comment 5 Marek Polacek 2014-10-17 21:04:15 UTC
Should be fixed.  I did what I outlined in #c2 and #c3.
Comment 6 Sasha Levin 2014-10-18 05:26:52 UTC
I'm still seeing the issue with the fix applied:

  CC      kernel/smpboot.o
In file included from include/linux/mutex.h:15:0,
                 from include/linux/kernfs.h:13,
                 from include/linux/sysfs.h:15,
                 from include/linux/kobject.h:21,
                 from include/linux/device.h:17,
                 from include/linux/node.h:17,
                 from include/linux/cpu.h:16,
                 from kernel/smpboot.c:4:
include/linux/spinlock_types.h:82:2: error: initializer element is not constant
  (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  ^
include/linux/mutex.h:112:18: note: in expansion of macro ‘__SPIN_LOCK_UNLOCKED’
   , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
                  ^
include/linux/mutex.h:118:27: note: in expansion of macro ‘__MUTEX_INITIALIZER’
  struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
                           ^
kernel/smpboot.c:80:8: note: in expansion of macro ‘DEFINE_MUTEX’
 static DEFINE_MUTEX(smpboot_threads_lock);
        ^
include/linux/spinlock_types.h:82:2: note: (near initialization for ‘smpboot_threads_lock.wait_lock’)
  (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  ^
include/linux/mutex.h:112:18: note: in expansion of macro ‘__SPIN_LOCK_UNLOCKED’
   , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
                  ^
include/linux/mutex.h:118:27: note: in expansion of macro ‘__MUTEX_INITIALIZER’
  struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
                           ^
kernel/smpboot.c:80:8: note: in expansion of macro ‘DEFINE_MUTEX’
 static DEFINE_MUTEX(smpboot_threads_lock);
Comment 7 Marek Polacek 2014-10-18 07:26:20 UTC
Well, can you give me preprocessed source code then?
Because the short testcase in Comment 1 is accepted now.
Comment 8 Sasha Levin 2014-10-18 17:53:11 UTC
Created attachment 33756 [details]
Preprocessed kernel/smpboot.c
Comment 9 Sasha Levin 2014-10-18 17:53:49 UTC
I've attached the preprocessed kernel/smpboot.c. The problem seems to be in line 24563:

static struct mutex smpboot_threads_lock = { .count = { (1) } , .wait_lock = (spinlock_t ) { { .rlock = { .raw_lock = { { 0 } }, .magic = 0xdead4ead, .owner_cpu = -1, .owner = ((void *)-1L), .dep_map = { .name = "smpboot_threads_lock.wait_lock" } } } } , .wait_list = { &(smpboot_threads_lock.wait_list), &(smpboot_threads_lock.wait_list) } , .magic = &smpboot_threads_lock , .dep_map = { .name = "smpboot_threads_lock" } };
Comment 10 Marek Polacek 2014-10-18 18:30:38 UTC
Thanks.  This fixes it (I need to write a proper testcase and test it).

2014-10-18  Marek Polacek  <polacek@redhat.com>

	PR c/63567
	* c-typeck.c (output_init_element): Allow initializing objects with
	static storage duration with compound literals even in C99 and add
	pedwarn for it.

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 0dd3366..ee874da 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -8251,11 +8251,14 @@ output_init_element (location_t loc, tree value, tree origtype,
     value = array_to_pointer_conversion (input_location, value);
 
   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
-      && require_constant_value && !flag_isoc99 && pending)
+      && require_constant_value && pending)
     {
       /* As an extension, allow initializing objects with static storage
 	 duration with compound literals (which are then treated just as
 	 the brace enclosed list they contain).  */
+      if (flag_isoc99)
+	pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
+		      "constant");
       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
       value = DECL_INITIAL (decl);
     }
Comment 11 Sasha Levin 2014-10-19 04:17:06 UTC
That does the trick. Thanks!

A different issue with the patch I've previously bisected came up, I'll open a different bug report for that.
Comment 12 Marek Polacek 2014-10-19 16:48:07 UTC
Author: mpolacek
Date: Sun Oct 19 16:47:35 2014
New Revision: 216440

URL: https://gcc.gnu.org/viewcvs?rev=216440&root=gcc&view=rev
Log:
	PR c/63567
	* c-typeck.c (output_init_element): Allow initializing objects with
	static storage duration with compound literals even in C99 and add
	pedwarn for it.

	* gcc.dg/pr63567-3.c: New test.
	* gcc.dg/pr63567-4.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr63567-3.c
    trunk/gcc/testsuite/gcc.dg/pr63567-4.c
Modified:
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-typeck.c
    trunk/gcc/testsuite/ChangeLog
Comment 13 Marek Polacek 2014-10-19 16:50:20 UTC
Now fixed for real.