StructName.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <gtest/gtest.h>
  21. #include "TestFixture.h"
  22. namespace glslangtest {
  23. namespace {
  24. using StructNameTest = GlslangTest<::testing::Test>;
  25. // Test the original bug report case from issue #3931.
  26. TEST_F(StructNameTest, StructNameAsStructMember)
  27. {
  28. const std::string inputFname = GlobalTestSettings.testRoot + "/struct_name_as_struct_member.frag";
  29. std::string input;
  30. tryLoadFile(inputFname, "input", &input);
  31. EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
  32. GlslangResult result = compileAndLink("struct_name_as_struct_member.frag", input, "", controls,
  33. glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
  34. // Should NOT have the original syntax error that was reported in the GitHub issue.
  35. EXPECT_EQ(result.linkingError.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
  36. // Should compile successfully.
  37. EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
  38. }
  39. // Test struct parameter after non-struct parameter (minimal regression test).
  40. TEST_F(StructNameTest, StructParameterAfterNonStruct)
  41. {
  42. const std::string inputFname = GlobalTestSettings.testRoot + "/struct_parameter_after_non_struct.frag";
  43. std::string input;
  44. tryLoadFile(inputFname, "input", &input);
  45. EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
  46. GlslangResult result = compileAndLink("struct_parameter_after_non_struct.frag", input, "", controls,
  47. glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
  48. // Should NOT have syntax errors related to struct types in function parameters.
  49. EXPECT_EQ(result.linkingError.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
  50. EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
  51. // Should compile successfully.
  52. EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
  53. }
  54. // Test struct names as member names in declarator lists (original issue #3931) - More comprehensive patterns.
  55. TEST_F(StructNameTest, StructMemberDeclaratorLists)
  56. {
  57. const std::string inputFname = GlobalTestSettings.testRoot + "/struct_member_declarator_lists.frag";
  58. std::string input;
  59. tryLoadFile(inputFname, "input", &input);
  60. EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
  61. GlslangResult result = compileAndLink("struct_member_declarator_lists.frag", input, "", controls,
  62. glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
  63. // Should NOT have the original syntax error from issue #3931.
  64. EXPECT_EQ(result.linkingError.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
  65. EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
  66. // Should compile successfully.
  67. EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
  68. }
  69. // Test function parameters with struct types (regression test for issue #4001).
  70. TEST_F(StructNameTest, StructFunctionParameters)
  71. {
  72. const std::string inputFname = GlobalTestSettings.testRoot + "/struct_function_parameters.frag";
  73. std::string input;
  74. tryLoadFile(inputFname, "input", &input);
  75. EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
  76. GlslangResult result = compileAndLink("struct_function_parameters.frag", input, "", controls,
  77. glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
  78. // Should NOT have syntax errors related to struct types in function parameters.
  79. EXPECT_EQ(result.linkingError.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
  80. EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
  81. // Should compile successfully.
  82. EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
  83. }
  84. // Test struct construction patterns - comprehensive patterns including struct constructors.
  85. TEST_F(StructNameTest, StructConstructionPatterns)
  86. {
  87. const std::string inputFname = GlobalTestSettings.testRoot + "/struct_construction_patterns.frag";
  88. std::string input;
  89. tryLoadFile(inputFname, "input", &input);
  90. EShMessages controls = DeriveOptions(Source::GLSL, Semantics::OpenGL, Target::AST);
  91. GlslangResult result = compileAndLink("struct_construction_patterns.frag", input, "", controls,
  92. glslang::EShTargetVulkan_1_0, glslang::EShTargetSpv_1_0);
  93. // Should NOT have syntax errors in struct construction patterns.
  94. EXPECT_EQ(result.linkingError.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
  95. EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected IDENTIFIER"), std::string::npos);
  96. // Should NOT have the original syntax error from issue #3931.
  97. EXPECT_EQ(result.linkingError.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
  98. EXPECT_EQ(result.shaderResults[0].output.find("syntax error, unexpected TYPE_NAME, expecting IDENTIFIER"), std::string::npos);
  99. // Should compile successfully.
  100. EXPECT_EQ(result.shaderResults[0].output.find("compilation errors"), std::string::npos);
  101. }
  102. } // anonymous namespace
  103. } // namespace glslangtest