From 5adcb87c948a5e7da7e079bfd79fcd9ad3d733b7 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Tue, 6 Nov 2012 15:29:13 -0800 Subject: [PATCH] glcpp: Handle newlines and #line correctly Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=51506 Fixes: preprocess2_frag.test from oglconform Reviewed-by: Matt Turner --- src/glsl/glcpp/glcpp-parse.y | 116 ++++++++++++++----------- src/glsl/glcpp/tests/091-hash-line.c.expected | 2 +- 2 files changed, 65 insertions(+), 53 deletions(-) diff --git a/src/glsl/glcpp/glcpp-parse.y b/src/glsl/glcpp/glcpp-parse.y index 8025c06..7c1cba2 100644 --- a/src/glsl/glcpp/glcpp-parse.y +++ b/src/glsl/glcpp/glcpp-parse.y @@ -190,12 +190,15 @@ line: control_line { ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n"); } +| control_line_to_expand | text_line { _glcpp_parser_print_expanded_token_list (parser, $1); ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n"); ralloc_free ($1); } -| expanded_line +| expanded_line { + ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n"); + } | HASH non_directive ; @@ -211,7 +214,7 @@ expanded_line: parser->new_line_number = $2; ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, - "#line %" PRIiMAX "\n", + "#line %" PRIiMAX, $2); } | LINE_EXPANDED integer_constant integer_constant NEWLINE { @@ -221,7 +224,7 @@ expanded_line: parser->new_source_number = $3; ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, - "#line %" PRIiMAX " %" PRIiMAX "\n", + "#line %" PRIiMAX " %" PRIiMAX, $2, $3); } ; @@ -244,34 +247,6 @@ control_line: } ralloc_free ($2); } -| HASH_LINE pp_tokens NEWLINE { - if (parser->skip_stack == NULL || - parser->skip_stack->type == SKIP_NO_SKIP) - { - _glcpp_parser_expand_and_lex_from (parser, - LINE_EXPANDED, $2); - } - } -| HASH_IF conditional_tokens NEWLINE { - /* Be careful to only evaluate the 'if' expression if - * we are not skipping. When we are skipping, we - * simply push a new 0-valued 'if' onto the skip - * stack. - * - * This avoids generating diagnostics for invalid - * expressions that are being skipped. */ - if (parser->skip_stack == NULL || - parser->skip_stack->type == SKIP_NO_SKIP) - { - _glcpp_parser_expand_and_lex_from (parser, - IF_EXPANDED, $2); - } - else - { - _glcpp_parser_skip_stack_push_if (parser, & @1, 0); - parser->skip_stack->type = SKIP_TO_ENDIF; - } - } | HASH_IF NEWLINE { /* #if without an expression is only an error if we * are not skipping */ @@ -279,7 +254,7 @@ control_line: parser->skip_stack->type == SKIP_NO_SKIP) { glcpp_error(& @1, parser, "#if with no expression"); - } + } _glcpp_parser_skip_stack_push_if (parser, & @1, 0); } | HASH_IFDEF IDENTIFIER junk NEWLINE { @@ -292,26 +267,6 @@ control_line: ralloc_free ($2); _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL); } -| HASH_ELIF conditional_tokens NEWLINE { - /* Be careful to only evaluate the 'elif' expression - * if we are not skipping. When we are skipping, we - * simply change to a 0-valued 'elif' on the skip - * stack. - * - * This avoids generating diagnostics for invalid - * expressions that are being skipped. */ - if (parser->skip_stack && - parser->skip_stack->type == SKIP_TO_ELSE) - { - _glcpp_parser_expand_and_lex_from (parser, - ELIF_EXPANDED, $2); - } - else - { - _glcpp_parser_skip_stack_change_if (parser, & @1, - "elif", 0); - } - } | HASH_ELIF NEWLINE { /* #elif without an expression is an error unless we * are skipping. */ @@ -357,6 +312,63 @@ control_line: | HASH NEWLINE ; +control_line_to_expand: + HASH_LINE pp_tokens NEWLINE { + if (parser->skip_stack == NULL || + parser->skip_stack->type == SKIP_NO_SKIP) + { + _glcpp_parser_expand_and_lex_from (parser, + LINE_EXPANDED, $2); + } + } +| HASH_IF conditional_tokens NEWLINE { + /* Be careful to only evaluate the 'if' expression if + * we are not skipping. When we are skipping, we + * simply push a new 0-valued 'if' onto the skip + * stack. + * + * This avoids generating diagnostics for invalid + * expressions that are being skipped. */ + if (parser->skip_stack == NULL || + parser->skip_stack->type == SKIP_NO_SKIP) + { + _glcpp_parser_expand_and_lex_from (parser, + IF_EXPANDED, $2); + } + else + { + _glcpp_parser_skip_stack_push_if (parser, & @1, 0); + parser->skip_stack->type = SKIP_TO_ENDIF; + ralloc_asprintf_rewrite_tail (&parser->output, + &parser->output_length, + "\n"); + } + } +| HASH_ELIF conditional_tokens NEWLINE { + /* Be careful to only evaluate the 'elif' expression + * if we are not skipping. When we are skipping, we + * simply change to a 0-valued 'elif' on the skip + * stack. + * + * This avoids generating diagnostics for invalid + * expressions that are being skipped. */ + if (parser->skip_stack && + parser->skip_stack->type == SKIP_TO_ELSE) + { + _glcpp_parser_expand_and_lex_from (parser, + ELIF_EXPANDED, $2); + } + else + { + _glcpp_parser_skip_stack_change_if (parser, & @1, + "elif", 0); + ralloc_asprintf_rewrite_tail (&parser->output, + &parser->output_length, + "\n"); + } + } +; + integer_constant: INTEGER_STRING { if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) { diff --git a/src/glsl/glcpp/tests/091-hash-line.c.expected b/src/glsl/glcpp/tests/091-hash-line.c.expected index 976d178..48af0b2 100644 --- a/src/glsl/glcpp/tests/091-hash-line.c.expected +++ b/src/glsl/glcpp/tests/091-hash-line.c.expected @@ -2,7 +2,6 @@ 0:25(1): preprocessor error: #error line 25 error 1:0(1): preprocessor error: #error source 1, line 0 error 2:30(1): preprocessor error: #error source 2, line 30 error - #line 0 #line 25 @@ -17,3 +16,4 @@ #line 90 2 #line 180 2 + -- 1.7.8.6