c++ - GCC 与 Clang : Meaning of "-pedantic-errors"

标签 c++ c gcc clang portability

我正在使用 Clang v3.7.0 和 Mingw-w64 5.1.0 和 GCC 5.1.0,全部 64 位,在 Windows 10 上。我的目标是使用一组 Clang 和 GCC 选项,这将使我有最好的机会检测潜在的 C89C++98许多不同编译器之间的语言标准可移植性问题。例如,对于 C,我一直在使用以下 GCC 命令行并取得了相当大的成功:

gcc -c -x c -std=c89 -pedantic-errors -Wall -Wextra -Wno-comment -Wno-parentheses -Wno-format-zero-length test.c

但是,我最近用 Clang 尝试了它并得到了不同的结果。这是我的示例测试代码:

int main(void)
{
    int length = (int)strlen("Hello");
    return 0;
}

使用 Clang 我得到了以下错误,而使用 GCC 我得到了相同的基本信息,但是它将它标记为警告:

test.c:3:22: error: implicitly declaring library function 'strlen'
with type 'unsigned long long (const char *)'
    int length = (int)strlen("Hello");

如果我删除 -pedantic-errors 选项,或者将其更改为 -pedantic ,然后 Clang 仅将其标记为警告,这正是我真正想要的。然而,根据 GCC 文档,-pedantic-errors 选项会导致被视为语言扩展的警告被标记为错误,但不使用函数原型(prototype)不是 C89 中的扩展。所以,我有三个基本问题:

  1. Clang 是否根据 GCC 使用的含义改变了 -pedantic-errors 的含义,还是我误解了什么?

  2. 强制遵守所选标准并针对所有不合格代码发出错误的最佳选项集是什么?

  3. 如果我继续对 Clang 使用 -pedantic-errors,有没有办法让它在特定情况下发出警告而不是错误?在本网站的另一篇帖子中给出了一个答案,说使用以下内容,其中 foo 是错误:

    -Wno-error=foo
    

如果这是一个正确的方法,我实际使用什么来代替 foo 来处理我遇到的错误,因为没有指示实际的错误编号?我不敢相信它实际上需要以下所有内容:

-Wno-error=implicitly declaring library function 'strlen'
with type 'unsigned long long (const char *)'

最佳答案

您的代码无效,行为未定义,因此编译器可以做任何事情also when compiling .隐式声明的 int strlen(char*)size_t strlen(const char *) 不兼容。

Has Clang changed the meaning of -pedantic-errors from the meaning used by GCC, or am I misinterpreting something?

当我读到它时,是的。来自 clang 文档:

-pedantic-errors

    Error on language extensions.

在海湾合作委员会中:

-pedantic

    Issue all the warnings demanded by strict ISO C and ISO C++ [...]

-pedantic-errors

    Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some cases where there is undefined behavior at compile-time and in some other cases that do not prevent compilation of programs that are valid according to the standard.

扩展的 Clang 错误。

当标准明确要求和其他“某些情况”时,GCC 错误。

这是一个不同的,它是一组不同的错误。标准可能不需要诊断,但它仍然是一个扩展 - GCC 将保持沉默,Clang 将出错。

What is the best set of options that will enforce adherence to the selected standard and will issue errors for all non-conforming code?

想到的第一个答案是:“无”。编译器本质上使用“实现定义的行为”和扩展,因为它们意味着首先编译代码,而不意味着不编译不符合要求的代码。在某些情况下,代码符合要求,但编译器之间的行为仍然不同 - 您可以探索这样的 a case here .

无论如何,继续使用-pedantic-errors,因为它似乎可以检测不一致的代码。您的代码无效,行为未定义,因此您的代码不符合要求,因此 clang 可以正确检测到它。还可以使用 linters 和 sanitizer 来检测其他未定义行为的情况。

If I continue to use -pedantic-errors with Clang is there a way to get it to issue a warning instead of an error in specific cases?

使用-fno-builtin

关于c++ - GCC 与 Clang : Meaning of "-pedantic-errors",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38820417/

相关文章:

c++ - 判断虚函数是否重载

c - 为什么 gcc 在一种情况下会警告 "assuming signed overflow"而在另一种情况下不会警告

C - 使用指针的数组总和

c - 如何将多个可继承句柄值传递给子进程...?

c++ - 我如何将内存读入 wstring?

c++ - 打印 vector 的 vector

c++ - 带有字符串/八进制的 int lambda 表达式? (C++)

c - 过程关联表和全局偏移表

avx512编译错误,是GCC问题吗?

c++ - clang 似乎使用 gcc 库