8769f4f7e55a1de926faa939139851962b435139
2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
28 #include "main/core.h" /* for isblank() on MSVC */
31 glcpp_error (YYLTYPE
*locp
, glcpp_parser_t
*parser
, const char *fmt
, ...)
36 parser
->info_log
= talloc_asprintf_append(parser
->info_log
,
38 "preprocessor error: ",
43 parser
->info_log
= talloc_vasprintf_append(parser
->info_log
, fmt
, ap
);
45 parser
->info_log
= talloc_strdup_append(parser
->info_log
, "\n");
49 glcpp_warning (YYLTYPE
*locp
, glcpp_parser_t
*parser
, const char *fmt
, ...)
53 parser
->info_log
= talloc_asprintf_append(parser
->info_log
,
55 "preprocessor warning: ",
60 parser
->info_log
= talloc_vasprintf_append(parser
->info_log
, fmt
, ap
);
62 parser
->info_log
= talloc_strdup_append(parser
->info_log
, "\n");
65 /* Searches backwards for '^ *#' from a given starting point. */
67 in_directive(const char *shader
, const char *ptr
)
69 assert(ptr
>= shader
);
71 /* Search backwards for '#'. If we find a \n first, it doesn't count */
72 for (; ptr
>= shader
&& *ptr
!= '#'; ptr
--) {
77 /* Found '#'...look for spaces preceded by a newline */
78 for (ptr
--; ptr
>= shader
&& isblank(*ptr
); ptr
--);
79 // FIXME: I don't think the '\n' case can happen
80 if (ptr
< shader
|| *ptr
== '\n')
86 /* Remove any line continuation characters in preprocessing directives.
87 * However, ignore any in GLSL code, as "There is no line continuation
88 * character" (1.30 page 9) in GLSL.
91 remove_line_continuations(glcpp_parser_t
*ctx
, const char *shader
)
93 int in_continued_line
= 0;
94 int extra_newlines
= 0;
95 char *clean
= talloc_strdup(ctx
, "");
96 const char *search_start
= shader
;
98 while ((newline
= strchr(search_start
, '\n')) != NULL
) {
99 const char *backslash
= NULL
;
101 /* # of characters preceding the newline. */
102 int n
= newline
- shader
;
104 /* Find the preceding '\', if it exists */
105 if (n
>= 1 && newline
[-1] == '\\')
106 backslash
= newline
- 1;
107 else if (n
>= 2 && newline
[-1] == '\r' && newline
[-2] == '\\')
108 backslash
= newline
- 2;
110 /* Double backslashes don't count (the backslash is escaped) */
111 if (backslash
!= NULL
&& backslash
[-1] == '\\') {
115 if (backslash
!= NULL
) {
116 /* We found a line continuation, but do we care? */
117 if (!in_continued_line
) {
118 if (in_directive(shader
, backslash
)) {
119 in_continued_line
= 1;
123 if (in_continued_line
) {
124 /* Copy everything before the \ */
125 clean
= talloc_strndup_append(clean
, shader
, backslash
- shader
);
126 shader
= newline
+ 1;
129 } else if (in_continued_line
) {
130 /* Copy everything up to and including the \n */
131 clean
= talloc_strndup_append(clean
, shader
, newline
- shader
+ 1);
132 shader
= newline
+ 1;
133 /* Output extra newlines to make line numbers match */
134 for (; extra_newlines
> 0; extra_newlines
--)
135 clean
= talloc_strdup_append(clean
, "\n");
136 in_continued_line
= 0;
138 search_start
= newline
+ 1;
140 clean
= talloc_strdup_append(clean
, shader
);
145 preprocess(void *talloc_ctx
, const char **shader
, char **info_log
,
146 const struct gl_extensions
*extensions
)
149 glcpp_parser_t
*parser
= glcpp_parser_create (extensions
);
150 *shader
= remove_line_continuations(parser
, *shader
);
152 glcpp_lex_set_source_string (parser
, *shader
);
154 glcpp_parser_parse (parser
);
156 if (parser
->skip_stack
)
157 glcpp_error (&parser
->skip_stack
->loc
, parser
, "Unterminated #if\n");
159 *info_log
= talloc_strdup_append(*info_log
, parser
->info_log
);
161 talloc_steal(talloc_ctx
, parser
->output
);
162 *shader
= parser
->output
;
164 errors
= parser
->error
;
165 glcpp_parser_destroy (parser
);