Home | History | Annotate | Download | only in glcpp
      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 	parser->info_log = hieralloc_asprintf_append(parser->info_log,
     37 						  "%u:%u(%u): "
     38 						  "preprocessor error: ",
     39 						  locp->source,
     40 						  locp->first_line,
     41 						  locp->first_column);
     42 	va_start(ap, fmt);
     43 	parser->info_log = hieralloc_vasprintf_append(parser->info_log, fmt, ap);
     44 	va_end(ap);
     45 	parser->info_log = hieralloc_strdup_append(parser->info_log, "\n");
     46 }
     47 
     48 void
     49 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...)
     50 {
     51 	va_list ap;
     52 
     53 	parser->info_log = hieralloc_asprintf_append(parser->info_log,
     54 						  "%u:%u(%u): "
     55 						  "preprocessor warning: ",
     56 						  locp->source,
     57 						  locp->first_line,
     58 						  locp->first_column);
     59 	va_start(ap, fmt);
     60 	parser->info_log = hieralloc_vasprintf_append(parser->info_log, fmt, ap);
     61 	va_end(ap);
     62 	parser->info_log = hieralloc_strdup_append(parser->info_log, "\n");
     63 }
     64 
     65 /* Searches backwards for '^ *#' from a given starting point. */
     66 static int
     67 in_directive(const char *shader, const char *ptr)
     68 {
     69 	assert(ptr >= shader);
     70 
     71 	/* Search backwards for '#'. If we find a \n first, it doesn't count */
     72 	for (; ptr >= shader && *ptr != '#'; ptr--) {
     73 		if (*ptr == '\n')
     74 			return 0;
     75 	}
     76 	if (ptr >= shader) {
     77 		/* Found '#'...look for spaces preceded by a newline */
     78 		for (ptr--; ptr >= shader && isblank(*ptr); ptr--);
     79 		// FIXME: I don't think the '\n' case can happen
     80 		if (ptr < shader || *ptr == '\n')
     81 			return 1;
     82 	}
     83 	return 0;
     84 }
     85 
     86 /* Remove any line continuation characters in preprocessing directives.
     87  * However, ignore any in GLSL code, as "There is no line continuation
     88  * character" (1.30 page 9) in GLSL.
     89  */
     90 static char *
     91 remove_line_continuations(glcpp_parser_t *ctx, const char *shader)
     92 {
     93 	int in_continued_line = 0;
     94 	int extra_newlines = 0;
     95 	char *clean = hieralloc_strdup(ctx, "");
     96 	const char *search_start = shader;
     97 	const char *newline;
     98 	while ((newline = strchr(search_start, '\n')) != NULL) {
     99 		const char *backslash = NULL;
    100 
    101 		/* # of characters preceding the newline. */
    102 		int n = newline - shader;
    103 
    104 		/* Find the preceding '\', if it exists */
    105 		if (n >= 1 && newline[-1] == '\\')
    106 			backslash = newline - 1;
    107 		else if (n >= 2 && newline[-1] == '\r' && newline[-2] == '\\')
    108 			backslash = newline - 2;
    109 
    110 		/* Double backslashes don't count (the backslash is escaped) */
    111 		if (backslash != NULL && backslash[-1] == '\\') {
    112 			backslash = NULL;
    113 		}
    114 
    115 		if (backslash != NULL) {
    116 			/* We found a line continuation, but do we care? */
    117 			if (!in_continued_line) {
    118 				if (in_directive(shader, backslash)) {
    119 					in_continued_line = 1;
    120 					extra_newlines = 0;
    121 				}
    122 			}
    123 			if (in_continued_line) {
    124 				/* Copy everything before the \ */
    125 				clean = hieralloc_strndup_append(clean, shader, backslash - shader);
    126 				shader = newline + 1;
    127 				extra_newlines++;
    128 			}
    129 		} else if (in_continued_line) {
    130 			/* Copy everything up to and including the \n */
    131 			clean = hieralloc_strndup_append(clean, shader, newline - shader + 1);
    132 			shader = newline + 1;
    133 			/* Output extra newlines to make line numbers match */
    134 			for (; extra_newlines > 0; extra_newlines--)
    135 				clean = hieralloc_strdup_append(clean, "\n");
    136 			in_continued_line = 0;
    137 		}
    138 		search_start = newline + 1;
    139 	}
    140 	clean = hieralloc_strdup_append(clean, shader);
    141 	return clean;
    142 }
    143 
    144 int
    145 preprocess(void *hieralloc_ctx, const char **shader, char **info_log,
    146 	   const struct gl_extensions *extensions, int api)
    147 {
    148 	int errors;
    149 	glcpp_parser_t *parser = glcpp_parser_create (extensions, api);
    150 	*shader = remove_line_continuations(parser, *shader);
    151 
    152 	glcpp_lex_set_source_string (parser, *shader);
    153 
    154 	glcpp_parser_parse (parser);
    155 
    156 	if (parser->skip_stack)
    157 		glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
    158 
    159 	*info_log = hieralloc_strdup_append(*info_log, parser->info_log);
    160 
    161 	hieralloc_steal(hieralloc_ctx, parser->output);
    162 	*shader = parser->output;
    163 
    164 	errors = parser->error;
    165 	glcpp_parser_destroy (parser);
    166 	return errors;
    167 }
    168