glcpp: Rewrite line-continuation support to act globally.
[mesa.git] / src / glsl / glcpp / pp.c
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include <assert.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "glcpp.h"
28 #include "main/core.h" /* for isblank() on MSVC */
29
30 void
31 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...)
32 {
33 va_list ap;
34
35 parser->error = 1;
36 ralloc_asprintf_rewrite_tail(&parser->info_log,
37 &parser->info_log_length,
38 "%u:%u(%u): "
39 "preprocessor error: ",
40 locp->source,
41 locp->first_line,
42 locp->first_column);
43 va_start(ap, fmt);
44 ralloc_vasprintf_rewrite_tail(&parser->info_log,
45 &parser->info_log_length,
46 fmt, ap);
47 va_end(ap);
48 ralloc_asprintf_rewrite_tail(&parser->info_log,
49 &parser->info_log_length, "\n");
50 }
51
52 void
53 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...)
54 {
55 va_list ap;
56
57 ralloc_asprintf_rewrite_tail(&parser->info_log,
58 &parser->info_log_length,
59 "%u:%u(%u): "
60 "preprocessor warning: ",
61 locp->source,
62 locp->first_line,
63 locp->first_column);
64 va_start(ap, fmt);
65 ralloc_vasprintf_rewrite_tail(&parser->info_log,
66 &parser->info_log_length,
67 fmt, ap);
68 va_end(ap);
69 ralloc_asprintf_rewrite_tail(&parser->info_log,
70 &parser->info_log_length, "\n");
71 }
72
73 /* Remove any line continuation characters in the shader, (whether in
74 * preprocessing directives or in GLSL code).
75 */
76 static char *
77 remove_line_continuations(glcpp_parser_t *ctx, const char *shader)
78 {
79 char *clean = ralloc_strdup(ctx, "");
80 const char *backslash, *newline;
81 int collapsed_newlines = 0;
82
83 while (true) {
84 backslash = strchr(shader, '\\');
85
86 /* If we have previously collapsed any line-continuations,
87 * then we want to insert additional newlines at the next
88 * occurrence of a newline character to avoid changing any
89 * line numbers.
90 */
91 if (collapsed_newlines) {
92 newline = strchr(shader, '\n');
93 if (newline &&
94 (backslash == NULL || newline < backslash))
95 {
96 ralloc_strncat(&clean, shader,
97 newline - shader + 1);
98 while (collapsed_newlines--)
99 ralloc_strcat(&clean, "\n");
100 shader = newline + 1;
101 }
102 }
103
104 if (backslash == NULL)
105 break;
106
107 /* At each line continuation, (backslash followed by a
108 * newline), copy all preceding text to the output, then
109 * advance the shader pointer to the character after the
110 * newline.
111 */
112 if (backslash[1] == '\n' ||
113 (backslash[1] == '\r' && backslash[2] == '\n'))
114 {
115 collapsed_newlines++;
116 ralloc_strncat(&clean, shader, backslash - shader);
117 if (backslash[1] == '\n')
118 shader = backslash + 2;
119 else
120 shader = backslash + 3;
121 }
122 }
123
124 ralloc_strcat(&clean, shader);
125
126 return clean;
127 }
128
129 int
130 glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
131 const struct gl_extensions *extensions, int api)
132 {
133 int errors;
134 glcpp_parser_t *parser = glcpp_parser_create (extensions, api);
135 *shader = remove_line_continuations(parser, *shader);
136
137 glcpp_lex_set_source_string (parser, *shader);
138
139 glcpp_parser_parse (parser);
140
141 if (parser->skip_stack)
142 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
143
144 ralloc_strcat(info_log, parser->info_log);
145
146 ralloc_steal(ralloc_ctx, parser->output);
147 *shader = parser->output;
148
149 errors = parser->error;
150 glcpp_parser_destroy (parser);
151 return errors;
152 }