Hello,
I would like to submit a small patch to fix a bug I noticed in the
handling of the pointer star in parameter declaration.
Since indent don't detect user-defined types at lexing time, the
eventual star following such a type is considered to be a binary operator.
As a consequence the formatting is off when it comes to aligning the
star on the left (or the right depending on the options.)
For example:
void f(char * s);
void g(custom_type * p);
Would become:
void f (char *s);
void g (custom_type * p);
with `indent -npro -par`
My patch simply detect that a `*` is being handled as a binary operator
in a parameter definition and redirect to the unary operator handler.
It thus provide the expected output:
void f(char *s);
void g(custom_type *p);
If you use a lot of typedef as I do and want to fully rely on indent to
keep style homogeneous, this small change is very welcome.
I added a regression test named `custom-type-pointer.c`. It tests for
function declarations and prototypes, and also check that actual
multiplications are not affected.
I had to modify the test `bug-gnu-33364.c` reference because my solution
work by trusting `in_decl` which is broken in that test.
Maybe this patch can be useful to others.
Cheers,
Théo Cavignac
PS: I never submitted any patch through a mailing list, hopefully I did
everything right.
diff --git a/regression/TEST b/regression/TEST
index 56f41d9..e94938d 100755
--- a/regression/TEST
+++ b/regression/TEST
@@ -37,7 +37,7 @@ BUGS="case-label.c one-line-1.c one-line-2.c one-line-3.c \
one-line-4.c struct-decl.c sizeof-in-while.c line-break-comment.c \
macro.c enum.c elif.c nested.c wrapped-string.c minus_predecrement.c \
bug-gnu-33364.c float-constant-suffix.c block-comments.c \
- no-forced-nl-in-block-init.c hexadecimal_float.c"
+ no-forced-nl-in-block-init.c hexadecimal_float.c custom-type-pointer.c"
INDENTSRC="args.c backup.h backup.c dirent_def.h globs.c indent.h \
indent.c indent_globs.h io.c lexi.c memcpy.c parse.c pr_comment.c \
diff --git a/regression/input/custom-type-pointer.c
b/regression/input/custom-type-pointer.c
new file mode 100644
index 0000000..6a0c513
--- /dev/null
+++ b/regression/input/custom-type-pointer.c
@@ -0,0 +1,8 @@
+typedef int * value;
+
+value * g(int v);
+
+void f(value * arr)
+{
+ value * v = g(a * b);
+}
diff --git a/regression/standard/bug-gnu-33364.c
b/regression/standard/bug-gnu-33364.c
index bd45c8a..a7e1e7c 100644
--- a/regression/standard/bug-gnu-33364.c
+++ b/regression/standard/bug-gnu-33364.c
@@ -1,5 +1,5 @@
main ()
{
return;
- malloc (x (int) + 2 * 4);
+ malloc (x (int) + 2 *4);
}
diff --git a/regression/standard/custom-type-pointer.c
b/regression/standard/custom-type-pointer.c
new file mode 100644
index 0000000..06b47cc
--- /dev/null
+++ b/regression/standard/custom-type-pointer.c
@@ -0,0 +1,9 @@
+typedef int *value;
+
+value *g (int v);
+
+void
+f (value *arr)
+{
+ value *v = g (a * b);
+}
diff --git a/src/handletoken.c b/src/handletoken.c
index 919fba9..1cdddda 100644
--- a/src/handletoken.c
+++ b/src/handletoken.c
@@ -713,9 +713,21 @@ static void handle_token_unary_op(
*/
static void handle_token_binary_op(
+ int * dec_ind,
const bb_code_ty can_break)
{
char * t_ptr;
+
+ /*
+ * the token is actually a pointer star used after a custom type
+ */
+ if ((*token == '*') &&
+ parser_state_tos->in_stmt &&
+ parser_state_tos->in_decl)
+ {
+ handle_token_unary_op(dec_ind, can_break);
+ return;
+ }
if (parser_state_tos->want_blank ||
(e_code > s_code && *e_code != ' '))
@@ -2251,12 +2263,12 @@ extern void handle_the_token(
case unary_op:
/* this could be any unary operation */
- handle_token_unary_op( dec_ind, can_break);
+ handle_token_unary_op(dec_ind, can_break);
break;
case binary_op:
/* any binary operation */
- handle_token_binary_op(can_break);
+ handle_token_binary_op(dec_ind, can_break);
break;
case postop: