From 171e2936413e5b461537bfb6c9e28b0b807e3fed Mon Sep 17 00:00:00 2001 From: scrawl Date: Sat, 20 Feb 2016 19:26:59 +0100 Subject: [PATCH] ShaderManager: insert #line directives when including files --- components/shader/shadermanager.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/components/shader/shadermanager.cpp b/components/shader/shadermanager.cpp index 286642e57..ce77f46dc 100644 --- a/components/shader/shadermanager.cpp +++ b/components/shader/shadermanager.cpp @@ -2,12 +2,14 @@ #include #include +#include #include #include #include #include +#include #include @@ -21,8 +23,11 @@ namespace Shader bool parseIncludes(boost::filesystem::path shaderPath, std::string& source) { + boost::replace_all(source, "\r\n", "\n"); + std::set includedFiles; size_t foundPos = 0; + int fileNumber = 1; while ((foundPos = source.find("#include")) != std::string::npos) { size_t start = source.find('"', foundPos); @@ -46,9 +51,19 @@ namespace Shader std::cerr << "Failed to open " << includePath.string() << std::endl; return false; } + std::stringstream buffer; buffer << includeFstream.rdbuf(); - source.replace(foundPos, (end-foundPos+1), buffer.str()); + + // insert #line directives so we get correct line numbers in compiler errors + int includedFileNumber = fileNumber++; + + int lineNumber = std::count(source.begin(), source.begin() + foundPos, '\n'); + + std::stringstream toInsert; + toInsert << "#line 0 " << includedFileNumber << "\n" << buffer.str() << "\n#line " << lineNumber << " 0\n"; + + source.replace(foundPos, (end-foundPos+1), toInsert.str()); if (includedFiles.insert(includePath).second == false) {