From fd432f85801e0553fa8a30823f3add8dcace3764 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Sat, 18 Mar 2017 10:52:41 +0100 Subject: [PATCH] Ensure struct members have valid identifiers. --- spirv_cross.cpp | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/spirv_cross.cpp b/spirv_cross.cpp index 4d0b8337..8119c344 100644 --- a/spirv_cross.cpp +++ b/spirv_cross.cpp @@ -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( 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]; + + // _ 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 &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( 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]; - - // _ 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