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 ralloc_asprintf_rewrite_tail(&parser
->info_log
,
37 &parser
->info_log_length
,
39 "preprocessor error: ",
44 ralloc_vasprintf_rewrite_tail(&parser
->info_log
,
45 &parser
->info_log_length
,
48 ralloc_asprintf_rewrite_tail(&parser
->info_log
,
49 &parser
->info_log_length
, "\n");
53 glcpp_warning (YYLTYPE
*locp
, glcpp_parser_t
*parser
, const char *fmt
, ...)
57 ralloc_asprintf_rewrite_tail(&parser
->info_log
,
58 &parser
->info_log_length
,
60 "preprocessor warning: ",
65 ralloc_vasprintf_rewrite_tail(&parser
->info_log
,
66 &parser
->info_log_length
,
69 ralloc_asprintf_rewrite_tail(&parser
->info_log
,
70 &parser
->info_log_length
, "\n");
73 /* Remove any line continuation characters in the shader, (whether in
74 * preprocessing directives or in GLSL code).
77 remove_line_continuations(glcpp_parser_t
*ctx
, const char *shader
)
79 char *clean
= ralloc_strdup(ctx
, "");
80 const char *backslash
, *newline
, *search_start
;
81 int collapsed_newlines
= 0;
83 search_start
= shader
;
86 backslash
= strchr(search_start
, '\\');
88 /* If we have previously collapsed any line-continuations,
89 * then we want to insert additional newlines at the next
90 * occurrence of a newline character to avoid changing any
93 if (collapsed_newlines
) {
94 newline
= strchr(search_start
, '\n');
96 (backslash
== NULL
|| newline
< backslash
))
98 ralloc_strncat(&clean
, shader
,
99 newline
- shader
+ 1);
100 while (collapsed_newlines
--)
101 ralloc_strcat(&clean
, "\n");
102 shader
= newline
+ 1;
103 search_start
= shader
;
107 search_start
= backslash
+ 1;
109 if (backslash
== NULL
)
112 /* At each line continuation, (backslash followed by a
113 * newline), copy all preceding text to the output, then
114 * advance the shader pointer to the character after the
117 if (backslash
[1] == '\n' ||
118 (backslash
[1] == '\r' && backslash
[2] == '\n'))
120 collapsed_newlines
++;
121 ralloc_strncat(&clean
, shader
, backslash
- shader
);
122 if (backslash
[1] == '\n')
123 shader
= backslash
+ 2;
125 shader
= backslash
+ 3;
126 search_start
= shader
;
130 ralloc_strcat(&clean
, shader
);
136 glcpp_preprocess(void *ralloc_ctx
, const char **shader
, char **info_log
,
137 const struct gl_extensions
*extensions
, struct gl_context
*gl_ctx
)
140 glcpp_parser_t
*parser
= glcpp_parser_create (extensions
, gl_ctx
->API
);
142 if (! gl_ctx
->Const
.DisableGLSLLineContinuations
)
143 *shader
= remove_line_continuations(parser
, *shader
);
145 glcpp_lex_set_source_string (parser
, *shader
);
147 glcpp_parser_parse (parser
);
149 if (parser
->skip_stack
)
150 glcpp_error (&parser
->skip_stack
->loc
, parser
, "Unterminated #if\n");
152 ralloc_strcat(info_log
, parser
->info_log
);
154 ralloc_steal(ralloc_ctx
, parser
->output
);
155 *shader
= parser
->output
;
157 errors
= parser
->error
;
158 glcpp_parser_destroy (parser
);