From: Mikko Rasa Date: Sat, 20 Feb 2021 12:42:19 +0000 (+0200) Subject: Track source names in SL::Module X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9d798ac368bfd236a7632a3a15e51bd1112ea63d;p=libs%2Fgl.git Track source names in SL::Module Error translation has also been moved to a separate class. This will allow moving the creation of shaders to Program. --- diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 0579bb01..347e16ad 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -1,8 +1,6 @@ #include #include #include -#include -#include #include "compatibility.h" #include "compiler.h" #include "error.h" @@ -34,6 +32,7 @@ void Compiler::clear() delete module; module = new Module(); imported_names.clear(); + module->source_map.set_name(0, ""); } void Compiler::set_source(const string &source, const string &src_name) @@ -104,44 +103,14 @@ void Compiler::add_shaders(Program &program) } catch(const compile_error &e) { - static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$"); - vector lines = split(e.what(), '\n'); - string translated; - for(vector::const_iterator i=lines.begin(); i!=lines.end(); ++i) - { - RegMatch m = r_message.match(*i); - if(m) - { - unsigned index = 0; - unsigned line = 0; - if(m[2]) - { - index = lexical_cast(m[2].str); - line = lexical_cast(m[3].str); - } - else if(m[4]) - { - index = lexical_cast(m[4].str); - line = lexical_cast(m[5].str); - } - const char *src = ""; - if(index==0) - src = ""; - else if(index-1source_map.translate_errors(e.what())); } } void Compiler::append_module(Module &mod, DataFile::Collection *res) { + module->source_map.merge_from(mod.source_map); + vector imports = NodeGatherer().apply(mod.shared); for(vector::iterator i=imports.begin(); i!=imports.end(); ++i) import(res, (*i)->module); @@ -192,7 +161,7 @@ void Compiler::import(DataFile::Collection *resources, const string &name) if(!io) throw runtime_error(format("module %s not found", name)); Parser import_parser; - append_module(import_parser.parse(*io, fn, imported_names.size()), resources); + append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources); } void Compiler::generate(Stage &stage) diff --git a/source/glsl/parser.cpp b/source/glsl/parser.cpp index 60cc3516..eee62129 100644 --- a/source/glsl/parser.cpp +++ b/source/glsl/parser.cpp @@ -28,30 +28,30 @@ Parser::~Parser() Module &Parser::parse(const string &s, const string &n, unsigned i) { source = s; - source_index = i; - parse_source(n); + parse_source(n, i); return *module; } Module &Parser::parse(IO::Base &io, const string &n, unsigned i) { source = string(); - source_index = i; while(!io.eof()) { char buffer[4096]; unsigned len = io.read(buffer, sizeof(buffer)); source.append(buffer, len); } - parse_source(n); + parse_source(n, i); return *module; } -void Parser::parse_source(const string &name) +void Parser::parse_source(const string &name, unsigned index) { delete module; module = new Module; cur_stage = &module->shared; + source_index = index; + module->source_map.set_name(source_index, name); tokenizer.begin(name, source); while(RefPtr statement = parse_global_declaration()) cur_stage->content.body.push_back(statement); diff --git a/source/glsl/parser.h b/source/glsl/parser.h index b6c8369d..58b14b50 100644 --- a/source/glsl/parser.h +++ b/source/glsl/parser.h @@ -16,7 +16,6 @@ class Parser { private: std::string source; - std::string source_name; unsigned source_index; Tokenizer tokenizer; Preprocessor preprocessor; @@ -33,7 +32,7 @@ public: Module &parse(IO::Base &, const std::string &, unsigned = 0); private: - void parse_source(const std::string &); + void parse_source(const std::string &, unsigned); void set_required_version(const Version &); void stage_change(Stage::Type); diff --git a/source/glsl/sourcemap.cpp b/source/glsl/sourcemap.cpp new file mode 100644 index 00000000..e74f1b67 --- /dev/null +++ b/source/glsl/sourcemap.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include "sourcemap.h" + +using namespace std; + +namespace Msp { +namespace GL { +namespace SL { + +SourceMap::SourceMap(): + base_index(0) +{ } + +void SourceMap::set_name(unsigned i, const std::string &n) +{ + if(source_names.empty()) + base_index = i; + else if(i lines = split(errors, '\n'); + string translated; + for(vector::const_iterator i=lines.begin(); i!=lines.end(); ++i) + { + RegMatch m = r_message.match(*i); + if(m) + { + unsigned index = 0; + unsigned line = 0; + if(m[2]) + { + index = lexical_cast(m[2].str); + line = lexical_cast(m[3].str); + } + else if(m[4]) + { + index = lexical_cast(m[4].str); + line = lexical_cast(m[5].str); + } + const char *src = ""; + if(index +#include + +namespace Msp { +namespace GL { +namespace SL { + +class SourceMap +{ +private: + unsigned base_index; + std::vector source_names; + +public: + SourceMap(); + + void set_name(unsigned, const std::string &); + unsigned get_count() const { return base_index+source_names.size(); } + void merge_from(const SourceMap &); + std::string translate_errors(const std::string &) const; +}; + +} // namespace SL +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index ac67a3fb..77647124 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -7,6 +7,7 @@ #include #include #include "extension.h" +#include "sourcemap.h" #include "uniform.h" #pragma push_macro("interface") @@ -386,6 +387,7 @@ struct Stage struct Module { + SourceMap source_map; Stage shared; std::list stages;