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_append(&parser->info_log, "%u:%u(%u): "
37 "preprocessor error: ",
42 ralloc_vasprintf_append(&parser->info_log, fmt, ap);
44 ralloc_strcat(&parser->info_log, "\n");
48 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...)
52 ralloc_asprintf_append(&parser->info_log, "%u:%u(%u): "
53 "preprocessor warning: ",
58 ralloc_vasprintf_append(&parser->info_log, fmt, ap);
60 ralloc_strcat(&parser->info_log, "\n");
63 /* Searches backwards for '^ *#' from a given starting point. */
65 in_directive(const char *shader, const char *ptr)
67 assert(ptr >= shader);
69 /* Search backwards for '#'. If we find a \n first, it doesn't count */
70 for (; ptr >= shader && *ptr != '#'; ptr--) {
75 /* Found '#'...look for spaces preceded by a newline */
76 for (ptr--; ptr >= shader && isblank(*ptr); ptr--);
77 // FIXME: I don't think the '\n' case can happen
78 if (ptr < shader || *ptr == '\n')
84 /* Remove any line continuation characters in preprocessing directives.
85 * However, ignore any in GLSL code, as "There is no line continuation
86 * character" (1.30 page 9) in GLSL.
89 remove_line_continuations(glcpp_parser_t *ctx, const char *shader)
91 int in_continued_line = 0;
92 int extra_newlines = 0;
93 char *clean = ralloc_strdup(ctx, "");
94 const char *search_start = shader;
96 while ((newline = strchr(search_start, '\n')) != NULL) {
97 const char *backslash = NULL;
99 /* # of characters preceding the newline. */
100 int n = newline - shader;
102 /* Find the preceding '\', if it exists */
103 if (n >= 1 && newline[-1] == '\\')
104 backslash = newline - 1;
105 else if (n >= 2 && newline[-1] == '\r' && newline[-2] == '\\')
106 backslash = newline - 2;
108 /* Double backslashes don't count (the backslash is escaped) */
109 if (backslash != NULL && backslash[-1] == '\\') {
113 if (backslash != NULL) {
114 /* We found a line continuation, but do we care? */
115 if (!in_continued_line) {
116 if (in_directive(shader, backslash)) {
117 in_continued_line = 1;
121 if (in_continued_line) {
122 /* Copy everything before the \ */
123 ralloc_strncat(&clean, shader, backslash - shader);
124 shader = newline + 1;
127 } else if (in_continued_line) {
128 /* Copy everything up to and including the \n */
129 ralloc_strncat(&clean, shader, newline - shader + 1);
130 shader = newline + 1;
131 /* Output extra newlines to make line numbers match */
132 for (; extra_newlines > 0; extra_newlines--)
133 ralloc_strcat(&clean, "\n");
134 in_continued_line = 0;
136 search_start = newline + 1;
138 ralloc_strcat(&clean, shader);
143 preprocess(void *ralloc_ctx, const char **shader, char **info_log,
144 const struct gl_extensions *extensions, int api)
147 glcpp_parser_t *parser = glcpp_parser_create (extensions, api);
148 *shader = remove_line_continuations(parser, *shader);
150 glcpp_lex_set_source_string (parser, *shader);
152 glcpp_parser_parse (parser);
154 if (parser->skip_stack)
155 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
157 ralloc_strcat(info_log, parser->info_log);
159 ralloc_steal(ralloc_ctx, parser->output);
160 *shader = parser->output;
162 errors = parser->error;
163 glcpp_parser_destroy (parser);