From ed00362ec1f6f385d03ea5ca2b3dc3a8c25f4426 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Thu, 23 Feb 2012 21:31:58 -0500 Subject: [PATCH] Fix spacing after qualified pointers when rendering C declarations. We don't want to print spaces after pointer qualifiers all the time: in particular, things like "type const pointer to array of int" would print a space between the "const" and the ")", as in: int (* const )[] Fix that up by eliding the space if (a) the pointer has qualifiers, and (b) the child is not a null declarator. --- src/declare.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/declare.c b/src/declare.c index bae96f3..d5af714 100644 --- a/src/declare.c +++ b/src/declare.c @@ -17,6 +17,7 @@ */ #include #include +#include #include #include "cdecl.h" @@ -84,7 +85,17 @@ static size_t declare_pointer(char *buf, size_t n, struct cdecl_pointer *p) ret += cdecl__advance_(&buf, &n, rc); rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL); - return ret + cdecl__advance(&buf, &n, rc); + return ret + rc; +} + +static bool pointer_needs_space(struct cdecl_declarator *d) +{ + assert(d->type == CDECL_DECL_POINTER); + + if (d->u.pointer.qualifiers) + return d->child->type != CDECL_DECL_NULL; + + return false; } static size_t declare_array(char *buf, size_t n, struct cdecl_array *a) @@ -140,7 +151,10 @@ declare_declarator(char *buf, size_t n, struct cdecl_declarator *d) break; case CDECL_DECL_POINTER: rc = declare_pointer(buf, n, &d->u.pointer); - ret += cdecl__advance_(&buf, &n, rc); + if (pointer_needs_space(d)) + ret += cdecl__advance(&buf, &n, rc); + else + ret += cdecl__advance_(&buf, &n, rc); break; /* * Arrays and functions are special: since they are postfix, -- 2.43.0