簡體   English   中英

來自 GCC 但不是 Clang 的復合文字和指定初始化程序警告

[英]Compound literal and designated initializer warning from GCC but not Clang

gcc -std=c99 -Wextra編譯這段代碼:

#include <stdio.h>

struct T {
    int a;
    int *b;
    int c;
};

int main(void)
{
    struct T t = {.b = ((int []){1, 1})};

    printf("%d\n", t.b[1]);
    return 0;
}

是在給我一個警告:

demo.c:11:12: warning: missing initializer for field ‘c’ of ‘struct T’ [-Wmissing-field-initializers]
     struct T t = {.b = ((int []){1, 1})};
            ^
demo.c:6:9: note: ‘c’ declared here
     int c;
         ^

但是指定的初始值設定項應該將其余成員初始化為零,即使它們被省略也是如此。

為什么警告? clang編譯同一段代碼時沒有警告)

gcc version 6.3.0 20170516 (Debian 6.3.0-18) 
clang version 3.8.1-24 (tags/RELEASE_381/final)

它看起來像一個 gcc“一致性錯誤”,這是gcc/c/c-typeck.c中的相關代碼片段

 7436   /* Warn when some struct elements are implicitly initialized to zero.  */
 7437   if (warn_missing_field_initializers
 7438       && constructor_type
 7439       && TREE_CODE (constructor_type) == RECORD_TYPE
 7440       && constructor_unfilled_fields)
 7441     {
 7442         bool constructor_zeroinit =
 7443          (vec_safe_length (constructor_elements) == 1
 7444           && integer_zerop ((*constructor_elements)[0].value));
 7445
 7446         /* Do not warn for flexible array members or zero-length arrays.  */
 7447         while (constructor_unfilled_fields
 7448                && (!DECL_SIZE (constructor_unfilled_fields)
 7449                    || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
 7450           constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
 7451
 7452         if (constructor_unfilled_fields
 7453             /* Do not warn if this level of the initializer uses member
 7454                designators; it is likely to be deliberate.  */
 7455             && !constructor_designated
 7456             /* Do not warn about initializing with ` = {0}'.  */
 7457             && !constructor_zeroinit)
 7458           {
 7459             if (warning_at (input_location, OPT_Wmissing_field_initializers,
 7460                             "missing initializer for field %qD of %qT",
 7461                             constructor_unfilled_fields,
 7462                             constructor_type))
 7463               inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
 7464                       "%qD declared here", constructor_unfilled_fields);
 7465           }
 7466     }

代碼的意圖似乎是警告任何屬性構造函數是否具有未填充字段。 您沒有在元素“a”上收到警告的事實可能是此處的“一致性錯誤”。

如果-Wextra旨在打開缺少的初始化程序警告,那么它有。 問題是,“缺少初始化程序警告”是否應該排除省略的屬性? gcc 和 clang 似乎不同意這一點——他們這樣做可能沒問題嗎?

這可能不是您正在尋找的答案.. 但希望它有助於您了解情況。 :). GCC 團隊有一個一致性錯誤,但在這些情況下,他們的代碼的意圖似乎是警告,而根據經驗,clang 不會。

為什么警告?

因為-Wmissing-field-initializers是由-Wextra設置的,而您在gcc調用中設置了后者。

-Wextra很挑剔,而-Wmissing-field-initializers甚至不是-Wall的元素。

只省略一些字段初始值設定項而不是全部是錯誤的來源。 在具有數百個元素的數組/結構中,您只是初始化其中的一些元素,那么人類幾乎不可能僅通過查看代碼來掌握它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM