Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

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

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions impeller/compiler/switches_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion impeller/compiler/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
18 changes: 18 additions & 0 deletions impeller/compiler/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions impeller/compiler/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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