简体   繁体   English

gcc -O3标志会导致-O2不会发出警告

[英]gcc -O3 flag causes warnings that -O2 doesn't

I'm building a fairly complex application, which built without any errors/warnings with -O0 . 我正在构建一个相当复杂的应用程序,它使用-O0构建时没有任何错误/警告。 However when I tried -O3 , I got a couple of them which are puzzling. 然而,当我尝试-O3 ,我得到了一些令人费解的东西。 For example: 例如:

1: static pinfo_t* pinfo[2] = { &gv1, &gv2 }; // GLOBAL
2: 
3: int i;
4: int x = sizeof(pinfo);
5: 
6: for (i = 0; i < x; i ++)
7:    if (pinfo[i]->id == NO_ID)
8:        printf("%s\n", pinfo[i]->name);

Note the compiler (gcc v4.3.2) built this successfully with -O0 (with O1 and O2 also). 注意编译器(gcc v4.3.2)使用-O0成功构建了这个(也带有O1和O2)。 But with -O3 , the compiler correctly pinpoints line 7 to be a potential problem causing line with the error: 但是使用-O3 ,编译器正确地将第7行精确定位为导致错误行的潜在问题:

error: array subscript is above array bounds

Fair enough, but when I comment out line 7, it has no problem with line 8, which should also have been flagged! 很公平,但是当我在第7行发表评论时,第8行也没有问题,这也应该被标记!

Any help is appreciated! 任何帮助表示赞赏!

Wrt the warnings and optimization level, in GCC some warnings are generated when analyzing the code during optimization passes, and thus when those passes are not enabled, those warnings are not emitted either. 在警告和优化级别中,在GCC中,在优化过程中分析代码时会生成一些警告,因此当未启用这些过程时,也不会发出这些警告。 It's a long-standing issue, but so far it hasn't propagated high enough on anyone's TODO list. 这是一个长期存在的问题,但到目前为止它还没有在任何人的TODO列表中传播得足够高。

Dollars to donuts that the compiler completely unrolls the loop at -O3 to something like: 美元到甜甜圈,编译器完全展开-O3循环到类似的东西:

...

if (pinfo[0]->id == NO_ID)
    printf("%s\n", pinfo[0]->name);
if (pinfo[1]->id == NO_ID)
    printf("%s\n", pinfo[1]->name);
if (pinfo[2]->id == NO_ID)
    printf("%s\n", pinfo[2]->name);

...

It then observes that the generated index is out of bounds and warns you about it. 然后它会观察到生成的索引超出范围并向您发出警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM