X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/31ac11cc668bb8ecc1317fd2e8bd79b7925bceeb..056280bf2e3188551539d87852ddbdaf322eae52:/src/declare.c diff --git a/src/declare.c b/src/declare.c index bae96f3..3b0b700 100644 --- a/src/declare.c +++ b/src/declare.c @@ -1,6 +1,6 @@ /* * Render C declarations. - * Copyright © 2011 Nick Bowler + * Copyright © 2011, 2021, 2023 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,26 +15,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + #include #include +#include #include #include "cdecl.h" -#include "output.h" - -static size_t declare_specs(char *buf, size_t n, struct cdecl_declspec *s) -{ - size_t ret = 0, rc; - - if (!s) - return 0; - - rc = cdecl__explain_pre_specs(buf, n, s); - ret += cdecl__advance(&buf, &n, rc); - - rc = cdecl__explain_post_specs(buf, n, s); - return ret + rc; -} +#include "cdecl-internal.h" static size_t declare_declarator(char *buf, size_t n, struct cdecl_declarator *d); @@ -43,7 +31,7 @@ static size_t declare_decl(char *buf, size_t n, struct cdecl *decl) { size_t ret = 0, rc; - rc = declare_specs(buf, n, decl->specifiers); + rc = cdecl__explain_specs(buf, n, decl->specifiers, -1); if (decl->declarators->type != CDECL_DECL_NULL) ret += cdecl__advance(&buf, &n, rc); else @@ -84,7 +72,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 +138,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,