Ensure struct members have valid identifiers.

This commit is contained in:
Hans-Kristian Arntzen 2017-03-18 10:52:41 +01:00
parent ecc6ef6937
commit fd432f8580

View File

@ -27,6 +27,26 @@ using namespace spirv_cross;
#define log(...) fprintf(stderr, __VA_ARGS__)
static string ensure_valid_identifier(const string &name)
{
// Functions in glslangValidator are mangled with name(<mangled> stuff.
// Normally, we would never see '(' in any legal identifiers, so just strip them out.
auto str = name.substr(0, name.find('('));
for (uint32_t i = 0; i < str.size(); i++)
{
auto &c = str[i];
// _<num> variables are reserved by the internal implementation,
// otherwise, make sure the name is a valid identifier.
if (i == 0 || (str[0] == '_' && i == 1))
c = isalpha(c) ? c : '_';
else
c = isalnum(c) ? c : '_';
}
return str;
}
Instruction::Instruction(const vector<uint32_t> &spirv, uint32_t &index)
{
op = spirv[index] & 0xffff;
@ -795,21 +815,7 @@ void Compiler::set_name(uint32_t id, const std::string &name)
if (name[0] == '_' && name.size() >= 2 && isdigit(name[1]))
return;
// Functions in glslangValidator are mangled with name(<mangled> stuff.
// Normally, we would never see '(' in any legal identifiers, so just strip them out.
str = name.substr(0, name.find('('));
for (uint32_t i = 0; i < str.size(); i++)
{
auto &c = str[i];
// _<num> variables are reserved by the internal implementation,
// otherwise, make sure the name is a valid identifier.
if (i == 0 || (str[0] == '_' && i == 1))
c = isalpha(c) ? c : '_';
else
c = isalnum(c) ? c : '_';
}
str = ensure_valid_identifier(name);
}
const SPIRType &Compiler::get_type(uint32_t id) const
@ -863,7 +869,17 @@ void Compiler::set_member_decoration(uint32_t id, uint32_t index, Decoration dec
void Compiler::set_member_name(uint32_t id, uint32_t index, const std::string &name)
{
meta.at(id).members.resize(max(meta[id].members.size(), size_t(index) + 1));
meta.at(id).members[index].alias = name;
auto &str = meta.at(id).members[index].alias;
str.clear();
if (name.empty())
return;
// Reserved for unnamed members.
if (name[0] == '_' && name.size() >= 2 && isdigit(name[1]))
return;
str = ensure_valid_identifier(name);
}
const std::string &Compiler::get_member_name(uint32_t id, uint32_t index) const