]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/sourcemap.cpp
Support specialization constants in the GLSL compiler
[libs/gl.git] / source / glsl / sourcemap.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/regex.h>
3 #include <msp/strings/utils.h>
4 #include "sourcemap.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10 namespace SL {
11
12 SourceMap::SourceMap():
13         base_index(0)
14 { }
15
16 void SourceMap::set_name(unsigned i, const std::string &n)
17 {
18         if(source_names.empty())
19                 base_index = i;
20         else if(i<base_index)
21         {
22                 source_names.insert(source_names.begin(), base_index-i, string());
23                 base_index = i;
24         }
25         i -= base_index;
26
27         if(source_names.size()<=i)
28                 source_names.resize(i+1);
29         source_names[i] = n;
30 }
31
32 const string &SourceMap::get_name(unsigned i) const
33 {
34         i -= base_index;
35         if(i<source_names.size())
36                 return source_names[i];
37
38         static string empty;
39         return empty;
40 }
41
42 void SourceMap::merge_from(const SourceMap &other)
43 {
44         if(other.base_index<base_index)
45         {
46                 source_names.insert(source_names.begin(), base_index-other.base_index, string());
47                 base_index = other.base_index;
48         }
49         if(get_count()<other.get_count())
50                 source_names.resize(other.get_count()-base_index);
51
52         for(unsigned i=0; i<other.source_names.size(); ++i)
53                 if(!other.source_names[i].empty())
54                         source_names[i+other.base_index-base_index] = other.source_names[i];
55 }
56
57 string SourceMap::translate_errors(const string &errors) const
58 {
59         static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
60         vector<string> lines = split(errors, '\n');
61         string translated;
62         for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
63         {
64                 RegMatch m = r_message.match(*i);
65                 if(m)
66                 {
67                         unsigned index = 0;
68                         unsigned line = 0;
69                         if(m[2])
70                         {
71                                 index = lexical_cast<unsigned>(m[2].str);
72                                 line = lexical_cast<unsigned>(m[3].str);
73                         }
74                         else if(m[4])
75                         {
76                                 index = lexical_cast<unsigned>(m[4].str);
77                                 line = lexical_cast<unsigned>(m[5].str);
78                         }
79                         const char *src = "<unknown>";
80                         if(index<source_names.size() && !source_names[index].empty())
81                                 src = source_names[index].c_str();
82                         translated += format("%s:%d: %s", src, line, m[6].str);
83                 }
84                 else
85                         translated += *i;
86                 translated += '\n';
87         }
88
89         return translated;
90 }
91
92 } // namespace SL
93 } // namespace GL
94 } // namespace Msp