[英]Does C Standard guarantee diagnostic message for #error directive?
I have some trouble understanding semantics of 5.1.1.3/1
Diagnostics subclause from N1570 C11 draft (emphasis mine): 从N1570 C11草案(强调我的)中理解5.1.1.3/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
I understand that the intent was to exclude (non-constraint) undefined behavior (thus that are no diagnostics on eg buffer overflow), but what about #error
directive? 我理解的意图是排除(非约束)未定义的行为(因此对于例如缓冲区溢出没有诊断),但是#error
指令呢? As in 6.10.5/1
Error directive : 与6.10.5/1
错误指令一样 :
A preprocessing directive of the form 表单的预处理指令
# error
pp-tokens opt new-line# error
pp-tokens opt new-linecauses the implementation to produce a diagnostic message that includes the specified sequence of preprocessing tokens. 使实现生成包含指定序列的预处理标记的诊断消息 。
Does these both subclauses are not mutually exclusive? 这两个子条款是否相互排斥?
For some other reference see also DR#176 . 对于其他参考,请参阅DR#176 。
As someone who served on the committee, our response to a question like this would often begin with a phrase along the lines of "A careful reading of the standard ...", which is not quite as glib as it sounds. 作为在委员会任职的人,我们对这样一个问题的回答通常都是从“仔细阅读标准......”这句话开始,这听起来并不像听起来那么狡猾。 the language used is very specific. 使用的语言非常具体。 Consider the first clause 考虑第一个条款
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 在其他情况下不需要产生诊断信息
This clause is explicitly referring to a "violation of any syntax rule or constraint". 该子句明确指的是“违反任何语法规则或约束”。 A well formed #error
directive does not, therefore, trigger this clause. 因此,格式良好的#error
指令不会触发此子句。 Thus it does not apply, and mutual-exclusion is moot. 因此它不适用,相互排斥是没有实际意义的。
Also note the final sentence, where it says that diagnostics " need not be produced " . 还要注意最后一句,它说诊断“不需要产生” 。 The term "need not" does not imply "must not". “不必”并不意味着“必须”。 It simply means the implementation has the option whether or not to issue a diagnostic for other conditions (for example, style concerns). 它只是意味着实现可以选择是否为其他条件发出诊断(例如,样式问题)。 But again this entire clause is irrelevant for #error
但是这整个条款再次与#error
无关
Your second quote simply states exactly what an implementation must do for a well-formed #error
directive. 您的第二个引用只是简单地说明了一个实现必须对格式良好的#error
指令做什么。
C99 went a step further than the suggested resolution for that DR. C99比该DR的建议分辨率更进了一步。 Instead of requiring a diagnostic, they require treating it as an error. 他们不需要诊断,而是需要将其视为错误。
4. Conformance 4.一致性
4 The implementation shall not successfully translate a preprocessing translation unit containing a
#error
preprocessing directive unless it is part of a group skipped by conditional inclusion. 4实现不应成功转换包含#error
预处理指令的预处理转换单元,除非它是由条件包含跳过的组的一部分。
Now, strictly speaking, perhaps you're right that an implementation could choose to refuse to compile a program containing an #error
directive without issuing a diagnostic, claiming that 5.1.1.3 allows it to ignore the semantics for #error
. 现在,严格地说,也许你是对的,实现可以选择拒绝编译包含#error
指令的程序而不发出诊断,声称5.1.1.3允许它忽略#error
的语义。 However, an implementation that goes to such lengths to being as useless as possible within the bounds set by the standard, would easily work around any attempt to require a diagnostic: the implementation could simply dump the complete preprocessor output (including anything following #error
), and follow that by "there's an error in there somewhere". 但是,在标准设置的范围内尽可能无用的实现很容易解决任何需要诊断的尝试:实现可以简单地转储完整的预处理器输出(包括#error
任何内容)并遵循“那里某处出现错误”。 Because of that, it effectively doesn't matter whether the standard requires a diagnostic. 因此,标准是否需要诊断无关紧要。 There's no excuse for an implementation not to do so, and very little that the standard could to do force unwilling implementors. 没有理由不执行此实施,并且很少有标准可以强制执行不情愿的实现者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.