[spirv-reader] Add Namer::MakeDerivedName
Bug: tint:3 Change-Id: I4dc3049c577049bb4ad0d1e42ec80f7d82cdb4a4 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23181 Reviewed-by: dan sinclair <dsinclair@google.com>
This commit is contained in:
parent
318ae59f4d
commit
ad2f7ccc55
|
@ -80,6 +80,13 @@ std::string Namer::FindUnusedDerivedName(const std::string& base_name) const {
|
|||
return derived_name;
|
||||
}
|
||||
|
||||
std::string Namer::MakeDerivedName(const std::string& base_name) {
|
||||
auto result = FindUnusedDerivedName(base_name);
|
||||
// Register it.
|
||||
name_to_id_[result] = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Namer::SaveName(uint32_t id, const std::string& name) {
|
||||
if (HasName(id)) {
|
||||
return Fail() << "internal error: ID " << id
|
||||
|
|
|
@ -89,6 +89,13 @@ class Namer {
|
|||
/// @returns a new name
|
||||
std::string FindUnusedDerivedName(const std::string& base_name) const;
|
||||
|
||||
/// Returns a newly registered name based on a given base name.
|
||||
/// In the internal table |name_to_id_|, it is mapped to the invalid
|
||||
/// SPIR-V ID 0. It does not have an entry in |id_to_name_|.
|
||||
/// @param base_name the base name
|
||||
/// @returns a new name
|
||||
std::string MakeDerivedName(const std::string& base_name);
|
||||
|
||||
/// Records a mapping from the given ID to a name. Emits a failure
|
||||
/// if the ID already has a registered name.
|
||||
/// @param id the SPIR-V ID
|
||||
|
|
|
@ -86,6 +86,8 @@ TEST_F(SpvNamerTest, NoNameRecorded) {
|
|||
TEST_F(SpvNamerTest, FindUnusedDerivedName_NoRecordedName) {
|
||||
Namer namer(fail_stream_);
|
||||
EXPECT_THAT(namer.FindUnusedDerivedName("eleanor"), Eq("eleanor"));
|
||||
// Prove that it wasn't registered when first found.
|
||||
EXPECT_THAT(namer.FindUnusedDerivedName("eleanor"), Eq("eleanor"));
|
||||
}
|
||||
|
||||
TEST_F(SpvNamerTest, FindUnusedDerivedName_HasRecordedName) {
|
||||
|
@ -103,6 +105,28 @@ TEST_F(SpvNamerTest, FindUnusedDerivedName_HasMultipleConflicts) {
|
|||
EXPECT_THAT(namer.FindUnusedDerivedName("rigby"), Eq("rigby_2"));
|
||||
}
|
||||
|
||||
TEST_F(SpvNamerTest, MakeDerivedName_NoRecordedName) {
|
||||
Namer namer(fail_stream_);
|
||||
EXPECT_THAT(namer.MakeDerivedName("eleanor"), Eq("eleanor"));
|
||||
// Prove that it was registered when first found.
|
||||
EXPECT_THAT(namer.MakeDerivedName("eleanor"), Eq("eleanor_1"));
|
||||
}
|
||||
|
||||
TEST_F(SpvNamerTest, MakeDerivedName_HasRecordedName) {
|
||||
Namer namer(fail_stream_);
|
||||
namer.SaveName(12, "rigby");
|
||||
EXPECT_THAT(namer.MakeDerivedName("rigby"), Eq("rigby_1"));
|
||||
}
|
||||
|
||||
TEST_F(SpvNamerTest, MakeDerivedName_HasMultipleConflicts) {
|
||||
Namer namer(fail_stream_);
|
||||
namer.SaveName(12, "rigby");
|
||||
namer.SaveName(13, "rigby_1");
|
||||
namer.SaveName(14, "rigby_3");
|
||||
// It picks the first non-conflicting suffix.
|
||||
EXPECT_THAT(namer.MakeDerivedName("rigby"), Eq("rigby_2"));
|
||||
}
|
||||
|
||||
TEST_F(SpvNamerTest, SaveNameOnce) {
|
||||
Namer namer(fail_stream_);
|
||||
|
||||
|
|
Loading…
Reference in New Issue