yum-mirror/glslang

git clone https://git.yummers.dev/yum-mirror/glslang

Diego NovilloFix erroneous parse errors in variable declarations using existing struct names (#4005)b4e66d7b

main
6.5 KiB129 linesraw
1// Copyright (C) 2025 NVIDIA Corporation
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#include <gtest/gtest.h>
22
23#include "TestFixture.h"
24
25namespace glslangtest {
26namespace {
27
28using StructNameTest = GlslangTest<::testing::Test>;
29
30// Test the original bug report case from issue #3931.
31TEST_F(StructNameTest, StructNameAsStructMember)
32{
33    const std::string inputFname = GlobalTestSettings.testRoot + "/struct_name_as_struct_member.frag";
34    std::string input;
35    tryLoadFile(inputFname, "input", &input);
36
37    EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
38    GlslangResult result = compileAndLink("struct_name_as_struct_member.frag", input, "", controls,
39                                          glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
40
41    // Should NOT have the original syntax error that was reported in the GitHub issue.
42    EXPECT_EQ(result.linkingError.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
43
44    // Should compile successfully.
45    EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
46}
47
48// Test struct parameter after non-struct parameter (minimal regression test).
49TEST_F(StructNameTest, StructParameterAfterNonStruct)
50{
51    const std::string inputFname = GlobalTestSettings.testRoot + "/struct_parameter_after_non_struct.frag";
52    std::string input;
53    tryLoadFile(inputFname, "input", &input);
54
55    EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
56    GlslangResult result = compileAndLink("struct_parameter_after_non_struct.frag", input, "", controls,
57                                          glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
58
59    // Should NOT have syntax errors related to struct types in function parameters.
60    EXPECT_EQ(result.linkingError.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
61    EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
62
63    // Should compile successfully.
64    EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
65}
66
67// Test struct names as member names in declarator lists (original issue #3931) - More comprehensive patterns.
68TEST_F(StructNameTest, StructMemberDeclaratorLists)
69{
70    const std::string inputFname = GlobalTestSettings.testRoot + "/struct_member_declarator_lists.frag";
71    std::string input;
72    tryLoadFile(inputFname, "input", &input);
73
74    EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
75    GlslangResult result = compileAndLink("struct_member_declarator_lists.frag", input, "", controls,
76                                          glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
77
78    // Should NOT have the original syntax error from issue #3931.
79    EXPECT_EQ(result.linkingError.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
80    EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
81
82    // Should compile successfully.
83    EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
84}
85
86// Test function parameters with struct types (regression test for issue #4001).
87TEST_F(StructNameTest, StructFunctionParameters)
88{
89    const std::string inputFname = GlobalTestSettings.testRoot + "/struct_function_parameters.frag";
90    std::string input;
91    tryLoadFile(inputFname, "input", &input);
92
93    EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
94    GlslangResult result = compileAndLink("struct_function_parameters.frag", input, "", controls,
95                                          glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
96
97    // Should NOT have syntax errors related to struct types in function parameters.
98    EXPECT_EQ(result.linkingError.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
99    EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
100
101    // Should compile successfully.
102    EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
103}
104
105// Test struct construction patterns - comprehensive patterns including struct constructors.
106TEST_F(StructNameTest, StructConstructionPatterns)
107{
108    const std::string inputFname = GlobalTestSettings.testRoot + "/struct_construction_patterns.frag";
109    std::string input;
110    tryLoadFile(inputFname, "input", &input);
111
112    EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
113    GlslangResult result = compileAndLink("struct_construction_patterns.frag", input, "", controls,
114                                          glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
115
116    // Should NOT have syntax errors in struct construction patterns.
117    EXPECT_EQ(result.linkingError.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
118    EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
119
120    // Should NOT have the original syntax error from issue #3931.
121    EXPECT_EQ(result.linkingError.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
122    EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
123
124    // Should compile successfully.
125    EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
126}
127
128} // anonymous namespace
129} // namespace glslangtest