• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 0, SH_ESSL_OUTPUT) {}
24 
25   protected:
compile(const std::string & shaderString)26     void compile(const std::string &shaderString)
27     {
28         int compilationFlags = SH_VARIABLES;
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);
46     EXPECT_TRUE(notFoundInCode("unused("));
47     EXPECT_TRUE(foundInCode("main(", 1));
48 }
49 
50 // Check that unimplemented prototypes are removed iff the options is set
TEST_F(PruneUnusedFunctionsTest,UnimplementedPrototype)51 TEST_F(PruneUnusedFunctionsTest, UnimplementedPrototype)
52 {
53     const std::string &shaderString =
54         "precision mediump float;\n"
55         "float unused(float a);\n"
56         "void main() {\n"
57         "    gl_FragColor = vec4(1.0);\n"
58         "}\n";
59     compile(shaderString);
60     EXPECT_TRUE(notFoundInCode("unused("));
61     EXPECT_TRUE(foundInCode("main(", 1));
62 }
63 
64 // Check that used functions are not pruned (duh)
TEST_F(PruneUnusedFunctionsTest,UsedFunction)65 TEST_F(PruneUnusedFunctionsTest, UsedFunction)
66 {
67     const std::string &shaderString =
68         "precision mediump float;\n"
69         "float used(float a);\n"
70         "void main() {\n"
71         "    gl_FragColor = vec4(used(1.0));\n"
72         "}\n"
73         "float used(float a) {\n"
74         "    return a;\n"
75         "}\n";
76     compile(shaderString);
77     EXPECT_TRUE(foundInCode("used(", 3));
78     EXPECT_TRUE(foundInCode("main(", 1));
79 }
80 
81 }  // namespace
82