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 function with the SH_PRUNE_UNUSED_FUNCTIONS compile flag
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, 0, SH_ESSL_OUTPUT) {}
24
25 protected:
compile(const std::string & shaderString,bool prune)26 void compile(const std::string &shaderString, bool prune)
27 {
28 int compilationFlags = SH_VARIABLES | (prune ? 0 : SH_DONT_PRUNE_UNUSED_FUNCTIONS);
29 MatchOutputCodeTest::compile(shaderString, compilationFlags);
30 }
31 };
32
33 // Check that unused function and prototypes are removed iff the options is set
TEST_F(PruneUnusedFunctionsTest,UnusedFunctionAndProto)34 TEST_F(PruneUnusedFunctionsTest, UnusedFunctionAndProto)
35 {
36 const std::string &shaderString =
37 "precision mediump float;\n"
38 "float unused(float a);\n"
39 "void main() {\n"
40 " gl_FragColor = vec4(1.0);\n"
41 "}\n"
42 "float unused(float a) {\n"
43 " return a;\n"
44 "}\n";
45 compile(shaderString, true);
46 EXPECT_TRUE(notFoundInCode("unused("));
47 EXPECT_TRUE(foundInCode("main(", 1));
48
49 compile(shaderString, false);
50 EXPECT_TRUE(foundInCode("unused(", 2));
51 EXPECT_TRUE(foundInCode("main(", 1));
52 }
53
54 // Check that unimplemented prototypes are removed iff the options is set
TEST_F(PruneUnusedFunctionsTest,UnimplementedPrototype)55 TEST_F(PruneUnusedFunctionsTest, UnimplementedPrototype)
56 {
57 const std::string &shaderString =
58 "precision mediump float;\n"
59 "float unused(float a);\n"
60 "void main() {\n"
61 " gl_FragColor = vec4(1.0);\n"
62 "}\n";
63 compile(shaderString, true);
64 EXPECT_TRUE(notFoundInCode("unused("));
65 EXPECT_TRUE(foundInCode("main(", 1));
66
67 compile(shaderString, false);
68 EXPECT_TRUE(foundInCode("unused(", 1));
69 EXPECT_TRUE(foundInCode("main(", 1));
70 }
71
72 // Check that used functions are not pruned (duh)
TEST_F(PruneUnusedFunctionsTest,UsedFunction)73 TEST_F(PruneUnusedFunctionsTest, UsedFunction)
74 {
75 const std::string &shaderString =
76 "precision mediump float;\n"
77 "float used(float a);\n"
78 "void main() {\n"
79 " gl_FragColor = vec4(used(1.0));\n"
80 "}\n"
81 "float used(float a) {\n"
82 " return a;\n"
83 "}\n";
84 compile(shaderString, true);
85 EXPECT_TRUE(foundInCode("used(", 3));
86 EXPECT_TRUE(foundInCode("main(", 1));
87
88 compile(shaderString, false);
89 EXPECT_TRUE(foundInCode("used(", 3));
90 EXPECT_TRUE(foundInCode("main(", 1));
91 }
92
93 } // namespace
94