[Impeller] Fix asset names used for the generated entrypoint name can contain invalid identifiers for the target language (flutter/engine#38202)

* [impeller] rename entrypoints that are not valid identifiers

* ++
This commit is contained in:
Jonah Williams
2022-12-11 18:01:23 -08:00
committed by GitHub
parent feab811ab9
commit a2cdd1948e
4 changed files with 34 additions and 1 deletions

View File

@@ -69,6 +69,17 @@ TEST(SwitchesTest, EntryPointCanBeSetForHLSL) {
ASSERT_EQ(switches.entry_point, "CustomEntryPoint");
}
TEST(SwitchesTEst, ConvertToEntrypointName) {
ASSERT_EQ(ConvertToEntrypointName("mandelbrot_unrolled"),
"mandelbrot_unrolled");
ASSERT_EQ(ConvertToEntrypointName("mandelbrot-unrolled"),
"mandelbrotunrolled");
ASSERT_EQ(ConvertToEntrypointName("7_"), "i_7_");
ASSERT_EQ(ConvertToEntrypointName("415"), "i_415");
ASSERT_EQ(ConvertToEntrypointName("#$%"), "i_");
ASSERT_EQ(ConvertToEntrypointName(""), "");
}
} // namespace testing
} // namespace compiler
} // namespace impeller

View File

@@ -96,7 +96,7 @@ std::string EntryPointFunctionNameFromSourceName(
std::stringstream stream;
std::filesystem::path file_path(file_name);
stream << Utf8FromPath(file_path.stem()) << "_";
stream << ConvertToEntrypointName(Utf8FromPath(file_path.stem())) << "_";
switch (type) {
case SourceType::kUnknown:
stream << "unknown";

View File

@@ -43,5 +43,23 @@ std::string ConvertToCamelCase(std::string_view string) {
return stream.str();
}
std::string ConvertToEntrypointName(std::string_view string) {
if (string.empty()) {
return "";
}
std::stringstream stream;
// Append a prefix if the first character is not a letter.
if (!std::isalpha(string.data()[0])) {
stream << "i_";
}
for (size_t i = 0, count = string.length(); i < count; i++) {
auto ch = string.data()[i];
if (std::isalnum(ch) || ch == '_') {
stream << ch;
}
}
return stream.str();
}
} // namespace compiler
} // namespace impeller

View File

@@ -25,5 +25,9 @@ std::string InferShaderNameFromPath(std::string_view path);
std::string ConvertToCamelCase(std::string_view string);
/// @brief Ensure that the entrypoint name is a valid identifier in the target
/// language.
std::string ConvertToEntrypointName(std::string_view string);
} // namespace compiler
} // namespace impeller