dawnbeen / c_formatter_42

C language formatter for 42 norminette
GNU General Public License v3.0
176 stars 17 forks source link

Fix #50 #52

Closed younesaassila closed 1 year ago

younesaassila commented 1 year ago

Fixes #50 by properly calculating the additional indent level:

The previous code assumed a paren depth of 1 meant 2 tabs. This is incorrect and the code made a few exceptions for control statements and return statements to correct for this behavior. Of course, this couldn't catch all possible code where this adjustment was needed.

The new code assumes a paren depth of 1 means 1 tab. Tab count starts increasing once the paren depth is at least 2. (Had to check norminette's code to find this behavior, it wasn't fun)


Also fixed a test case that was based on the previous (incorrect) behavior (and it indeed made the norminette complain)


Finally, made a line of the code clearer and removed an unused comment at the top of the test file.

keyhr commented 1 year ago

Is Norminette rules changed after I quit 42? or did I just missed the test case? Anyway, the code seems good.

younesaassila commented 1 year ago

I don't believe the rules changed recently. The bug was just well hidden thanks to the discount system for if, while, etc.

younesaassila commented 1 year ago

@cacharle You should reopen #51 (marked as duplicate of #50) because they are not relating to the same issue. #51 seems to be a conflict with clang-format where clang-format breaks the alignment of a correctly aligned file and c_formatter_42 fails to fix it after. I can reproduce the bug (both old and new versions) with this file of mine (for function are_rotations_same_direction):

See: #53

Code ```c /* ************************************************************************** */ /* */ /* ::: :::::::: */ /* push_swap.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: yaassila +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/01 13:00:00 by yaassila #+# #+# */ /* Updated: 2023/02/14 18:00:00 by yaassila ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PUSH_SWAP_H # define PUSH_SWAP_H # include "libft.h" # include // --- Types --- typedef struct s_node { int data; struct s_node *prev; struct s_node *next; } t_node; typedef struct s_stack { t_node *top; t_node *bottom; size_t size; } t_stack; typedef struct s_find_result { t_node *node; size_t index; } t_find_result; typedef struct s_cost { size_t stack_a_index; size_t stack_b_index; int cost; } t_cost; // --- Prototypes --- int parse_args(int argc, char const *argv[], t_stack *stack); t_node *node_new(int data); t_stack *stack_new(void); void stack_free(t_stack *stack); void stack_print(t_stack *stack); t_find_result stack_find(t_stack *stack, int data); t_find_result stack_find_min(t_stack *stack); t_find_result stack_find_max(t_stack *stack); t_node *stack_pop_top(t_stack *stack); t_node *stack_pop_bottom(t_stack *stack); void stack_push_top(t_stack *stack, t_node *node); void stack_push_bottom(t_stack *stack, t_node *node); void sa(t_stack *stack_a); void sb(t_stack *stack_b); void ss(t_stack *stack_a, t_stack *stack_b); void pa(t_stack *stack_a, t_stack *stack_b); void pb(t_stack *stack_a, t_stack *stack_b); void ra(t_stack *stack_a); void rb(t_stack *stack_b); void rr(t_stack *stack_a, t_stack *stack_b); void rra(t_stack *stack_a); void rrb(t_stack *stack_b); void rrr(t_stack *stack_a, t_stack *stack_b); size_t get_rotate_count(t_stack *stack, size_t index); t_bool are_rotations_same_direction(t_stack *stack_a, t_stack *stack_b, size_t index_a, size_t index_b); t_bool are_rotations_normal(t_stack *stack_a, t_stack *stack_b, size_t index_a, size_t index_b); void rotate_stack_to_index(t_stack *stack, size_t index, void (*r)(t_stack *), void (*rr)(t_stack *)); void rotate_stacks_to_indices(t_stack *stack_a, t_stack *stack_b, size_t index_a, size_t index_b); t_bool is_stack_sorted_ascending(t_stack *stack); void sort(t_stack *stack_a, t_stack *stack_b); void three_sort(t_stack *stack_a); void insertion_sort(t_stack *stack_a, t_stack *stack_b); void min_cost_sort(t_stack *stack_a, t_stack *stack_b, size_t insertion_sort_threshold); #endif ```