GCC supports `-Wparentheses' (warn about some constructs where parentheses are suggested for clarification, although not strictly syntactically necessary). The corresponding code is also there for GPC. I'm going to activate it now. The questions are:
- Which constructs to warn about? So far, I've come up with the following ones:
* `and' in an operand of `or', as in `a or b and c' which is equivalent to `a or (b and c)', but may confuse the reader. (Same for `and_then' instead of `and' etc., of course.)
* logical operations (`and', `or', `not', etc.) in an operand of a relational operator (`=', etc., `in'). This would catch cases like the dreaded `if not a = b'.
- What should be the default? I suggest off by default and on with `-Wall' (same as in GCC).
(Of course, the issue is not as serious in Pascal than it is in C where the danger of confusion between `==' and `=' in a comparison almost requires such a warning, but I think it's also useful in Pascal.)
Frank
Le Lundi 21 Avril 2003 03:24, Frank Heckenbach a écrit :
GCC supports `-Wparentheses' (warn about some constructs where parentheses are suggested for clarification, although not strictly syntactically necessary). The corresponding code is also there for GPC. I'm going to activate it now. The questions are:
Which constructs to warn about? So far, I've come up with the following ones:
`and' in an operand of `or', as in `a or b and c' which is equivalent to `a or (b and c)', but may confuse the reader. (Same for `and_then' instead of `and' etc., of course.)
logical operations (`and', `or', `not', etc.) in an operand of a relational operator (`=', etc., `in'). This would catch cases like the dreaded `if not a = b'.
What should be the default? I suggest off by default and on with `-Wall' (same as in GCC).
if the list is as short as the one you propose, the default can be on, because in these cases, even if parentheses are actually unnecessary, they greatly improve legibility, and so make maintenance very much simpler and safer. if the list is longer, and includes less obvious cases, it is better that the default be off
Jean-Pierre Vial wrote:
Le Lundi 21 Avril 2003 03:24, Frank Heckenbach a écrit :
GCC supports `-Wparentheses' (warn about some constructs where parentheses are suggested for clarification, although not strictly syntactically necessary). The corresponding code is also there for GPC. I'm going to activate it now. The questions are:
Which constructs to warn about? So far, I've come up with the following ones:
`and' in an operand of `or', as in `a or b and c' which is equivalent to `a or (b and c)', but may confuse the reader. (Same for `and_then' instead of `and' etc., of course.)
logical operations (`and', `or', `not', etc.) in an operand of a relational operator (`=', etc., `in'). This would catch cases like the dreaded `if not a = b'.
What should be the default? I suggest off by default and on with `-Wall' (same as in GCC).
if the list is as short as the one you propose, the default can be on, because in these cases, even if parentheses are actually unnecessary, they greatly improve legibility, and so make maintenance very much simpler and safer. if the list is longer, and includes less obvious cases, it is better that the default be off
1. precedence of operators.
It is well defined that a or b and c is equal to a or (b and c) , as a + b * c equal to a + (b * c). A warning here is annoying. If at all, it should be a separate option, off by default and not included in -Wall.
2. logical operations in an operand of a relational operator
OK, this can be confusing, a warning is useful (off by default and on with -Wall).
Regards,
Adriaan van Os
Adriaan van Os wrote:
- precedence of operators.
It is well defined that a or b and c is equal to a or (b and c) , as a + b * c equal to a + (b * c). A warning here is annoying. If at all, it should be a separate option, off by default and not included in -Wall.
- logical operations in an operand of a relational operator
OK, this can be confusing, a warning is useful (off by default and on with -Wall).
Sorry, I think you're contradicting yourself. 2. is also about precendence of operators, and it's just as well defined. That's not the question -- it's about whether humans (not computers) are likely to confuse them.
When writing more complex Boolean expressions, I've found it useful (in C so far) to get this warning, since occasionally I really make such mistakes (and they're often not easy to notice otherwise).
Frank
Frank Heckenbach wrote:
GCC supports `-Wparentheses' (warn about some constructs where parentheses are suggested for clarification, although not strictly syntactically necessary). The corresponding code is also there for GPC. I'm going to activate it now. The questions are:
Which constructs to warn about? So far, I've come up with the following ones:
`and' in an operand of `or', as in `a or b and c' which is equivalent to `a or (b and c)', but may confuse the reader. (Same for `and_then' instead of `and' etc., of course.)
logical operations (`and', `or', `not', etc.) in an operand of a relational operator (`=', etc., `in'). This would catch cases like the dreaded `if not a = b'.
What should be the default? I suggest off by default and on with `-Wall' (same as in GCC).
(Of course, the issue is not as serious in Pascal than it is in C where the danger of confusion between `==' and `=' in a comparison almost requires such a warning, but I think it's also useful in Pascal.)
I'm not too sure about this. In C the primary use is to warn about using an assignment as a logical, which is a syntax error in Pascal, and to warn about possible misconstrued elses, as in "if (a) if (b) c else d;" The else problem is valid for Pascal. Gcc doesn't warn about "if (a | b & c)", although perhaps it should. I definitely think -Wall should warn about the elses, but suspect people will turn it off if it warns about the logical parenthization. Can it be separated into a -Welse and a -Wparenthesis, so that it has to be explicitly turned on? Is there a -Welse in gcc now? Can it warn on (a or b and c) and not on (a + b * c)?
CBFalconer wrote:
Frank Heckenbach wrote:
GCC supports `-Wparentheses' (warn about some constructs where parentheses are suggested for clarification, although not strictly syntactically necessary). The corresponding code is also there for GPC. I'm going to activate it now. The questions are:
Which constructs to warn about? So far, I've come up with the following ones:
`and' in an operand of `or', as in `a or b and c' which is equivalent to `a or (b and c)', but may confuse the reader. (Same for `and_then' instead of `and' etc., of course.)
logical operations (`and', `or', `not', etc.) in an operand of a relational operator (`=', etc., `in'). This would catch cases like the dreaded `if not a = b'.
What should be the default? I suggest off by default and on with `-Wall' (same as in GCC).
(Of course, the issue is not as serious in Pascal than it is in C where the danger of confusion between `==' and `=' in a comparison almost requires such a warning, but I think it's also useful in Pascal.)
I'm not too sure about this. In C the primary use is to warn about using an assignment as a logical, which is a syntax error in Pascal, and to warn about possible misconstrued elses, as in "if (a) if (b) c else d;"
GCC puts this into the same option. I didn't mention this here, because it's not quite the same. It's about associativity, not precedence, and BTW, implementing it would be completely distinct.
The else problem is valid for Pascal. Gcc doesn't warn about "if (a | b & c)", although perhaps it should.
It does:
frank@goedel:~/work# cat x.c int main () { return 0 | 0 & 0; } frank@goedel:~/work# gcc -Wall x.c x.c: In function `main': x.c:3: warning: suggest parentheses around arithmetic in operand of |
I definitely think -Wall should warn about the elses, but suspect people will turn it off if it warns about the logical parenthization.
Well, for me it's just the opposite. I don't like the `else' warning because in my experience it forces too many additional `begin'/`end' pairs, and mismatching is usually seen by proper indentation, anyway. Whereas I found relatively few places in my code where I needed additional parentheses for the precedence warnings (mostly with bit-operators which are non-standard, anyway).
Can it be separated into a -Welse and a -Wparenthesis, so that it has to be explicitly turned on?
I agree insofar as I'm implementing `-Wparentheses' which will do the precedence warnings. If anyone wants to implement `-Welse', just go ahead. I probably won't do it myself (see above).
Is there a -Welse in gcc now?
No, in GCC both is `-Wparentheses' (which I don't really like).
Can it warn on (a or b and c) and not on (a + b * c)?
Yes, I'd prefer so. The precedence of `+' and `*' should be clear enough to most people.
Frank