• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/transform/fold_trivial_single_use_lets.h"
16 
17 #include "src/transform/test_helper.h"
18 
19 namespace tint {
20 namespace transform {
21 namespace {
22 
23 using FoldTrivialSingleUseLetsTest = TransformTest;
24 
TEST_F(FoldTrivialSingleUseLetsTest,EmptyModule)25 TEST_F(FoldTrivialSingleUseLetsTest, EmptyModule) {
26   auto* src = "";
27   auto* expect = "";
28 
29   auto got = Run<FoldTrivialSingleUseLets>(src);
30 
31   EXPECT_EQ(expect, str(got));
32 }
33 
TEST_F(FoldTrivialSingleUseLetsTest,Single)34 TEST_F(FoldTrivialSingleUseLetsTest, Single) {
35   auto* src = R"(
36 fn f() {
37   let x = 1;
38   _ = x;
39 }
40 )";
41 
42   auto* expect = R"(
43 fn f() {
44   _ = 1;
45 }
46 )";
47 
48   auto got = Run<FoldTrivialSingleUseLets>(src);
49 
50   EXPECT_EQ(expect, str(got));
51 }
52 
TEST_F(FoldTrivialSingleUseLetsTest,Multiple)53 TEST_F(FoldTrivialSingleUseLetsTest, Multiple) {
54   auto* src = R"(
55 fn f() {
56   let x = 1;
57   let y = 2;
58   let z = 3;
59   _ = x + y + z;
60 }
61 )";
62 
63   auto* expect = R"(
64 fn f() {
65   _ = ((1 + 2) + 3);
66 }
67 )";
68 
69   auto got = Run<FoldTrivialSingleUseLets>(src);
70 
71   EXPECT_EQ(expect, str(got));
72 }
73 
TEST_F(FoldTrivialSingleUseLetsTest,Chained)74 TEST_F(FoldTrivialSingleUseLetsTest, Chained) {
75   auto* src = R"(
76 fn f() {
77   let x = 1;
78   let y = x;
79   let z = y;
80   _ = z;
81 }
82 )";
83 
84   auto* expect = R"(
85 fn f() {
86   _ = 1;
87 }
88 )";
89 
90   auto got = Run<FoldTrivialSingleUseLets>(src);
91 
92   EXPECT_EQ(expect, str(got));
93 }
94 
TEST_F(FoldTrivialSingleUseLetsTest,NoFold_NonTrivialLet)95 TEST_F(FoldTrivialSingleUseLetsTest, NoFold_NonTrivialLet) {
96   auto* src = R"(
97 fn function_with_posssible_side_effect() -> i32 {
98   return 1;
99 }
100 
101 fn f() {
102   let x = 1;
103   let y = function_with_posssible_side_effect();
104   _ = (x + y);
105 }
106 )";
107 
108   auto* expect = src;
109 
110   auto got = Run<FoldTrivialSingleUseLets>(src);
111 
112   EXPECT_EQ(expect, str(got));
113 }
114 
TEST_F(FoldTrivialSingleUseLetsTest,NoFold_UseInSubBlock)115 TEST_F(FoldTrivialSingleUseLetsTest, NoFold_UseInSubBlock) {
116   auto* src = R"(
117 fn f() {
118   let x = 1;
119   {
120     _ = x;
121   }
122 }
123 )";
124 
125   auto* expect = src;
126 
127   auto got = Run<FoldTrivialSingleUseLets>(src);
128 
129   EXPECT_EQ(expect, str(got));
130 }
131 
TEST_F(FoldTrivialSingleUseLetsTest,NoFold_MultipleUses)132 TEST_F(FoldTrivialSingleUseLetsTest, NoFold_MultipleUses) {
133   auto* src = R"(
134 fn f() {
135   let x = 1;
136   _ = (x + x);
137 }
138 )";
139 
140   auto* expect = src;
141 
142   auto got = Run<FoldTrivialSingleUseLets>(src);
143 
144   EXPECT_EQ(expect, str(got));
145 }
146 
TEST_F(FoldTrivialSingleUseLetsTest,NoFold_Shadowing)147 TEST_F(FoldTrivialSingleUseLetsTest, NoFold_Shadowing) {
148   auto* src = R"(
149 fn f() {
150   var y = 1;
151   let x = y;
152   {
153     let y = false;
154     _ = (x + x);
155   }
156 }
157 )";
158 
159   auto* expect = src;
160 
161   auto got = Run<FoldTrivialSingleUseLets>(src);
162 
163   EXPECT_EQ(expect, str(got));
164 }
165 
166 }  // namespace
167 }  // namespace transform
168 }  // namespace tint
169