From 10ae438399f14367dd9e03032594c1e16c428999 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Tue, 25 May 2010 20:35:01 -0700 Subject: Avoid getting extra trailing whitespace from macros. This trailing whitespace was coming from macro definitions and from macro arguments. We fix this with a little extra state in the token_list. It now remembers the last non-space token added, so that these can be trimmed off just before printing the list. With this fix test 23 now passes. Tests 24 and 25 are also passing, but they probbably would ahve before this fix---just that they weren't being run earlier. --- glcpp-parse.y | 30 ++++++++++++++++++++++++++++-- glcpp.h | 1 + 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/glcpp-parse.y b/glcpp-parse.y index 02286cd8e0..60eaf215b8 100644 --- a/glcpp-parse.y +++ b/glcpp-parse.y @@ -471,7 +471,7 @@ _token_create_ival (void *ctx, int type, int ival) } void -_token_print (token_t *token) +_glcpp_parser_print_token (glcpp_parser_t *parser, token_t *token) { if (token->type < 256) { printf ("%c", token->type); @@ -527,6 +527,7 @@ _token_list_create (void *ctx) list = xtalloc (ctx, token_list_t); list->head = NULL; list->tail = NULL; + list->non_space_tail = NULL; return list; } @@ -548,6 +549,8 @@ _token_list_append (token_list_t *list, token_t *token) } list->tail = node; + if (token->type != SPACE) + list->non_space_tail = node; } void @@ -560,6 +563,25 @@ _token_list_append_list (token_list_t *list, token_list_t *tail) } list->tail = tail->tail; + list->non_space_tail = tail->non_space_tail; +} + +void +_token_list_trim_trailing_space (token_list_t *list) +{ + token_node_t *tail, *next; + + if (list->non_space_tail) { + tail = list->non_space_tail->next; + list->non_space_tail->next = NULL; + list->tail = list->non_space_tail; + + while (tail) { + next = tail->next; + talloc_free (tail); + tail = next; + } + } } void @@ -618,7 +640,7 @@ _glcpp_parser_print_expanded_token (glcpp_parser_t *parser, /* We only expand identifiers */ if (token->type != IDENTIFIER) { - _token_print (token); + _glcpp_parser_print_token (parser, token); return 0; } @@ -719,6 +741,8 @@ _arguments_parse (argument_list_t *arguments, token_node_t **node_ret) if (node->token->type == ',' && paren_count == 1) { + if (argument) + _token_list_trim_trailing_space (argument); argument = NULL; } else { @@ -834,6 +858,8 @@ _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, if (list == NULL) return; + _token_list_trim_trailing_space (list); + for (node = list->head; node; node = node->next) { if (_glcpp_parser_print_expanded_token (parser, node->token)) _glcpp_parser_print_expanded_function (parser, &node); diff --git a/glcpp.h b/glcpp.h index 6bd6e66a7c..21db918cdc 100644 --- a/glcpp.h +++ b/glcpp.h @@ -72,6 +72,7 @@ typedef struct token_node { struct token_list { token_node_t *head; token_node_t *tail; + token_node_t *non_space_tail; }; typedef struct argument_node { -- cgit v1.2.3