clang W 标志顺序

clang W flag order

我注意到 clang 有一个有趣的行为(我使用 3.6.0),但我没有在文档或其他任何地方找到任何关于它的参考。这是一个小例子:

int main(){
    int a;
    return 0;
}

我用 clang++ -Wall -W -Werror -Wno-error=unused-variable main.cpp 编译它,我得到了预期的警告:

main.cpp:2:9: warning: unused variable 'a' [-Wunused-variable]
    int a;
1 warning generated.

现在,让我们试试clang++ -Werror -Wno-error=unused-variable -Wall -W main.cpp

main.cpp:2:9: error: unused variable 'a' [-Werror,-Wunused-variable]
    int a;
1 error generated.

我错过了什么吗?是预期的吗?就此而言,gcc 会编译这两行。

这是我得到的答复:

I think that the better title would be that -Wno-error is position dependent on the command line while -Werror is not. The important part is whether the diagnostic is an error or a warning. With the example:

int main() {
  int a;
  return 0;
}

$ clang main.cpp -Wunused-variable

This gives an unused variable warning.

$ clang main.cpp -Werror -Wunused-variable
$ clang main.cpp -Wunused-variable -Werror

Both of these give an unused variable error. -Werror does not change behavior based on position.

$ clang main.cpp -Werror -Wno-error=unused-variable -Wunused-variable
$ clang main.cpp -Werror -Wunused-variable -Wno-error=unused-variable

The first gives an error while the second gives an warning. This means that -Wno-error=* is position dependent. (GCC will issue warnings for both of these lines.)

-Werror does not interact or depend on the warnings on the command line. -Wno-error=warning does depend on its relative position to -Wwarning.

我非常满意。它只是应该写在某个地方(我可能错过了!)