Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC C++ warning: suggest parentheses

I've written an expression parser which spits out a bunch of assembler instructions for x86, x64 and ARM.

To test it I've written a small app that generates random expressions, compiles them with GCC and compares the result with my code, so far so good.

Now I want to have my parser produce warnings similar to GCC.

I've noticed that with GCC 5.1.0

    int a = 100 + 100 | 10;

GCC give a suggested parentheses warning around |

but

    int b = 100 * 100 | 10;

GCC gives no warning.

but both addition and multiplication have higher precedence than bitwise OR, so why no warning on the int b = expression?

I'm very tired lol so may have overlooked something.

like image 223
CascadeCoder Avatar asked Dec 20 '25 00:12

CascadeCoder


1 Answers

The ultimate answer can only come from the implementers, but I guess the reason is that there are other languages which have different preferences of those operators, and therefore users of those other languages may misinterpret the expression. For example in some Pascal dialects, & has the same precedence as * and | has the same precedence as +, so an expression involving both + and | without parentheses in between may have a different interpretation (standard Pascal has no & or |, but the precedence of and and or in standard Pascal follows the same rules). I guess that just like many languages copy the C operator precedence, others copy the Pascal one.

like image 98
celtschk Avatar answered Dec 21 '25 17:12

celtschk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!