xref: /aosp_15_r20/external/angle/src/tests/compiler_tests/PruneUnusedFunctions_test.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2015 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 // PruneUnusedFunctions_test.cpp:
7 //   Test for the pruning of unused functions
8 //
9 
10 #include "GLSLANG/ShaderLang.h"
11 #include "angle_gl.h"
12 #include "gtest/gtest.h"
13 #include "tests/test_utils/compiler_test.h"
14 
15 using namespace sh;
16 
17 namespace
18 {
19 
20 class PruneUnusedFunctionsTest : public MatchOutputCodeTest
21 {
22   public:
PruneUnusedFunctionsTest()23     PruneUnusedFunctionsTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_ESSL_OUTPUT) {}
24 
25   protected:
compile(const std::string & shaderString)26     void compile(const std::string &shaderString)
27     {
28         ShCompileOptions compileOptions = {};
29 
30         MatchOutputCodeTest::compile(shaderString, compileOptions);
31     }
32 };
33 
34 // Check that unused function and prototypes are removed iff the options is set
TEST_F(PruneUnusedFunctionsTest,UnusedFunctionAndProto)35 TEST_F(PruneUnusedFunctionsTest, UnusedFunctionAndProto)
36 {
37     const std::string &shaderString =
38         "precision mediump float;\n"
39         "float unused(float a);\n"
40         "void main() {\n"
41         "    gl_FragColor = vec4(1.0);\n"
42         "}\n"
43         "float unused(float a) {\n"
44         "    return a;\n"
45         "}\n";
46     compile(shaderString);
47     EXPECT_TRUE(notFoundInCode("unused("));
48     EXPECT_TRUE(foundInCode("main(", 1));
49 }
50 
51 // Check that unimplemented prototypes are removed iff the options is set
TEST_F(PruneUnusedFunctionsTest,UnimplementedPrototype)52 TEST_F(PruneUnusedFunctionsTest, UnimplementedPrototype)
53 {
54     const std::string &shaderString =
55         "precision mediump float;\n"
56         "float unused(float a);\n"
57         "void main() {\n"
58         "    gl_FragColor = vec4(1.0);\n"
59         "}\n";
60     compile(shaderString);
61     EXPECT_TRUE(notFoundInCode("unused("));
62     EXPECT_TRUE(foundInCode("main(", 1));
63 }
64 
65 // Check that used functions are not pruned (duh)
TEST_F(PruneUnusedFunctionsTest,UsedFunction)66 TEST_F(PruneUnusedFunctionsTest, UsedFunction)
67 {
68     const std::string &shaderString =
69         "precision mediump float;\n"
70         "float used(float a);\n"
71         "void main() {\n"
72         "    gl_FragColor = vec4(used(1.0));\n"
73         "}\n"
74         "float used(float a) {\n"
75         "    return a;\n"
76         "}\n";
77     compile(shaderString);
78     EXPECT_TRUE(foundInCode("used(", 3));
79     EXPECT_TRUE(foundInCode("main(", 1));
80 }
81 
82 }  // namespace
83