Beispiel #1
0
string path_source_replace_includes(const string& source,
                                    const string& path,
                                    const string& source_filename)
{
	/* Our own little c preprocessor that replaces #includes with the file
	 * contents, to work around issue of opencl drivers not supporting
	 * include paths with spaces in them.
	 */

	string result = "";
	vector<string> lines;
	string_split(lines, source, "\n", false);

	for(size_t i = 0; i < lines.size(); ++i) {
		string line = lines[i];
		if(line[0] == '#') {
			string token = string_strip(line.substr(1, line.size() - 1));
			if(string_startswith(token, "include")) {
				token = string_strip(token.substr(7, token.size() - 7));
				if(token[0] == '"') {
					size_t n_start = 1;
					size_t n_end = token.find("\"", n_start);
					string filename = token.substr(n_start, n_end - n_start);
					string text, filepath = path_join(path, filename);
					if(path_read_text(filepath, text)) {
						/* Replace include directories with both current path
						 * and path extracted from the include file.
						 * Not totally robust, but works fine for Cycles kernel
						 * and avoids having list of include directories.x
						 */
						text = path_source_replace_includes(
						        text, path_dirname(filepath), filename);
						text = path_source_replace_includes(text, path, filename);
						/* Use line directives for better error messages. */
						line = line_directive(filepath, 1)
						     + token.replace(0, n_end + 1, "\n" + text + "\n")
						     + line_directive(path_join(path, source_filename), i);
					}
				}
			}
		}
		result += line + "\n";
	}

	return result;
}
Beispiel #2
0
bool 
PreProcessor::pp()
{
	bool success = true;
	while (!tokenit.eof()) {
		const Token &t = tokenit.get(0);
		switch (t.id) {
		case _IDENT:
			identifier();
			break;
		case _PP_DEFINE:
			success = define_directive();
			break;
		case _PP_UNDEF:
			success = undef_directive();
			break;
		case _PP_INCLUDE:
			success = include_directive();
			break;
		case _PP_INCLUDE_NEXT:
			success = include_directive();
			break;
		case _PP_IF:
			success = if_directive();
			break;
		case _PP_ELIF:
			success = elif_directive();
			break;
		case _PP_IFDEF:
			success = ifdef_directive();
			break;
		case _PP_IFNDEF:
			success = ifndef_directive();
			break;
		case _PP_ELSE:
			success = else_directive();
			break;
		case _PP_ENDIF:
			success = endif_directive();
			break;
		case _PP_ERROR:
			success = error_directive();
			break;
		case _PP_WARNING:
			success = warning_directive();
			break;
		case _PP_LINE:
			success = line_directive();
			break;
		case _PP_PRAGMA:
			success = pragma_directive();
			break;
		default:
			tokenit.next();
			break;
		}
		if (!success) {
			break;
		}
	}
	if (!ifBlockEnables.empty()) {
		ERROR("expected 'endif'");
		success = false;
	}
	return success;
}