Module: Mesa Branch: staging/23.0 Commit: 09c6627fa3b11bebc52b43c7548522e443ebe9e3 URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=09c6627fa3b11bebc52b43c7548522e443ebe9e3
Author: Timothy Arceri <[email protected]> Date: Wed Feb 15 23:14:13 2023 +1100 glsl: isolate object macro replacments Here we use a leading space to isolate them from the code they will be inserted into. For example: #define VALUE -1.0 int a = -VALUE; Should be evaluated to int a = - -1.0; not int a = --1.0; Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/7932 Cc: mesa-stable Reviewed-by: Ian Romanick <[email protected]> Reviewed-by: Tapani Pälli <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21352> (cherry picked from commit 3a9edfc4943d28894b0a39b6ee3350e57a979d79) --- .pick_status.json | 2 +- src/compiler/glsl/glcpp/glcpp-parse.y | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index dd858b2a1ac..9d5525cefc1 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -1030,7 +1030,7 @@ "description": "glsl: isolate object macro replacments", "nominated": true, "nomination_type": 0, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": null }, diff --git a/src/compiler/glsl/glcpp/glcpp-parse.y b/src/compiler/glsl/glcpp/glcpp-parse.y index e94971e5e91..3638560f1bd 100644 --- a/src/compiler/glsl/glcpp/glcpp-parse.y +++ b/src/compiler/glsl/glcpp/glcpp-parse.y @@ -1969,8 +1969,8 @@ _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node, */ static token_list_t * _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node, - token_node_t **last, expansion_mode_t mode, - int line) + token_node_t *node_prev, token_node_t **last, + expansion_mode_t mode, int line) { token_t *token = node->token; const char *identifier; @@ -2033,6 +2033,22 @@ _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node, return _token_list_create_with_one_space(parser); replacement = _token_list_copy(parser, macro->replacements); + + /* If needed insert space in front of replacements to isolate them from + * the code they will be inserted into. For example: + * + * #define VALUE -1.0 + * int a = -VALUE; + * + * Should be evaluated to int a = - -1.0; not int a = --1.0; + */ + if (node_prev && + (node_prev->token->type == '-' || node_prev->token->type == '+') && + node_prev->token->type == replacement->head->token->type) { + token_t *new_token = _token_create_ival(parser, SPACE, SPACE); + _token_list_prepend(parser, replacement, new_token); + } + _glcpp_parser_apply_pastes(parser, replacement); return replacement; } @@ -2139,7 +2155,8 @@ _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list, while (parser->active && parser->active->marker == node) _parser_active_list_pop (parser); - expansion = _glcpp_parser_expand_node (parser, node, &last, mode, line); + expansion = + _glcpp_parser_expand_node(parser, node, node_prev, &last, mode, line); if (expansion) { token_node_t *n;
