Add reader::spv::SuggestSanitizedName

Bug: tint:3
Change-Id: Ie53bbb7a0b2523a2098b485b6a5bb3d5fa4e52f9
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17422
Reviewed-by: dan sinclair <dsinclair@google.com>
This commit is contained in:
David Neto 2020-03-23 16:45:01 +00:00 committed by dan sinclair
parent 8af12f7503
commit ca8e6dd48d
3 changed files with 48 additions and 0 deletions

View File

@ -76,6 +76,15 @@ bool Namer::SaveName(uint32_t id, const std::string& name) {
return true; return true;
} }
bool Namer::SuggestSanitizedName(uint32_t id,
const std::string& suggested_name) {
if (HasName(id)) {
return false;
}
return SaveName(id, FindUnusedDerivedName(Sanitize(suggested_name)));
}
} // namespace spirv } // namespace spirv
} // namespace reader } // namespace reader
} // namespace tint } // namespace tint

View File

@ -75,6 +75,13 @@ class Namer {
/// @returns true if the ID did not have a previously registered name. /// @returns true if the ID did not have a previously registered name.
bool SaveName(uint32_t id, const std::string& name); bool SaveName(uint32_t id, const std::string& name);
/// Saves a sanitized name for the given ID, if that ID does not yet
/// have a registered name.
/// @param id the SPIR-V ID
/// @param suggested_name the suggested name
/// @returns true if a name was newly registered for the ID
bool SuggestSanitizedName(uint32_t id, const std::string& suggested_name);
private: private:
FailStream fail_stream_; FailStream fail_stream_;

View File

@ -143,6 +143,38 @@ TEST_F(SpvNamerTest, SaveNameFailsDueToIdReuse) {
EXPECT_FALSE(error().empty()); EXPECT_FALSE(error().empty());
} }
TEST_F(SpvNamerTest, SuggestSanitizedName_TakeSuggestionWhenNoConflict) {
Namer namer(fail_stream_);
EXPECT_TRUE(namer.SuggestSanitizedName(1, "father"));
EXPECT_THAT(namer.GetName(1), Eq("father"));
}
TEST_F(SpvNamerTest,
SuggestSanitizedName_RejectSuggestionWhenConflictOnSameId) {
Namer namer(fail_stream_);
namer.SaveName(1, "lennon");
EXPECT_FALSE(namer.SuggestSanitizedName(1, "mccartney"));
EXPECT_THAT(namer.GetName(1), Eq("lennon"));
}
TEST_F(SpvNamerTest, SuggestSanitizedName_SanitizeSuggestion) {
Namer namer(fail_stream_);
EXPECT_TRUE(namer.SuggestSanitizedName(9, "m:kenzie"));
EXPECT_THAT(namer.GetName(9), Eq("m_kenzie"));
}
TEST_F(SpvNamerTest,
SuggestSanitizedName_GenerateNewNameWhenConflictOnDifferentId) {
Namer namer(fail_stream_);
namer.SaveName(7, "rice");
EXPECT_TRUE(namer.SuggestSanitizedName(9, "rice"));
EXPECT_THAT(namer.GetName(9), Eq("rice_1"));
}
} // namespace } // namespace
} // namespace spirv } // namespace spirv
} // namespace reader } // namespace reader