[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Correctly handle pointer alignment with typedef'd types
From: |
Nikhil Benesch |
Subject: |
[PATCH] Correctly handle pointer alignment with typedef'd types |
Date: |
Thu, 11 Apr 2019 19:30:14 +0200 |
This fixes a as-of-yet unreported bug, in which indent misaligns pointers
in function parameters when the parameters use a typedef type. For example,
func f1(int *void)
renders correctly, but
func f1(uint64_t *void)
misrenders as:
func f1(uint64_t * void)
The problem occurs because the parser thinks `uint64_t* void` is a binary
multiplication operation, while `int *void` involves a unary dereference
operator. The pointer alignment code only adjust the spacing of unary
operators, at present.
The attached patch fixes the issue by looking for multiplications inside
function parameters and spacing them properly. With the patch, the entire
regression test suite passes on my machine, including the additional test
coverage for the bug.
P.S. Is there a better place to send this mail? I didn't see any mailing
lists besides this one, and the patches section of the Savannah project
doesn't seem to be used.
---
commit 58905ad3140b33ce6d429e3086611daccf05ec4f
Author: Nikhil Benesch <address@hidden>
Date: Thu Apr 11 19:15:05 2019 +0200
Correctly handle pointer alignment with typedefs
diff --git a/regression/input/pointer-pal.c b/regression/input/pointer-pal.c
index 6973a7b..eb0da73 100644
--- a/regression/input/pointer-pal.c
+++ b/regression/input/pointer-pal.c
@@ -1 +1,3 @@
main(){int*x;x=malloc(sizeof(int));*x = 1;}
+func1(int* p) {}
+func2(uint64_t* p){uint64_t* q; p = q * q;}
diff --git a/regression/standard/pointer-pal.c
b/regression/standard/pointer-pal.c
index 1817315..7c0b5af 100644
--- a/regression/standard/pointer-pal.c
+++ b/regression/standard/pointer-pal.c
@@ -4,3 +4,13 @@ main ()
x = malloc (sizeof (int));
*x = 1;
}
+
+func1 (int* p)
+{
+}
+
+func2 (uint64_t* p)
+{
+ uint64_t* q;
+ p = q * q;
+}
diff --git a/src/handletoken.c b/src/handletoken.c
index 919fba9..9880530 100644
--- a/src/handletoken.c
+++ b/src/handletoken.c
@@ -717,8 +717,10 @@ static void handle_token_binary_op(
{
char * t_ptr;
- if (parser_state_tos->want_blank ||
- (e_code > s_code && *e_code != ' '))
+ if ((parser_state_tos->want_blank || (e_code > s_code && *e_code != ' '))
+ && !(parser_state_tos->in_parameter_declaration
+ && !settings.pointer_align_right
+ && *token == '*'))
{
set_buf_break (bb_binary_op, paren_target);
*(e_code++) = ' ';
@@ -752,7 +754,9 @@ static void handle_token_binary_op(
}
#endif
- parser_state_tos->want_blank = true;
+ parser_state_tos->want_blank = !(parser_state_tos->in_parameter_declaration
+ && settings.pointer_align_right
+ && *token == '*');
}
/**
- [PATCH] Correctly handle pointer alignment with typedef'd types,
Nikhil Benesch <=