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
Create script to apply style to diffs #11261
Conversation
Fixes #11170 |
So what about existing one here: |
@Maijin maybe I should remove that? I just copied it directly in |
Needs to have only one yes. |
removed |
Don't forget to update scripts like indent.sh that uses this clang-format |
Thanks, done. |
I slightly modified |
I removed the indent-diff.sh script and just handled everything in the clang-format-diff.py script |
Even though @radare said clang-format doesn't work, after testing some files I think it does a pretty good job. There are some things that are not well indented (like break in a switch case), but the good thing about this script is that it works at the "patch level" so we can more easily manually check those cases. Most of the stuff will be done automatically (e.g. indentation, spaces around functions, operators, etc.) I believe as @radare said there are cases that clang-format does not handle well, but I do think we can either adapt to what the tools expect (instead of having our own particular coding style) or just consider them from case to case, manually. |
doc/indent-example.c
Outdated
"Usage:", "aa[0*?]", " # see also 'af' and 'afna'", | ||
"aa", " ", "alias for 'af@@ sym.*;af@entry0;afva'", //;.afna @@ fcn.*'", | ||
"aa*", "", "analyze all flags starting with sym. (af @@ sym.*)", | ||
NULL}; |
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.
this thing is different from what it is now, but... do we really care? I think we can adapt to this style as long as the automatic tool is able to correctly indent this.
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.
the } should be in the next line
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.
} shuld be in the next line
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.
As said, is this really important? What I want is one way to automatically indent/fix style in my code. I don't care whether there's a space before/after a given word, as long as there's a tool that automagically does that.
Is there such a tool?
If yes, perfect, please show me.
If not, I think it's time we change a bit our coding style to adapt to those existing tools.
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.
well it looks bad to me, and i dont know if there's any tool to do this unless my sys/indent.sh was doing it, which i didnt checked. i dont think we should change our coding style because we need to make code beautiful to read and not adjust to the bugs of the indentation tools. again, i didnt had time to test this pr yet, just speaking out loud
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.
The }
is probably attached because there is no ,
after NULL. Maybe we could programmatically insert colons at the end of lists like this to force the }
to go to the next line.
On the other hand, I agree with retlibc that the attached-closing-curly-bracket-style is also OK, you would just need to get used to it.
r_anal_esil_set_pc (core->anal->esil, fcn ? fcn->addr : core->offset); | ||
switch (*input) { | ||
case 'a': // "afta" | ||
{ |
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.
I've seen so many different styles in r2 code base related to this... Again, who cares as long as we have an automatic tool?
r_anal_esil_set_pc (core->anal->esil, fcn ? fcn->addr : core->offset); | ||
switch (*input) { | ||
case 'a': // "afta" | ||
{ |
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.
I've seen so many different styles related to switch/cases in r2 codebase that I think we can adapt to this, since at least it is supported by automatic tools.
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.
i think we should refactor this out and not allow such blocks in case statements. just move the code in a separate function or define variables outside will make it more readable. so im not sure if i want to define a syntax rule for those cases
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.
I do agree, but still, there's so much code in r2codebase that has these code blocks in the switch-cases... And refacatoring is something the formatter can't do, we should ask for it during reviews.
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.
coccinelle can do that
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.
Can it extract functions from switch-case blocks?
Anyway, I don't think we do want such a behaviour. That's something that should be reviewed manually by us.
doc/indent-example.c
Outdated
r_anal_esil_set_pc (core->anal->esil, fcn ? fcn->addr : core->offset); | ||
r_core_anal_type_match (core, fcn); | ||
r_core_seek (core, seek, true); | ||
} break; |
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.
NOTE: this is the only case which I think deviates a lot from our current style... I think we can check this manually for the moment and just ask people to put the break inside the braces, so it will be correctly indented as part of the block.
@radare I've put an example with clang-format in doc/indent-example.c and added some comments on some different things that apply there. I'd like your comments on that. Also, by applying clang-format on entire files I obtain reasonable results. |
Have you reported how "break;" after a block in a switch statement is formatted to the Clang project? Seems like a bug. But on the other hand, why not include the "break;" in the block in the first place? Seems like that would be a prettier style? The change is even easy to do programmatically if you decide to use clang-format for radare2 (at least for the cases where the "break;" precedes a case keyword). |
@nsajko I din't report the "break;" thing to Clang yet. I will double-check and do it if necessary. |
I think you should add "IndentPPDirectives: AfterHash" to .clang_format. |
@nsajko thanks for the help! have you seen indented |
I did a quick search in libr: util/thread_sem.c util/sys.c util/regex/regexec.c util/regex/regcomp.c util/regex/regex2.h fs/fs.c include/sdb/types.h include/r_types.h debug/p/debug_native.c etc. |
yeah but as you can probably see most of the code doesn't do that. I guess those parts were copied from other code which used the indentation. Anyway, let's see what @radare thinks about it. |
OK |
The radare2 .clang-format should probably also have something like
With the other foreach macros, too. |
Maybe merge pre-commit-indent in the python script and probably indent.sh in it to have only one consolidated script instead of 3 scattered? |
obviously this is something to be done by a human
… On 21 Sep 2018, at 11:51, Riccardo Schirone ***@***.***> wrote:
@ret2libc commented on this pull request.
In doc/indent-example.c <#11261 (comment)>:
> +static const char *help_msg_aa[] = {
+ "Usage:", "aa[0*?]", " # see also 'af' and 'afna'",
+ "aa", " ", "alias for 'af@@ ***@***.***;afva'", //;.afna @@ fcn.*'",
+ "aa*", "", "analyze all flags starting with sym. (af @@ sym.*)",
+ NULL};
+
+static int cmpaddr(const void *_a, const void *_b) {
+ const RAnalFunction *a = _a, *b = _b;
+ return a->addr - b->addr;
+}
+
+int main(int argc, char **argv) {
+ r_anal_esil_set_pc (core->anal->esil, fcn ? fcn->addr : core->offset);
+ switch (*input) {
+ case 'a': // "afta"
+ {
Can it extract functions from switch-case blocks?
Anyway, I don't think we do want such a behaviour. That's something that should be reviewed manually by us.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#11261 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AA3-lkdDLmFdOQBndI1WgIyIRgS4o9Vjks5udLawgaJpZM4WM0iH>.
|
Codecov Report
@@ Coverage Diff @@
## master #11261 +/- ##
=======================================
Coverage 37.47% 37.47%
=======================================
Files 899 899
Lines 284166 284166
=======================================
Hits 106492 106492
Misses 177674 177674 Continue to review full report at Codecov.
|
sys/clang-format-diff.py
Outdated
@@ -0,0 +1,153 @@ | |||
#!/usr/bin/env python |
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.
Could you be explicit about which version of python you are invoking? Eg. do #! /usr/bin/python2
. This is needed for example on Archlinux.
EDIT: to clarify, /bin/python is usually a symbolic link to /bin/python2, but on Archlinux /bin/python instead links to /bin/python3. So using python
as a command is not a good idea.
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.
Actually, it would be great if you could port this to the latest Python version, it is probably not a big effort.
sys/clang-format-diff.py
Outdated
@@ -0,0 +1,153 @@ | |||
#!/usr/bin/env python2 |
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.
Don't know if you are interested in porting to Python 3, but I am pretty sure you would just need to change this "shebang" line, import functools
, and change reduce
to functools.reduce
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.
already done
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.
Thanks :)
@radare ping I think this is now ready to be (at least) tested more. |
sys/pre-commit-indent
Outdated
@@ -0,0 +1,34 @@ | |||
#!/bin/sh |
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.
rename to sys/pre-commit-indent.sh
sys/pre-commit-indent
Outdated
|
||
tmpfile="$(mktemp)" | ||
unstaged_diff="$(mktemp)" | ||
git diff > "${unstaged_diff}" |
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.
can be "", chk if mktemp is not failing and tmpfile and unsaged_diff are not. ""
sys/pre-commit-indent
Outdated
tmpfile="$(mktemp)" | ||
unstaged_diff="$(mktemp)" | ||
git diff > "${unstaged_diff}" | ||
git checkout -- . |
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.
|| exit 1
sys/pre-commit-indent
Outdated
|
||
restore_exit() { | ||
git apply < "${unstaged_diff}" 2>/dev/null | ||
rm "${tmpfile}" |
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.
add -f
sys/pre-commit-indent
Outdated
restore_exit() { | ||
git apply < "${unstaged_diff}" 2>/dev/null | ||
rm "${tmpfile}" | ||
rm "${unstaged_diff}" |
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.
add -f
sys/pre-commit-indent
Outdated
# | ||
# To enable this hook, move it to ".git/hooks/pre-commit". | ||
|
||
tmpfile="$(mktemp)" |
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.
uppercase variable names?
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.
lgtm
I think I've done everything requested. |
I copied the clang-format-diff.py script from the clang project, as it provides a way to apply clang-format to diffs only. The license is
University of Illinois Open Source
. Is that ok?Moreover, this patch restores the tab size to 8 char.