如果C标准说禁止使用空格,为什么clang和gcc都只在反斜杠后有空格时才发出警告? [英] Why both clang and gcc only give a warning when there is a space after backslash if C standard says that whitespace is forbidden?

查看:162
本文介绍了如果C标准说禁止使用空格,为什么clang和gcc都只在反斜杠后有空格时才发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译以下代码段时,gcc和clang都只会发出警告。注意 \ 之后的 int 之后的空格:

When compiling the following snippet, both gcc and clang only issue a warning. Notice space after \ next to int:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int \ 
        a = 10;

    printf("%d\n", a);
}

gcc:


main.c:7:6:警告:反斜杠和换行符之间用空格分隔[默认情况下启用
]

main.c:7:6: warning: backslash and newline separated by space [enabled by default]

clang:


main.c:7:7:警告:反斜杠和换行符之间用空格
隔开int ^

main.c:7:7: warning: backslash and newline separated by space int ^

在5.1.1.2中的c99标准中,它表示:

In c99 standard in 5.1.1.2 it says:


每个反斜杠字符()紧跟
换行符的实例都将被删除,从而将物理源代码行拼接成
逻辑源代码行。

Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines.

为什么C编译器在这里不符合C标准?我认为这只是创作者的决定。我在gcc邮件列表中找到一条消息,认为是这种现象的引入: http://gcc.gnu.org/ml/gcc-patches/2000-09/msg00430.html 。他们说这样做是因为尾随空格很常见,并且他们不想将它们视为错误。

Why don't C compilers conform to C standard here? I think it's only their creators decision not to. I found a message on gcc mailing list that I think introduced this behavior: http://gcc.gnu.org/ml/gcc-patches/2000-09/msg00430.html. There, they say that this is done because trailing whitespaces are common and they don't want to treat them as an error. How common is that?

推荐答案

只要文件更改<$ c $,就允许编译器扩展语言。 c> gcc 在他们的文档中的 6.21转义换行符的规则稍宽松

The compiler is allowed to extend the language as long as the document the change which gcc does in their docs in section 6.21 Slightly Looser Rules for Escaped Newlines.


最近,预处理器已放松了对转义换行符的处理。以前,换行符必须立即加上反斜杠。当前实现允许以空格,水平和垂直制表符的形式出现空格,并在反斜杠和后续换行符之间形成换页。预处理器发出警告,但将其视为有效的转义换行符,并将这两行合并以形成一条逻辑行。这适用于注释和标记以及标记之间。出于放松目的,注释不被视为空格,因为它们尚未被空格替换。

Recently, the preprocessor has relaxed its treatment of escaped newlines. Previously, the newline had to immediately follow a backslash. The current implementation allows whitespace in the form of spaces, horizontal and vertical tabs, and form feeds between the backslash and the subsequent newline. The preprocessor issues a warning, but treats it as a valid escaped newline and combines the two lines to form a single logical line. This works within comments and tokens, as well as between tokens. Comments are not treated as whitespace for the purposes of this relaxation, since they have not yet been replaced with spaces.

clang 努力支持 gcc 扩展名,并指向 gcc 文档:

and clang strives to support gcc extensions and points to the gcc docs on them:


此文档描述了Clang提供的语言扩展。除了此处列出的语言扩展之外,Clang还旨在支持各种GCC扩展。有关这些扩展的更多信息,请参见GCC手册。

this document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.

因此,他们履行了对标准的义务。实际上, Linux依赖于许多gcc扩展。我们可以通过查看草稿C99标准部分 4。 Conformance 段落 6 看到以下内容:

So their obligations with respect to the standard are fulfilled. In fact Linux depends on many gcc extensions. We can see this by looking at the draft C99 standard section 4. Conformance paragraphs 6 which say:


[...]符合要求的实现可以扩展(包括其他
库函数),只要它们不改变任何行为 3)

脚注3说:


这意味着符合标准的实现只保留本国际标准中明确保留的标识符。

This implies that a conforming implementation reserves no identifiers other than those explicitly reserved in this International Standard.

和第 8 段:


实现应随附以下文件:定义所有实现定义的和特定于语言环境的特征以及所有扩展。

An implementation shall be accompanied by a document that defines all implementation defined and locale-specific characteristics and all extensions.

gcc 还说明您可以使用 -pedantic 标志来标记在使用扩展程序时生成警告,您可以使用 -pedantic-errors 标志进行设置它是一个错误:

gcc also documents that you can use the -pedantic flag to generate a warning when using extensions and you can use -pedantic-errors flag to make it an error:


[...]以获取标准所需的所有诊断信息,还应指定-pedantic(或- pedantic-errors(如果您希望它们是错误而不是警告)。

[...] to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).

这篇关于如果C标准说禁止使用空格,为什么clang和gcc都只在反斜杠后有空格时才发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆