[Mesa-dev] [PATCH 1/7] glcpp: Add support for __LINE__ and __FILE__ macros

Matt Turner mattst88 at gmail.com
Thu Nov 29 10:07:13 PST 2012


On Wed, Nov 28, 2012 at 9:38 PM, Carl Worth <cworth at cworth.org> wrote:
> These tokens are easy to expand by just looking at the current, tracked
> location values, (and no need to look anything up in the hash table).
>
> Add a test which verifies __LINE__ with several values, (and verifies __FILE__
> for the single value of 0). Our testing framework isn't so[hissticated enough
> here to have a test with multiple file inputs.
> ---
>  src/glsl/glcpp/glcpp-parse.y                       |   31 +++++++++++++++++++-
>  src/glsl/glcpp/tests/113-line-and-file-macros.c    |    7 +++++
>  .../tests/113-line-and-file-macros.c.expected      |    8 +++++
>  3 files changed, 45 insertions(+), 1 deletion(-)
>  create mode 100644 src/glsl/glcpp/tests/113-line-and-file-macros.c
>  create mode 100644 src/glsl/glcpp/tests/113-line-and-file-macros.c.expected
>
> diff --git a/src/glsl/glcpp/glcpp-parse.y b/src/glsl/glcpp/glcpp-parse.y
> index b0537c3..d928044 100644
> --- a/src/glsl/glcpp/glcpp-parse.y
> +++ b/src/glsl/glcpp/glcpp-parse.y
> @@ -1528,8 +1528,37 @@ _glcpp_parser_expand_node (glcpp_parser_t *parser,
>                 return NULL;
>         }
>
> -       /* Look up this identifier in the hash table. */
>         identifier = token->value.str;
> +
> +       /* Special handling for __LINE__ and __FILE__, (not through
> +        * the hash table). */
> +       if (strcmp(identifier, "__LINE__") == 0) {
> +               token_list_t *replacement;
> +               token_t *value;
> +
> +               replacement = _token_list_create (parser);
> +               value = _token_create_ival (parser, INTEGER,
> +                                           node->token->location.first_line);
> +               _token_list_append (replacement, value);
> +
> +               *last = node;
> +               return replacement;
> +       }
> +
> +       if (strcmp(identifier, "__FILE__") == 0) {
> +               token_list_t *replacement;
> +               token_t *value;
> +
> +               replacement = _token_list_create (parser);
> +               value = _token_create_ival (parser, INTEGER,
> +                                           node->token->location.source);
> +               _token_list_append (replacement, value);
> +
> +               *last = node;
> +               return replacement;
> +       }
> +
> +       /* Look up this identifier in the hash table. */
>         macro = hash_table_find (parser->defines, identifier);
>
>         /* Not a macro, so no expansion needed. */
> diff --git a/src/glsl/glcpp/tests/113-line-and-file-macros.c b/src/glsl/glcpp/tests/113-line-and-file-macros.c
> new file mode 100644
> index 0000000..369c487
> --- /dev/null
> +++ b/src/glsl/glcpp/tests/113-line-and-file-macros.c
> @@ -0,0 +1,7 @@
> +1. Number of dalmations: __LINE__ __FILE__ __LINE__
> +2. Nominal visual acuity: __LINE__ __FILE__ / __LINE__ __FILE__
> +3. Battle of Thermopylae, as film: __LINE__ __FILE__ __FILE__
> +4. HTTP code for "Not Found": __LINE__ __FILE__ __LINE__
> +5. Hexadecimal for 20560: __LINE__ __FILE__ __LINE__ __FILE__
> +6: Zip code for Nortonville, KS: __LINE__ __LINE__ __FILE__ __LINE__ __FILE__
> +7. James Bond, as a number: __FILE__ __FILE__ __LINE__
> diff --git a/src/glsl/glcpp/tests/113-line-and-file-macros.c.expected b/src/glsl/glcpp/tests/113-line-and-file-macros.c.expected
> new file mode 100644
> index 0000000..3562fb9
> --- /dev/null
> +++ b/src/glsl/glcpp/tests/113-line-and-file-macros.c.expected
> @@ -0,0 +1,8 @@
> +1. Number of dalmations: 1 0 1
> +2. Nominal visual acuity: 2 0 / 2 0
> +3. Battle of Thermopylae, as film: 3 0 0
> +4. HTTP code for "Not Found": 4 0 4
> +5. Hexadecimal for 20560: 5 0 5 0
> +6: Zip code for Nortonville, KS: 6 6 0 6 0
> +7. James Bond, as a number: 0 0 7
> +

#line __LINE__
__LINE__
#line __LINE__
__LINE__

when run through glcpp emits this:

#line 1
1
#line 2
2

which doesn't seem right and is what I was seeing when trying to
implement __LINE__. I didn't investigate too much.


More information about the mesa-dev mailing list