-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Replace ReturnErrorCodeIf with VerifyOrReturnError #36083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Review changes with SemanticDiff. |
5c75ee5 to
6130fd8
Compare
…urnError($A == $B || $C == $D, $Z);'
…urnError($A != $B || $C != $D, $Z);'
…urnError($A != $B && $C != $D, $Z);'
…A) && !($B), $Z);' This commit was manually optimized.
…A) || !($B), $Z);' This commit was manually optimized.
6130fd8 to
c626609
Compare
|
@andy31415 , to my surprise I was able to convert everything automatically with simple DeMorgan simplifications. I hope, that right now readability will not be sacrificed. |
|
PR #36083: Size comparison from 579b1b1 to 92e76fb Full report (93 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, nrfconnect, nxp, psoc6, qpg, stm32, telink, tizen)
|
Several change iterations, removing stale changes requested tag.
andy31415
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks readable at a first glance.
* 'ReturnErrorCodeIf(!$A, $B);' -> 'VerifyOrReturnError($A, $B);' * 'ReturnErrorCodeIf($A == $B, $C);' -> 'VerifyOrReturnError($A != $B, $C);' * 'ReturnErrorCodeIf($A != $B, $C);' -> 'VerifyOrReturnError($A == $B, $C);' * 'ReturnErrorCodeIf($A < $B, $C);' -> 'VerifyOrReturnError($A >= $B, $C);' * 'ReturnErrorCodeIf($A <= $B, $C);' -> 'VerifyOrReturnError($A > $B, $C);' * 'ReturnErrorCodeIf($A > $B, $C);' -> 'VerifyOrReturnError($A <= $B, $C);' * 'ReturnErrorCodeIf($A >= $B, $C);' -> 'VerifyOrReturnError($A < $B, $C);' * 'ReturnErrorCodeIf($A($$$B), $C);' -> 'VerifyOrReturnError(!$A($$$B), $C);' * 'ReturnErrorCodeIf($A, $B);' -> 'VerifyOrReturnError(!$A, $B);' * Replace ReturnErrorCodeWithMetricIf with VerifyOrReturnErrorWithMetric * Restyled by clang-format * 'ReturnErrorCodeIf($A != $B && $C != $D, $Z);' --rewrite 'VerifyOrReturnError($A == $B || $C == $D, $Z);' * 'ReturnErrorCodeIf($A == $B && $C == $D, $Z);' --rewrite 'VerifyOrReturnError($A != $B || $C != $D, $Z);' * 'ReturnErrorCodeIf($A == $B || $C == $D, $Z);' --rewrite 'VerifyOrReturnError($A != $B && $C != $D, $Z);' * 'ReturnErrorCodeIf($A || $B, $Z);' --rewrite 'VerifyOrReturnError(!($A) && !($B), $Z);' This commit was manually optimized. * 'ReturnErrorCodeIf($A && $B, $Z);' --rewrite 'VerifyOrReturnError(!($A) || !($B), $Z);' This commit was manually optimized. * Drop support for ReturnErrorCodeIf * Restyled by clang-format --------- Co-authored-by: Restyled.io <[email protected]>
* 'ReturnErrorCodeIf(!$A, $B);' -> 'VerifyOrReturnError($A, $B);' * 'ReturnErrorCodeIf($A == $B, $C);' -> 'VerifyOrReturnError($A != $B, $C);' * 'ReturnErrorCodeIf($A != $B, $C);' -> 'VerifyOrReturnError($A == $B, $C);' * 'ReturnErrorCodeIf($A < $B, $C);' -> 'VerifyOrReturnError($A >= $B, $C);' * 'ReturnErrorCodeIf($A <= $B, $C);' -> 'VerifyOrReturnError($A > $B, $C);' * 'ReturnErrorCodeIf($A > $B, $C);' -> 'VerifyOrReturnError($A <= $B, $C);' * 'ReturnErrorCodeIf($A >= $B, $C);' -> 'VerifyOrReturnError($A < $B, $C);' * 'ReturnErrorCodeIf($A($$$B), $C);' -> 'VerifyOrReturnError(!$A($$$B), $C);' * 'ReturnErrorCodeIf($A, $B);' -> 'VerifyOrReturnError(!$A, $B);' * Replace ReturnErrorCodeWithMetricIf with VerifyOrReturnErrorWithMetric * Restyled by clang-format * 'ReturnErrorCodeIf($A != $B && $C != $D, $Z);' --rewrite 'VerifyOrReturnError($A == $B || $C == $D, $Z);' * 'ReturnErrorCodeIf($A == $B && $C == $D, $Z);' --rewrite 'VerifyOrReturnError($A != $B || $C != $D, $Z);' * 'ReturnErrorCodeIf($A == $B || $C == $D, $Z);' --rewrite 'VerifyOrReturnError($A != $B && $C != $D, $Z);' * 'ReturnErrorCodeIf($A || $B, $Z);' --rewrite 'VerifyOrReturnError(!($A) && !($B), $Z);' This commit was manually optimized. * 'ReturnErrorCodeIf($A && $B, $Z);' --rewrite 'VerifyOrReturnError(!($A) || !($B), $Z);' This commit was manually optimized. * Drop support for ReturnErrorCodeIf * Restyled by clang-format --------- Co-authored-by: Restyled.io <[email protected]>
Rationale
The
ReturnErrorCodeIf(expr, value)define is a C/C++ syntactic sugar for very simpleif (expr) { return value; }pattern. This define does not add any new value to plain C/C++ code. However, in case of error handling we already haveVerifyOrReturnErrorwhich optionally can get 3rd argument for some "extra code". TheVerifyOrReturnErroris widely used in the codebase. It also indicates that the "if" branch is for error handling (so it's a natural replacement for "return-error-if-something"). Instead of having two macros which basically do the same thing, how about using only one uniformly in the project?Changes
The code was automatically converted with AST search/replace (in order to avoid typos during conversion) using following patterns:
ReturnErrorCodeIf(!$A, $B);->VerifyOrReturnError($A, $B);ReturnErrorCodeIf($A == $B, $C);->VerifyOrReturnError($A != $B, $C);ReturnErrorCodeIf($A != $B, $C);->VerifyOrReturnError($A == $B, $C);ReturnErrorCodeIf($A < $B, $C);->VerifyOrReturnError($A >= $B, $C);ReturnErrorCodeIf($A <= $B, $C);->VerifyOrReturnError($A > $B, $C);ReturnErrorCodeIf($A > $B, $C);->VerifyOrReturnError($A <= $B, $C);ReturnErrorCodeIf($A >= $B, $C);->VerifyOrReturnError($A < $B, $C);ReturnErrorCodeIf($A($$$B), $C);->VerifyOrReturnError(!$A($$$B), $C);ReturnErrorCodeIf($A, $B);->VerifyOrReturnError(!($A), $B);ReturnErrorCodeIf($A != $B && $C != $D, $Z);--rewriteVerifyOrReturnError($A == $B || $C == $D, $Z);ReturnErrorCodeIf($A == $B && $C == $D, $Z);--rewriteVerifyOrReturnError($A != $B || $C != $D, $Z);ReturnErrorCodeIf($A == $B || $C == $D, $Z);--rewriteVerifyOrReturnError($A != $B && $C != $D, $Z);ReturnErrorCodeIf($A || $B, $Z);--rewriteVerifyOrReturnError(!($A) && !($B), $Z);(manually simplified)ReturnErrorCodeIf($A && $B, $Z);--rewriteVerifyOrReturnError(!($A) || !($B), $Z);(manually simplified)Testing
CI will verify.