[msl-writer] Handle remapping remap collisions.

If the remapper give an ident a new name which collides with a future
ident name we need to rename the future name to not collide.

Bug: tint:7
Change-Id: I162a3071f75ed714cee730b78aebefa8c75c77de
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24521
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
This commit is contained in:
dan sinclair 2020-07-08 17:17:35 +00:00
parent 28ba9a369e
commit 928f59ca46
3 changed files with 26 additions and 2 deletions

View File

@ -267,7 +267,7 @@ Namer::Namer() = default;
Namer::~Namer() = default; Namer::~Namer() = default;
std::string Namer::NameFor(const std::string& name) { std::string Namer::NameFor(const std::string& name) {
// If it's in the name make we can just return it. There are no shadow names // If it's in the name map we can just return it. There are no shadow names
// in WGSL so this has to be unique in the WGSL names, and we've already // in WGSL so this has to be unique in the WGSL names, and we've already
// checked the name collisions with MSL. // checked the name collisions with MSL.
auto it = name_map_.find(name); auto it = name_map_.find(name);
@ -288,6 +288,19 @@ std::string Namer::NameFor(const std::string& name) {
} }
i++; i++;
} }
remapped_names_.insert(ret_name);
} else {
uint32_t i = 0;
// Make sure the ident name wasn't assigned by a remapping.
while (true) {
auto remap_it = remapped_names_.find(ret_name);
if (remap_it == remapped_names_.end()) {
break;
}
ret_name = name + "_" + std::to_string(i);
i++;
}
remapped_names_.insert(ret_name);
} }
name_map_[name] = ret_name; name_map_[name] = ret_name;

View File

@ -17,6 +17,7 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <unordered_set>
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -37,6 +38,8 @@ class Namer {
private: private:
/// Map of original name to new name. The two names may be the same. /// Map of original name to new name. The two names may be the same.
std::unordered_map<std::string, std::string> name_map_; std::unordered_map<std::string, std::string> name_map_;
// The list of names taken by the remapper
std::unordered_set<std::string> remapped_names_;
}; };
} // namespace msl } // namespace msl

View File

@ -28,13 +28,21 @@ TEST_F(MslNamerTest, ReturnsName) {
EXPECT_EQ("my_name", n.NameFor("my_name")); EXPECT_EQ("my_name", n.NameFor("my_name"));
} }
TEST_F(MslNamerTest, HandlesConflictWithRenamedReservedWord) { TEST_F(MslNamerTest, HandlesConflictWithRenamedReservedWordAfterIdentSeen) {
Namer n; Namer n;
EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0")); EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0"));
EXPECT_EQ("float_tint_1", n.NameFor("float")); EXPECT_EQ("float_tint_1", n.NameFor("float"));
EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0")); EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0"));
} }
TEST_F(MslNamerTest, HandlesConflictWithRenamedReservedWordBeforeIdentSeen) {
Namer n;
EXPECT_EQ("float_tint_0", n.NameFor("float"));
EXPECT_EQ("float_tint_0_0", n.NameFor("float_tint_0"));
EXPECT_EQ("float_tint_0_0_0", n.NameFor("float_tint_0_0"));
EXPECT_EQ("float_tint_0_0", n.NameFor("float_tint_0"));
}
using MslReservedNameTest = testing::TestWithParam<std::string>; using MslReservedNameTest = testing::TestWithParam<std::string>;
TEST_P(MslReservedNameTest, Emit) { TEST_P(MslReservedNameTest, Emit) {
auto name = GetParam(); auto name = GetParam();