Merge remote branch 'origin/master' into glsl2
[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 <ctype.h>
26 #include "glcpp.h"
27
28 void
29 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...)
30 {
31 parser->error = 1;
32 parser->info_log = talloc_asprintf_append(parser->info_log,
33 "%u:%u(%u): "
34 "preprocessor error: ",
35 locp->source,
36 locp->first_line,
37 locp->first_column);
38 va_list ap;
39 va_start(ap, fmt);
40 parser->info_log = talloc_vasprintf_append(parser->info_log, fmt, ap);
41 va_end(ap);
42 parser->info_log = talloc_strdup_append(parser->info_log, "\n");
43 }
44
45 void
46 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...)
47 {
48 parser->info_log = talloc_asprintf_append(parser->info_log,
49 "%u:%u(%u): "
50 "preprocessor warning: ",
51 locp->source,
52 locp->first_line,
53 locp->first_column);
54 va_list ap;
55 va_start(ap, fmt);
56 parser->info_log = talloc_vasprintf_append(parser->info_log, fmt, ap);
57 va_end(ap);
58 parser->info_log = talloc_strdup_append(parser->info_log, "\n");
59 }
60
61 /* Searches backwards for '^ *#' from a given starting point. */
62 static int
63 in_directive(const char *shader, const char *ptr)
64 {
65 assert(ptr >= shader);
66
67 /* Search backwards for '#'. If we find a \n first, it doesn't count */
68 for (; ptr >= shader && *ptr != '#'; ptr--) {
69 if (*ptr == '\n')
70 return 0;
71 }
72 if (ptr >= shader) {
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')
77 return 1;
78 }
79 return 0;
80 }
81
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.
85 */
86 static char *
87 remove_line_continuations(glcpp_parser_t *ctx, const char *shader)
88 {
89 int in_continued_line = 0;
90 int extra_newlines = 0;
91 char *clean = talloc_strdup(ctx, "");
92 const char *search_start = shader;
93 const char *newline;
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;
101 }
102 /* Double backslashes don't count (the backslash is escaped) */
103 if (backslash != NULL && backslash[-1] == '\\') {
104 backslash = NULL;
105 }
106
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;
112 extra_newlines = 0;
113 }
114 }
115 if (in_continued_line) {
116 /* Copy everything before the \ */
117 clean = talloc_strndup_append(clean, shader, backslash - shader);
118 shader = newline + 1;
119 extra_newlines++;
120 }
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;
129 }
130 search_start = newline + 1;
131 }
132 clean = talloc_strdup_append(clean, shader);
133 return clean;
134 }
135
136 extern int
137 preprocess(void *talloc_ctx, const char **shader, char **info_log,
138 const struct gl_extensions *extensions)
139 {
140 int errors;
141 glcpp_parser_t *parser = glcpp_parser_create (extensions);
142 *shader = remove_line_continuations(parser, *shader);
143
144 glcpp_lex_set_source_string (parser, *shader);
145
146 glcpp_parser_parse (parser);
147
148 *info_log = talloc_strdup_append(*info_log, parser->info_log);
149
150 talloc_steal(talloc_ctx, parser->output);
151 *shader = parser->output;
152
153 errors = parser->error;
154 glcpp_parser_destroy (parser);
155 return errors;
156 }