Skip to content

Commit d23bacd

Browse files
jonahwilliamsloic-sharma
authored andcommitted
[Impeller] Fix asset names used for the generated entrypoint name can contain invalid identifiers for the target language (flutter#38202)
* [impeller] rename entrypoints that are not valid identifiers * ++
1 parent ffcc46f commit d23bacd

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

impeller/compiler/switches_unittests.cc

+11
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ TEST(SwitchesTest, EntryPointCanBeSetForHLSL) {
6969
ASSERT_EQ(switches.entry_point, "CustomEntryPoint");
7070
}
7171

72+
TEST(SwitchesTEst, ConvertToEntrypointName) {
73+
ASSERT_EQ(ConvertToEntrypointName("mandelbrot_unrolled"),
74+
"mandelbrot_unrolled");
75+
ASSERT_EQ(ConvertToEntrypointName("mandelbrot-unrolled"),
76+
"mandelbrotunrolled");
77+
ASSERT_EQ(ConvertToEntrypointName("7_"), "i_7_");
78+
ASSERT_EQ(ConvertToEntrypointName("415"), "i_415");
79+
ASSERT_EQ(ConvertToEntrypointName("#$%"), "i_");
80+
ASSERT_EQ(ConvertToEntrypointName(""), "");
81+
}
82+
7283
} // namespace testing
7384
} // namespace compiler
7485
} // namespace impeller

impeller/compiler/types.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ std::string EntryPointFunctionNameFromSourceName(
9696

9797
std::stringstream stream;
9898
std::filesystem::path file_path(file_name);
99-
stream << Utf8FromPath(file_path.stem()) << "_";
99+
stream << ConvertToEntrypointName(Utf8FromPath(file_path.stem())) << "_";
100100
switch (type) {
101101
case SourceType::kUnknown:
102102
stream << "unknown";

impeller/compiler/utilities.cc

+18
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@ std::string ConvertToCamelCase(std::string_view string) {
4343
return stream.str();
4444
}
4545

46+
std::string ConvertToEntrypointName(std::string_view string) {
47+
if (string.empty()) {
48+
return "";
49+
}
50+
std::stringstream stream;
51+
// Append a prefix if the first character is not a letter.
52+
if (!std::isalpha(string.data()[0])) {
53+
stream << "i_";
54+
}
55+
for (size_t i = 0, count = string.length(); i < count; i++) {
56+
auto ch = string.data()[i];
57+
if (std::isalnum(ch) || ch == '_') {
58+
stream << ch;
59+
}
60+
}
61+
return stream.str();
62+
}
63+
4664
} // namespace compiler
4765
} // namespace impeller

impeller/compiler/utilities.h

+4
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,9 @@ std::string InferShaderNameFromPath(std::string_view path);
2525

2626
std::string ConvertToCamelCase(std::string_view string);
2727

28+
/// @brief Ensure that the entrypoint name is a valid identifier in the target
29+
/// language.
30+
std::string ConvertToEntrypointName(std::string_view string);
31+
2832
} // namespace compiler
2933
} // namespace impeller

0 commit comments

Comments
 (0)