| 178 | #if USE_BOOST_WAVE |
| 179 | |
| 180 | bool |
| 181 | OSLCompilerImpl::preprocess_buffer (const std::string &buffer, |
| 182 | const std::string &filename, |
| 183 | const std::string &stdoslpath, |
| 184 | const std::vector<std::string> &defines, |
| 185 | const std::vector<std::string> &includepaths, |
| 186 | std::string &result) |
| 187 | { |
| 188 | std::ostringstream ss; |
| 189 | boost::wave::util::file_position_type current_position; |
| 190 | |
| 191 | std::string instring; |
| 192 | if (!stdoslpath.empty()) |
| 193 | instring = OIIO::Strutil::format("#include \"%s\"\n", stdoslpath.c_str()); |
| 194 | else |
| 195 | instring = "\n"; |
| 196 | instring += buffer; |
| 197 | |
| 198 | try { |
| 199 | typedef boost::wave::cpplexer::lex_token<> token_type; |
| 200 | typedef boost::wave::cpplexer::lex_iterator<token_type> lex_iterator_type; |
| 201 | typedef boost::wave::context<std::string::iterator, lex_iterator_type> context_type; |
| 202 | |
| 203 | // Setup wave context |
| 204 | context_type ctx (instring.begin(), instring.end(), filename.c_str()); |
| 205 | |
| 206 | // Turn on support of variadic macros, e.g. #define FOO(...) __VA_ARGS__ |
| 207 | // Turn off whitespace insertion. |
| 208 | boost::wave::language_support lang = boost::wave::language_support ( |
| 209 | (ctx.get_language() | boost::wave::support_option_variadics) |
| 210 | & ~boost::wave::language_support::support_option_insert_whitespace); |
| 211 | ctx.set_language (lang); |
| 212 | |
| 213 | ctx.add_macro_definition (OIIO::Strutil::format("OSL_VERSION_MAJOR=%d", |
| 214 | OSL_LIBRARY_VERSION_MAJOR).c_str()); |
| 215 | ctx.add_macro_definition (OIIO::Strutil::format("OSL_VERSION_MINOR=%d", |
| 216 | OSL_LIBRARY_VERSION_MINOR).c_str()); |
| 217 | ctx.add_macro_definition (OIIO::Strutil::format("OSL_VERSION_PATCH=%d", |
| 218 | OSL_LIBRARY_VERSION_PATCH).c_str()); |
| 219 | ctx.add_macro_definition (OIIO::Strutil::format("OSL_VERSION=%d", |
| 220 | OSL_LIBRARY_VERSION_CODE).c_str()); |
| 221 | for (size_t i = 0; i < defines.size(); ++i) { |
| 222 | if (defines[i][1] == 'D') |
| 223 | ctx.add_macro_definition (defines[i].c_str()+2); |
| 224 | else if (defines[i][1] == 'U') |
| 225 | ctx.remove_macro_definition (defines[i].c_str()+2); |
| 226 | } |
| 227 | for (size_t i = 0; i < includepaths.size(); ++i) { |
| 228 | ctx.add_sysinclude_path (includepaths[i].c_str()); |
| 229 | ctx.add_include_path (includepaths[i].c_str()); |
| 230 | } |
| 231 | |
| 232 | context_type::iterator_type first = ctx.begin(); |
| 233 | context_type::iterator_type last = ctx.end(); |
| 234 | |
| 235 | #if 0 |
| 236 | // N.B. The force_include() method is buggy, see |
| 237 | // https://svn.boost.org/trac/boost/ticket/6838 |