• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // PruneEmptyCases_test.cpp:
7 //   Tests for pruning empty cases and switch statements. This ensures that the translator doesn't
8 //   produce switch statements where the last case statement is not followed by anything.
9 //
10 
11 #include "GLSLANG/ShaderLang.h"
12 #include "angle_gl.h"
13 #include "gtest/gtest.h"
14 #include "tests/test_utils/compiler_test.h"
15 
16 using namespace sh;
17 
18 namespace
19 {
20 
21 class PruneEmptyCasesTest : public MatchOutputCodeTest
22 {
23   public:
PruneEmptyCasesTest()24     PruneEmptyCasesTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, 0, SH_GLSL_COMPATIBILITY_OUTPUT)
25     {}
26 };
27 
28 // Test that a switch statement that only contains no-ops is pruned entirely.
TEST_F(PruneEmptyCasesTest,SwitchStatementWithOnlyNoOps)29 TEST_F(PruneEmptyCasesTest, SwitchStatementWithOnlyNoOps)
30 {
31     const std::string shaderString =
32         R"(#version 300 es
33 
34         uniform int ui;
35 
36         void main(void)
37         {
38             int i = ui;
39             switch (i)
40             {
41                 case 0:
42                 case 1:
43                     { {} }
44                     int j;
45                     1;
46             }
47         })";
48     compile(shaderString);
49     ASSERT_TRUE(notFoundInCode("switch"));
50     ASSERT_TRUE(notFoundInCode("case"));
51 }
52 
53 // Test that a init statement that has a side effect is preserved even if the switch is pruned.
TEST_F(PruneEmptyCasesTest,SwitchStatementWithOnlyNoOpsAndInitWithSideEffect)54 TEST_F(PruneEmptyCasesTest, SwitchStatementWithOnlyNoOpsAndInitWithSideEffect)
55 {
56     const std::string shaderString =
57         R"(#version 300 es
58 
59         precision mediump float;
60         out vec4 my_FragColor;
61 
62         uniform int uni_i;
63 
64         void main(void)
65         {
66             int i = uni_i;
67             switch (++i)
68             {
69                 case 0:
70                 case 1:
71                     { {} }
72                     int j;
73                     1;
74             }
75             my_FragColor = vec4(i);
76         })";
77     compile(shaderString);
78     ASSERT_TRUE(notFoundInCode("switch"));
79     ASSERT_TRUE(notFoundInCode("case"));
80     ASSERT_TRUE(foundInCode("++_ui"));
81 }
82 
83 // Test a switch statement where the last case only contains no-ops.
TEST_F(PruneEmptyCasesTest,SwitchStatementLastCaseOnlyNoOps)84 TEST_F(PruneEmptyCasesTest, SwitchStatementLastCaseOnlyNoOps)
85 {
86     const std::string shaderString =
87         R"(#version 300 es
88 
89         precision mediump float;
90         out vec4 my_FragColor;
91 
92         uniform int ui;
93 
94         void main(void)
95         {
96             int i = ui;
97             switch (i)
98             {
99                 case 0:
100                     my_FragColor = vec4(0);
101                     break;
102                 case 1:
103                 case 2:
104                     { {} }
105                     int j;
106                     1;
107             }
108         })";
109     compile(shaderString);
110     ASSERT_TRUE(foundInCode("switch"));
111     ASSERT_TRUE(foundInCode("case", 1));
112 }
113 
114 }  // namespace
115