Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix consume_line_continuations() skipping characters

Andrew Priceaafac7d75c74
diff --git a/servers/rendering/shader_preprocessor.cpp b/servers/rendering/shader_preprocessor.cpp
index 2582773e834..dea79507d4e 100644
--- a/servers/rendering/shader_preprocessor.cpp
+++ b/servers/rendering/shader_preprocessor.cpp
@@ -81,24 +81,20 @@ char32_t ShaderPreprocessor::Tokenizer::peek() {
return 0;
}
-int ShaderPreprocessor::Tokenizer::consume_line_continuations(int p_offset) {
+int ShaderPreprocessor::Tokenizer::consume_line_continuations() {
int skips = 0;
- for (int i = index + p_offset; i < size; i++) {
- char32_t c = code[i];
- if (c == '\\') {
- if (i + 1 < size && code[i + 1] == '\n') {
- // This line ends with "\" and "\n" continuation.
- add_generated(Token('\n', line));
- line++;
- skips++;
-
- i = i + 2;
- index = i;
- } else {
- break;
- }
- } else if (!is_whitespace(c)) {
+ for (; index < size; index++) {
+ char32_t c = code[index];
+ if (c != '\\') {
+ break;
+ }
+ if (index + 1 < size && code[index + 1] == '\n') {
+ add_generated(Token('\n', line));
+ line++;
+ skips++;
+ index++;
+ } else {
break;
}
}
@@ -109,11 +105,11 @@ LocalVector<ShaderPreprocessor::Token> ShaderPreprocessor::Tokenizer::advance(ch
LocalVector<ShaderPreprocessor::Token> tokens;
while (index < size) {
- char32_t c = code[index++];
- if (c == '\\' && consume_line_continuations(-1) > 0) {
+ char32_t c = code[index];
+ if (c == '\\' && consume_line_continuations() > 0) {
continue;
}
-
+ index++;
if (c == '\n') {
add_generated(ShaderPreprocessor::Token('\n', line));
line++;
@@ -148,7 +144,7 @@ String ShaderPreprocessor::Tokenizer::get_identifier(bool *r_is_cursor, bool p_s
while (true) {
char32_t c = peek();
- if (c == '\\' && consume_line_continuations(0) > 0) {
+ if (c == '\\' && consume_line_continuations() > 0) {
continue;
}
diff --git a/servers/rendering/shader_preprocessor.h b/servers/rendering/shader_preprocessor.h
index 39a9e558578..851d4fc9a41 100644
--- a/servers/rendering/shader_preprocessor.h
+++ b/servers/rendering/shader_preprocessor.h
@@ -91,7 +91,7 @@ private:
int get_line() const;
int get_index() const;
char32_t peek();
- int consume_line_continuations(int p_offset);
+ int consume_line_continuations();
void get_and_clear_generated(LocalVector<char32_t> *r_out);
void backtrack(char32_t p_what);
diff --git a/tests/servers/rendering/test_shader_preprocessor.cpp b/tests/servers/rendering/test_shader_preprocessor.cpp
index 16286fc3fd9..90617edec64 100644
--- a/tests/servers/rendering/test_shader_preprocessor.cpp
+++ b/tests/servers/rendering/test_shader_preprocessor.cpp
@@ -293,6 +293,54 @@ TEST_CASE("[ShaderPreprocessor] Concatenation sorting network") {
CHECK_SHADER_EQ(result, expected);
}
+TEST_CASE("[ShaderPreprocessor] Line continuations") {
+ String code(
+ "#define SAMPLE(N) vec4 tex##N = texture( \\\n"
+ "material##N \\\n"
+ ", \\\n"
+ "UV \\\n"
+ "\\\n"
+ " )\n"
+ "\n"
+ "#define FUN_FUN(n)\\\n"
+ "void fun1() { return;\\\n"
+ "}\\\n"
+ "void fun2() { return; }\n"
+ "FUN_FUN(1.0)\n"
+ "\n"
+ "#define SCALE\\\n"
+ " 0.5\n"
+ "\n"
+ "#define HALF\\\n"
+ " (0.5)\n"
+ "\n"
+ "#define DECL float\\\n"
+ " v = 0.5;\n"
+ "\n"
+ "void fragment() {\n"
+ " DECL\n"
+ " SAMPLE(0);\n"
+ " ALBEDO = vec3(SCALE);\n"
+ " ALBEDO = vec3(HALF);\n"
+ "}\n");
+ String expected(
+ "void fun1() { return;}"
+ "void fun2() { return; }\n"
+ "\n"
+ "void fragment() {\n"
+ " float v = 0.5;\n"
+ " vec4 tex0 = texture( material0 , UV );\n"
+ " ALBEDO = vec3(0.5);\n"
+ " ALBEDO = vec3((0.5));\n"
+ "}\n");
+ String result;
+
+ ShaderPreprocessor preprocessor;
+ CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
+
+ CHECK_SHADER_EQ(result, expected);
+}
+
TEST_CASE("[ShaderPreprocessor] Undefined behavior") {
// None of these are valid concatenation, nor valid shader code.
// Don't care about results, just make sure there's no crash.