cl: array type has incomplete element type => 为什么警告而不是错误?
cl: array type has incomplete element type => why warning and not error?
示例代码(t0.c):
typedef struct S1 T1;
typedef T1 T[];
int main(void)
{
return sizeof(T);
}
结果:
clang(版本 11.0.0):
t0.c:2:13: error: array has incomplete element type 'T1' (aka 'struct S1')
gcc(版本 10.2.0):
t0.c:2:12: error: array type has incomplete element type 'T1' {aka 'struct S1'}
cl(版本 19.25.28611):
t0.c(6): warning C4034: sizeof returns 0
问题:为什么 cl
生成警告而不是错误?
UPD.
- 如果指定
/Za
(禁用语言扩展),则 __STDC__
定义为定义 1。
- 根据
ANSI Conformance
页 (https://docs.microsoft.com/en-us/cpp/c-language/ansi-conformance?view=msvc-160):
Microsoft C conforms to the standard for the C language as set forth in the 9899:1990 edition of the ANSI C standard.
- ISO/IEC 9899:1990, 6.3.3.4 sizeof 运算符(强调已加):
The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an lvalue that designates a bitfield object.
UPD2。添加了编译器版本。
某事是“警告”还是“错误”由个人实施决定:
5.1.1.3 Diagnostics
1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
9) The intent is that an implementation should identify the nature of, and where possible localize, each violation. Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated. It may also successfully translate an invalid program.
C 2011 Online Draft
在我使用过的大多数编译器中,任何停止翻译的东西(不可恢复的语法错误,如缺少 }
或未声明的标识符)都被视为 错误,而可能导致运行时错误的问题被视为警告,但没有统一的标准。只要满足上面的基本要求,实现就可以自由地做它想做的事。它可以发出 "?"
的单个诊断以响应所有语法 and/or 约束错误并满足标准的要求。
示例代码(t0.c):
typedef struct S1 T1;
typedef T1 T[];
int main(void)
{
return sizeof(T);
}
结果:
clang(版本 11.0.0):
t0.c:2:13: error: array has incomplete element type 'T1' (aka 'struct S1')
gcc(版本 10.2.0):
t0.c:2:12: error: array type has incomplete element type 'T1' {aka 'struct S1'}
cl(版本 19.25.28611):
t0.c(6): warning C4034: sizeof returns 0
问题:为什么 cl
生成警告而不是错误?
UPD.
- 如果指定
/Za
(禁用语言扩展),则__STDC__
定义为定义 1。 - 根据
ANSI Conformance
页 (https://docs.microsoft.com/en-us/cpp/c-language/ansi-conformance?view=msvc-160):
Microsoft C conforms to the standard for the C language as set forth in the 9899:1990 edition of the ANSI C standard.
- ISO/IEC 9899:1990, 6.3.3.4 sizeof 运算符(强调已加):
The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an lvalue that designates a bitfield object.
UPD2。添加了编译器版本。
某事是“警告”还是“错误”由个人实施决定:
5.1.1.3 DiagnosticsC 2011 Online Draft
1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
9) The intent is that an implementation should identify the nature of, and where possible localize, each violation. Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated. It may also successfully translate an invalid program.
在我使用过的大多数编译器中,任何停止翻译的东西(不可恢复的语法错误,如缺少 }
或未声明的标识符)都被视为 错误,而可能导致运行时错误的问题被视为警告,但没有统一的标准。只要满足上面的基本要求,实现就可以自由地做它想做的事。它可以发出 "?"
的单个诊断以响应所有语法 and/or 约束错误并满足标准的要求。