]> git.draconx.ca Git - cdecl99.git/commitdiff
Fix spacing after qualified pointers when rendering C declarations.
authorNick Bowler <nbowler@draconx.ca>
Fri, 24 Feb 2012 02:31:58 +0000 (21:31 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sat, 25 Feb 2012 22:06:55 +0000 (17:06 -0500)
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

index bae96f351d370bbf0a3442d4579949bc4440692d..d5af7149e551570dee37569413eafdcd9553dfe1 100644 (file)
@@ -17,6 +17,7 @@
  */
 #include <config.h>
 #include <stdio.h>
+#include <stdbool.h>
 #include <assert.h>
 
 #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,