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

Refactor output modes to use enum (#489) #929

Closed
wants to merge 7 commits into from
Closed

Refactor output modes to use enum (#489) #929

wants to merge 7 commits into from

Conversation

valdaarhun
Copy link
Contributor

Your checklist for this pull request

  • I've read the guidelines for contributing to this repository
  • I made sure to follow the project's coding style
  • I've added tests that prove my fix is effective or that my feature works (if possible)
  • I've updated the documentation and the rizin book with the relevant information (if needed)

Detailed description

Refactored output modes to use enum. int mode and char mode patterns in the code have been replaced with RzOutputMode enum type.

Refactoring is still in progress.

Test plan

...

Closing issues

closes #489

@@ -237,8 +237,8 @@ RZ_API const char *rz_meta_type_to_string(int type) {
return "# unknown meta # ";
}

RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64 size, int rad, PJ *pj, bool show_full) {
rz_return_if_fail(!(rad == 'j' && !pj)); // rad == 'j' => pj != NULL
RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64 size, RzOutputMode rad, PJ *pj, bool show_full) {
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to rename variable too rad -> mode. And everywhere else you meet unclear name for the mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, will do.

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 have changed rad to mode wherever output modes are involved.

@@ -339,7 +339,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
break;
case 0:
case 1:
Copy link
Member

Choose a reason for hiding this comment

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

i would remove 0 and 1.

@@ -339,7 +339,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
break;
case 0:
case 1:
case '*':
case RZ_OUTPUT_MODE_RIZIN:
Copy link
Member

Choose a reason for hiding this comment

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

no need to define this if it goes into the default switch case.

Copy link
Contributor Author

@valdaarhun valdaarhun Mar 29, 2021

Choose a reason for hiding this comment

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

Ok, so I will remove all the three cases.

case 'j': print_types_json(pdb, pj, tpi_stream->types); return;
case 'r': print_types_format(pdb, tpi_stream->types); return;
case RZ_OUTPUT_MODE_JSON: print_types_json(pdb, pj, tpi_stream->types); return;
case RZ_OUTPUT_MODE_RIZIN: print_types_format(pdb, tpi_stream->types); return;
Copy link
Member

Choose a reason for hiding this comment

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

i'm not sure about this switch. what currently does the 'd' command?

rz_analysis_var_list_show(core->analysis, fcn, 'r', 0, NULL);
rz_analysis_var_list_show(core->analysis, fcn, 'b', RZ_OUTPUT_MODE_QUIET, NULL);
rz_analysis_var_list_show(core->analysis, fcn, 's', RZ_OUTPUT_MODE_QUIET, NULL);
rz_analysis_var_list_show(core->analysis, fcn, 'r', RZ_OUTPUT_MODE_QUIET, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

i think this should be RZ_OUTPUT_MODE_STANDARD

@@ -4464,11 +4464,11 @@ RZ_API int rz_core_analysis_search(RzCore *core, ut64 from, ut64 to, ut64 ref, i
op.size = 1;
}
break;
case 'r':
case RZ_OUTPUT_MODE_RIZIN:
Copy link
Member

Choose a reason for hiding this comment

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

i'm not sure this change is correct. that 'r' is a command, not a mode.

@@ -260,7 +260,7 @@ static RzOutputMode suffix2mode(const char *suffix) {
return argv_modes[i].mode;
}
}
return 0;
return RZ_OUTPUT_MODE_QUIET;
Copy link
Member

Choose a reason for hiding this comment

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

not sure about this change either.

@@ -1300,8 +1300,8 @@ static void cmd_debug_modules(RzCore *core, int mode) { // "dmm"
pj_end(pj);
} break;
case ':':
case '*':
if (mode == '*' || (mode == ':' && addr >= map->addr && addr < map->addr_end)) {
case 'RZ_OUTPUT_MODE_RIZIN':
Copy link
Member

Choose a reason for hiding this comment

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

this change doesn't make sense. the command requires to be splitted.

Copy link
Contributor Author

@valdaarhun valdaarhun Mar 30, 2021

Choose a reason for hiding this comment

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

@wargio I am sorry but I haven't understood what you mean by "command should be split". Do you mean I should have a separate case for mode == ':'? Also, I noticed that I have accidentally put RZ_OUTPUT_MODE_RIZIN in single quotes. I'll change that as well.

RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64 size, int rad, PJ *pj, bool show_full) {
rz_return_if_fail(!(rad == 'j' && !pj)); // rad == 'j' => pj != NULL
RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64 size, RzOutputMode mode, PJ *pj, bool show_full) {
rz_return_if_fail(!(mode == RZ_OUTPUT_MODE_JSON && !pj)); // mode == 'j' => pj != NULL
Copy link
Member

Choose a reason for hiding this comment

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

Please also update the commentary or just remove it.

@@ -348,7 +348,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
if (!s) {
s = strdup(pstr);
}
if (rad) {
if (mode) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should be if (mode), code doesn't make sense in JSON mode, for example. Lets just use some specific mode here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@XVilka Do you mean I should change it to something more specific like if (mode == RZ_OUTPUT_MODE_STANDARD)?

@@ -370,7 +370,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
free(s);
} break;
case RZ_META_TYPE_STRING:
if (rad) {
if (mode) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here and below.

@@ -306,7 +306,7 @@ RZ_API int rz_bp_list(RzBreakpoint *bp, int rad) {
pj_ks(pj, "data", rz_str_get(b->data));
pj_ks(pj, "cond", rz_str_get(b->cond));
pj_end(pj);
} else if (rad) {
} else if (mode) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here, better to use a switch instead of if/else and use specific modes.

@@ -1216,7 +1216,35 @@ RZ_IPI int rz_cmd_flag(void *data, const char *input) {
}
break;
case 's':
flag_space_stack_list(core->flags, input[2]);
RzOutputMode mode;
Copy link
Member

Choose a reason for hiding this comment

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

You could extract this in a separate function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@XVilka I just wanted to confirm one thing. Should I create an RZ_API defined function in this file that I can then call in every other file as well?

@@ -1456,7 +1484,35 @@ RZ_IPI int rz_cmd_flag(void *data, const char *input) {
free(s);
}
} else {
rz_flag_list(core->flags, *input, input[0] ? input + 1 : "");
RzOutputMode mode;
Copy link
Member

Choose a reason for hiding this comment

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

Same here, will also avoid duplication.

if (input[2] == '.') { // "Cs.."
ut64 size;
RzAnalysisMetaItem *mi = rz_meta_get_at(core->analysis, addr, type, &size);
if (mi) {
rz_meta_print(core->analysis, mi, addr, size, input[3], NULL, false);
switch(input[3]){
Copy link
Member

Choose a reason for hiding this comment

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

And here

@XVilka XVilka requested a review from ret2libc March 30, 2021 05:51
@wargio
Copy link
Member

wargio commented Mar 30, 2021

ok, here is what i would do, because this PR is quite messy and big even to comment.

  1. I would keep this branch and switch to dev
  2. i would then create a new branch from dev
  3. i would edit only 1 function at time, fix any incompatibility with the old code, rebuild and run the tests.
  4. if the tests does not fail, then commit these changes and push them.
  5. repeat 3 and 4 till you have converted all the functions and made a single commit for each of them.

you can open a new PR and reference that one to this.

@wargio
Copy link
Member

wargio commented Mar 30, 2021

if you prefer, we can go function by function, so we can guide you towards the resolution of the issue.

@XVilka
Copy link
Member

XVilka commented Mar 31, 2021

I agree about step-by-step approach, but instead of one function would do one/two files per PR. This way export from this PR is easy - just use git diff filename or similar command.

@valdaarhun
Copy link
Contributor Author

@wargio @XVilka Yeah, I think I'll do that. It'll be more organised that way.

@valdaarhun
Copy link
Contributor Author

valdaarhun commented Apr 1, 2021

@wargio @XVilka I created a new branch and refactored one file. I built it on my local computer and didn't get any compilation error, so I decided to push it in a new PR (#950). However, I have got three failing tests and I don't understand the reason behind them.

@wargio
Copy link
Member

wargio commented Apr 2, 2021

i'll close this to discuss on the other PR.

@wargio wargio closed this Apr 2, 2021
@wargio wargio mentioned this pull request Apr 2, 2021
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor output modes to use enum
3 participants