spirv-reader: add namer::IsRegistered

Change-Id: I014132f9a655c7f52ebce4b141f6ea5235e828f1
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49140
Auto-Submit: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
David Neto 2021-04-26 19:25:06 +00:00 committed by Commit Bot service account
parent 439ef278b7
commit 9ef328f530
3 changed files with 28 additions and 5 deletions

View File

@ -105,17 +105,16 @@ std::string Namer::GetMemberName(uint32_t struct_id,
std::string Namer::FindUnusedDerivedName(const std::string& base_name) const {
// Ensure uniqueness among names.
std::string derived_name;
for (int i = 0;; i++) {
int i = 0;
do {
std::stringstream new_name_stream;
new_name_stream << base_name;
if (i > 0) {
new_name_stream << "_" << i;
}
i++;
derived_name = new_name_stream.str();
if (name_to_id_.count(derived_name) == 0) {
break;
}
}
} while (IsRegistered(derived_name));
return derived_name;
}

View File

@ -57,6 +57,12 @@ class Namer {
return id_to_name_.find(id) != id_to_name_.end();
}
/// @param name a string
/// @returns true if the string has been registered as a name.
bool IsRegistered(const std::string& name) const {
return name_to_id_.find(name) != name_to_id_.end();
}
/// @param id the SPIR-V ID
/// @returns the name for the ID. It must have been registered.
const std::string& GetName(uint32_t id) const {

View File

@ -100,6 +100,24 @@ TEST_F(SpvNamerTest, FindUnusedDerivedName_HasMultipleConflicts) {
EXPECT_THAT(namer.FindUnusedDerivedName("rigby"), Eq("rigby_2"));
}
TEST_F(SpvNamerTest, IsRegistered_NoRecordedName) {
Namer namer(fail_stream_);
EXPECT_FALSE(namer.IsRegistered("abbey"));
}
TEST_F(SpvNamerTest, IsRegistered_RegisteredById) {
Namer namer(fail_stream_);
namer.SaveName(1, "abbey");
EXPECT_TRUE(namer.IsRegistered("abbey"));
}
TEST_F(SpvNamerTest, IsRegistered_RegisteredByDerivation) {
Namer namer(fail_stream_);
const auto got = namer.MakeDerivedName("abbey");
EXPECT_TRUE(namer.IsRegistered("abbey"));
EXPECT_EQ(got, "abbey");
}
TEST_F(SpvNamerTest, MakeDerivedName_NoRecordedName) {
Namer namer(fail_stream_);
EXPECT_THAT(namer.MakeDerivedName("eleanor"), Eq("eleanor"));