Flags (and replaces) the alternative tokens for binary and unary
operators, such as `not (for !), bitand (for &), or (for ||`)
or `not_eq (for !=`).
Details
Diff Detail
- Build Status
Buildable 5031 Build 5031: arc lint + arc unit
Event Timeline
I think will be good idea to make this check working in both ways (standard or alternative operator representations), depending on option.
Probably readability-operators-representation will be better name for check.
See also PR25195.
Thanks for your feedback, Eugene!
I'm not sure if it would be helpful to have this check in both ways. I did a code search for "not_eq", "bitand" and "and_eq"
on github, and their usage seems to be a clear minority.
So I would propose to keep the features as-is for now,
change the name to readability-operators-representation, and then later (someone else?) might also add an option
for making this work the other way around. Would that be ok for you?
Were you refering to https://reviews.llvm.org/D25195 ("[lit] Fix FormatError on individual test timeout")?
Nope, "PR" notation is used to reference bugs ("Problem Reports", PRobably): http://llvm.org/PR25195
So I would propose to keep the features as-is for now,
change the name to readability-operators-representation, and then later (someone else?) might also add an option
for making this work the other way around. Would that be ok for you?
Fine by me.
FWIW, I'm pretty sure this can and should be done on the lexer level - it will be faster and more universal.
Thanks for working on this!
I'm not sure if it would be helpful to have this check in both ways. I did a code search for "not_eq", "bitand" and "and_eq" on github, and their usage seems to be a clear minority.
I actually was requesting the opposite version of this (but suggesting to implement both), because for me "if (!something)" is much harder to read than "if (not something)" (I am a bit blind).
So I would propose to keep the features as-is for now, change the name to readability-operators-representation, and then later (someone else?) might also add an option for making this work the other way around. Would that be ok for you?
Sounds good to me. This solves half of the problem, and I agree with you that more people will benefit from this check than from the opposite check. Thanks again for working on this!
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) | I think this would make more sense lifted into an AST matcher -- there are bound to be a *lot* of binary operators, so letting the matcher memoize things is likely to give better performance. |
68 ↗ | (On Diff #93057) | This diagnostic doesn't help the user to understand what's wrong with their code (especially in the presence of multiple operators). Perhaps "'%0' is an alternative token spelling; consider using '%1'" It would be nice if we could say "consider using %1 for <reason>", but I'm really not certain why we would diagnose this code in the first place (it's purely a matter of stylistic choice, as I understand it). |
docs/clang-tidy/checks/readability-operators-representation.rst | ||
4 ↗ | (On Diff #93057) | This underline is going to cause Sphinx diagnostics. |
8 ↗ | (On Diff #93057) | Why would someone want to do this? You should at least touch on that in the documentation. Also, this doesn't read very clearly to me. Perhaps it would be better as a table with two columns, the first specifying the alternative token spelling and the second specifying the replacement (with suitable headings), |
test/clang-tidy/readability-operators-representation.cpp | ||
54 ↗ | (On Diff #93057) | Please add a test where a class overloads the operators. For special fun: struct S { friend S& operator and(const S &LHS, const S &RHS); }; int main() { S s1, s2; S s3 = s1 and s2; } I'm not convinced the correct answer here is to tell the user to use a spelling that isn't used by the class definition... |
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) | Any reasons not to do this on the lexer level? |
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) | Technical reasons? None. |
Improved diagnostic; updated documentation; added test for overloaded operator
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) | Do I correctly understand that "doing this on lexer level" would mean to implement this as a warning directly into clang? If yes, would it be possible to generate fixits and have them possibly applied automatically (as it is the case for clang-tidy)? |
68 ↗ | (On Diff #93057) | The main rational for this check is to enforce consistency and thus make it easier to read and comprehend the code. |
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
22 ↗ | (On Diff #93309) | I think that this check should only be performed in C++. In C, the alternative token spellings are implemented as macros and would require additional work to support. |
34 ↗ | (On Diff #93057) | You are correct, that means implementing it as a warning in Clang. I believe you can still generate those fixits from lexer-level diagnostics, but have not verified it. However, I don't think this diagnostic would be appropriate for Clang because it would have to be off by default. |
68 ↗ | (On Diff #93057) | I think that enforcing consistency is a good rationale for having the check, but would second the suggestion that this check have an option to enforce the consistency one way or the other. Then the diagnostic can be: "'%0' is %select{an alternative token|a primary token}2; consider using '%1' for consistency" |
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) | Actually, I was thinking about changing this clang-tidy check to analyze token stream somehow (probably by handling PPCallbacks to detect ranges that need to be re-lexed) instead of matching the AST. I didn't intend to propose a new Clang warning (but it looks like the wording was misleading). |
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) | There is some value in that -- it means we could support C, for instance. I'm not certain how easy or hard it would be, but suspect it's reasonable. However, in C, there's still the problem of the include file that introduces those macros. Do we have facilities to remove an include in clang-tidy? |
68 ↗ | (On Diff #93057) | The diagnostic is improved, but there's still no way to go the opposite direction (from primary to alternative). |
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) | Yes, it may make sense in C, but parameter for map from macro name to operator is needed. Product I'm working for, with long history, had alternative operator presentation implemented in C. |
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) | Changing macro-based "alternative tokens" to the corresponding operators can also be formulated in the terms of expanding a set of macros, which should work with any LangOpts and arbitrary alternative operator names.
Sounds like a generic "remove unused includes" problem, since we need to analyze whether the header is needed for anything else. In particular, even if the use of the header is limited to the alternative representations of operators, we can't remove the include until we replace all these macros. Clang-tidy doesn't provide any support for transactional fixes and dependencies between fixes. |
clang-tidy/readability/OperatorsRepresentationCheck.cpp | ||
---|---|---|
34 ↗ | (On Diff #93057) |
That's a good point. I also don't think an unused include is sufficiently awful to block this. |
Yep, I am also a fan of "and","or" and "not". The other tokens doesn't make it more readable imho
As noted above, my concern with the current implementation is that the use of AST in this check seems to be superfluous. It should be enough to handle PPCallbacks and re-lex each file opened during compilation to find all alternative tokens.