Skip to content

Commit

Permalink
[Impeller] Fix asset names used for the generated entrypoint name can…
Browse files Browse the repository at this point in the history
… contain invalid identifiers for the target language (#38202)

* [impeller] rename entrypoints that are not valid identifiers

* ++
  • Loading branch information
jonahwilliams authored Dec 12, 2022
1 parent 3ca497e commit 922546c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
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

0 comments on commit 922546c

Please sign in to comment.