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.
29 glcpp_error (YYLTYPE
*locp
, glcpp_parser_t
*parser
, const char *fmt
, ...)
32 parser
->info_log
= talloc_asprintf_append(parser
->info_log
,
34 "preprocessor error: ",
40 parser
->info_log
= talloc_vasprintf_append(parser
->info_log
, fmt
, ap
);
42 parser
->info_log
= talloc_strdup_append(parser
->info_log
, "\n");
46 glcpp_warning (YYLTYPE
*locp
, glcpp_parser_t
*parser
, const char *fmt
, ...)
48 parser
->info_log
= talloc_asprintf_append(parser
->info_log
,
50 "preprocessor warning: ",
56 parser
->info_log
= talloc_vasprintf_append(parser
->info_log
, fmt
, ap
);
58 parser
->info_log
= talloc_strdup_append(parser
->info_log
, "\n");
61 /* Searches backwards for '^ *#' from a given starting point. */
63 in_directive(const char *shader
, const char *ptr
)
65 assert(ptr
>= shader
);
67 /* Search backwards for '#'. If we find a \n first, it doesn't count */
68 for (; ptr
>= shader
&& *ptr
!= '#'; ptr
--) {
73 /* Found '#'...look for spaces preceded by a newline */
74 for (ptr
--; ptr
>= shader
&& isblank(*ptr
); ptr
--);
75 // FIXME: I don't think the '\n' case can happen
76 if (ptr
< shader
|| *ptr
== '\n')
82 /* Remove any line continuation characters in preprocessing directives.
83 * However, ignore any in GLSL code, as "There is no line continuation
84 * character" (1.30 page 9) in GLSL.
87 remove_line_continuations(glcpp_parser_t
*ctx
, const char *shader
)
89 int in_continued_line
= 0;
90 int extra_newlines
= 0;
91 char *clean
= talloc_strdup(ctx
, "");
92 const char *search_start
= shader
;
94 while ((newline
= strchr(search_start
, '\n')) != NULL
) {
95 const char *backslash
= NULL
;
96 /* Find the preceding '\', if it exists */
97 if (newline
[-1] == '\\') {
98 backslash
= newline
- 1;
99 } else if (newline
[-1] == '\r' && newline
[-2] == '\\') {
100 backslash
= newline
- 2;
102 /* Double backslashes don't count (the backslash is escaped) */
103 if (backslash
!= NULL
&& backslash
[-1] == '\\') {
107 if (backslash
!= NULL
) {
108 /* We found a line continuation, but do we care? */
109 if (!in_continued_line
) {
110 if (in_directive(shader
, backslash
)) {
111 in_continued_line
= 1;
115 if (in_continued_line
) {
116 /* Copy everything before the \ */
117 clean
= talloc_strndup_append(clean
, shader
, backslash
- shader
);
118 shader
= newline
+ 1;
121 } else if (in_continued_line
) {
122 /* Copy everything up to and including the \n */
123 clean
= talloc_strndup_append(clean
, shader
, newline
- shader
+ 1);
124 shader
= newline
+ 1;
125 /* Output extra newlines to make line numbers match */
126 for (; extra_newlines
> 0; extra_newlines
--)
127 clean
= talloc_strdup_append(clean
, "\n");
128 in_continued_line
= 0;
130 search_start
= newline
+ 1;
132 clean
= talloc_strdup_append(clean
, shader
);
137 preprocess(void *talloc_ctx
, const char **shader
, char **info_log
,
138 const struct gl_extensions
*extensions
)
141 glcpp_parser_t
*parser
= glcpp_parser_create (extensions
);
142 *shader
= remove_line_continuations(parser
, *shader
);
144 glcpp_lex_set_source_string (parser
, *shader
);
146 glcpp_parser_parse (parser
);
148 *info_log
= talloc_strdup_append(*info_log
, parser
->info_log
);
150 talloc_steal(talloc_ctx
, parser
->output
);
151 *shader
= parser
->output
;
153 errors
= parser
->error
;
154 glcpp_parser_destroy (parser
);