Skip to content
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

Merged
merged 34 commits into from Sep 26, 2018
Merged

Conversation

ret2libc
Copy link
Contributor

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.

@ret2libc ret2libc requested review from radare and Maijin August 26, 2018 15:18
@ret2libc
Copy link
Contributor Author

Fixes #11170

@Maijin
Copy link
Contributor

Maijin commented Aug 26, 2018

So what about existing one here:

@ret2libc
Copy link
Contributor Author

@Maijin maybe I should remove that? I just copied it directly in .clang-format, since at least it will be parsed by editors plugins.

@Maijin
Copy link
Contributor

Maijin commented Aug 26, 2018

Needs to have only one yes.

@ret2libc
Copy link
Contributor Author

removed

@Maijin
Copy link
Contributor

Maijin commented Aug 26, 2018

Don't forget to update scripts like indent.sh that uses this clang-format

@ret2libc
Copy link
Contributor Author

Thanks, done.

@ret2libc ret2libc changed the title Create script to apply style to diffs WIP: Create script to apply style to diffs Aug 26, 2018
@ret2libc ret2libc changed the title WIP: Create script to apply style to diffs Create script to apply style to diffs Aug 26, 2018
@ret2libc ret2libc changed the title Create script to apply style to diffs WIP: Create script to apply style to diffs Aug 26, 2018
@ret2libc ret2libc changed the title WIP: Create script to apply style to diffs Create script to apply style to diffs Aug 26, 2018
@ret2libc
Copy link
Contributor Author

I slightly modified clang-format-diff.py to handle function declaration/definitions which do not require the space before the parenthesis.

@ret2libc
Copy link
Contributor Author

I removed the indent-diff.sh script and just handled everything in the clang-format-diff.py script

@ret2libc
Copy link
Contributor Author

ret2libc commented Aug 27, 2018

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.

"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};
Copy link
Contributor Author

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.

Copy link
Collaborator

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

Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Collaborator

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

Copy link
Contributor

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"
{
Copy link
Contributor Author

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"
{
Copy link
Contributor Author

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.

Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coccinelle can do that

Copy link
Contributor Author

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.

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;
Copy link
Contributor Author

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.

@ret2libc
Copy link
Contributor Author

@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.

@nsajko
Copy link
Contributor

nsajko commented Aug 29, 2018

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).

@ret2libc
Copy link
Contributor Author

@nsajko I din't report the "break;" thing to Clang yet. I will double-check and do it if necessary.
I also think that including the break; in the block is better anyway.

@nsajko
Copy link
Contributor

nsajko commented Aug 29, 2018

I think you should add "IndentPPDirectives: AfterHash" to .clang_format.

@ret2libc
Copy link
Contributor Author

@nsajko thanks for the help! have you seen indented #if in the current code? I'd like to introduce as less changes to the style as possible for the moment.

@nsajko
Copy link
Contributor

nsajko commented Aug 29, 2018

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.

@ret2libc
Copy link
Contributor Author

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.

@nsajko
Copy link
Contributor

nsajko commented Aug 29, 2018

OK

@nsajko
Copy link
Contributor

nsajko commented Aug 29, 2018

The radare2 .clang-format should probably also have something like

ForEachMacros: ['r_list_foreach', 'ls_foreach', 'fcn_tree_foreach_intersect', 'r_skiplist_foreach', 'graph_foreach_anode']

With the other foreach macros, too.

@Maijin
Copy link
Contributor

Maijin commented Aug 29, 2018

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?

@ret2libc
Copy link
Contributor Author

@Maijin I think I could merge indent.sh in it, but I'm not sure about pre-commit-indent. I think I will keep it separate, it's just a script that calls the .py and prints an error messag.

@nsajko thanks! That's useful, if you find anything else please share!

@radare
Copy link
Collaborator

radare commented Sep 21, 2018 via email

@codecov-io
Copy link

codecov-io commented Sep 21, 2018

Codecov Report

Merging #11261 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           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.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d04e888...0efaa0e. Read the comment docs.

@@ -0,0 +1,153 @@
#!/usr/bin/env python
Copy link
Contributor

@nsajko nsajko Sep 21, 2018

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.

Copy link
Contributor

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.

@@ -0,0 +1,153 @@
#!/usr/bin/env python2
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

@ret2libc ret2libc changed the title WIP: Create script to apply style to diffs Create script to apply style to diffs Sep 21, 2018
@ret2libc
Copy link
Contributor Author

@radare ping I think this is now ready to be (at least) tested more.

@@ -0,0 +1,34 @@
#!/bin/sh
Copy link
Collaborator

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


tmpfile="$(mktemp)"
unstaged_diff="$(mktemp)"
git diff > "${unstaged_diff}"
Copy link
Collaborator

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. ""

tmpfile="$(mktemp)"
unstaged_diff="$(mktemp)"
git diff > "${unstaged_diff}"
git checkout -- .
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| exit 1


restore_exit() {
git apply < "${unstaged_diff}" 2>/dev/null
rm "${tmpfile}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add -f

restore_exit() {
git apply < "${unstaged_diff}" 2>/dev/null
rm "${tmpfile}"
rm "${unstaged_diff}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add -f

#
# To enable this hook, move it to ".git/hooks/pre-commit".

tmpfile="$(mktemp)"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uppercase variable names?

Copy link
Collaborator

@radare radare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ret2libc
Copy link
Contributor Author

I think I've done everything requested.

@ret2libc ret2libc merged commit 58ae116 into radareorg:master Sep 26, 2018
Refactorings automation moved this from IN PROGRESS to DONE Sep 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Refactorings
  
DONE
Development

Successfully merging this pull request may close these issues.

None yet

5 participants