Debian Codegraph @

llvm-project/clang/unittests/Analysis/FlowSensitive/DataflowAnalysisContextTest.cpp

raw
1 //===- unittests/Analysis/FlowSensitive/DataflowAnalysisContextTest.cpp ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
10 #include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 #include <memory>
14
15 namespace {
16
17 using namespace clang;
18 using namespace dataflow;
19
20 class DataflowAnalysisContextTest : public ::testing::Test {
21 protected:
22 DataflowAnalysisContextTest()
23 : Context(std::make_unique<WatchedLiteralsSolver>()) {}
24
25 DataflowAnalysisContext Context;
26 };
27
28 TEST_F(DataflowAnalysisContextTest,
29 CreateAtomicBoolValueReturnsDistinctValues) {
30 auto &X = Context.createAtomicBoolValue();
31 auto &Y = Context.createAtomicBoolValue();
32 EXPECT_NE(&X, &Y);
33 }
34
35 TEST_F(DataflowAnalysisContextTest,
36 CreateTopBoolValueReturnsDistinctValues) {
37 auto &X = Context.createTopBoolValue();
38 auto &Y = Context.createTopBoolValue();
39 EXPECT_NE(&X, &Y);
40 }
41
42 TEST_F(DataflowAnalysisContextTest, DistinctTopsNotEquivalent) {
43 auto &X = Context.createTopBoolValue();
44 auto &Y = Context.createTopBoolValue();
45 EXPECT_FALSE(Context.equivalentBoolValues(X, Y));
46 }
47
48 TEST_F(DataflowAnalysisContextTest,
49 GetOrCreateConjunctionReturnsSameExprGivenSameArgs) {
50 auto &X = Context.createAtomicBoolValue();
51 auto &XAndX = Context.getOrCreateConjunction(X, X);
52 EXPECT_EQ(&XAndX, &X);
53 }
54
55 TEST_F(DataflowAnalysisContextTest,
56 GetOrCreateConjunctionReturnsSameExprOnSubsequentCalls) {
57 auto &X = Context.createAtomicBoolValue();
58 auto &Y = Context.createAtomicBoolValue();
59 auto &XAndY1 = Context.getOrCreateConjunction(X, Y);
60 auto &XAndY2 = Context.getOrCreateConjunction(X, Y);
61 EXPECT_EQ(&XAndY1, &XAndY2);
62
63 auto &YAndX = Context.getOrCreateConjunction(Y, X);
64 EXPECT_EQ(&XAndY1, &YAndX);
65
66 auto &Z = Context.createAtomicBoolValue();
67 auto &XAndZ = Context.getOrCreateConjunction(X, Z);
68 EXPECT_NE(&XAndY1, &XAndZ);
69 }
70
71 TEST_F(DataflowAnalysisContextTest,
72 GetOrCreateDisjunctionReturnsSameExprGivenSameArgs) {
73 auto &X = Context.createAtomicBoolValue();
74 auto &XOrX = Context.getOrCreateDisjunction(X, X);
75 EXPECT_EQ(&XOrX, &X);
76 }
77
78 TEST_F(DataflowAnalysisContextTest,
79 GetOrCreateDisjunctionReturnsSameExprOnSubsequentCalls) {
80 auto &X = Context.createAtomicBoolValue();
81 auto &Y = Context.createAtomicBoolValue();
82 auto &XOrY1 = Context.getOrCreateDisjunction(X, Y);
83 auto &XOrY2 = Context.getOrCreateDisjunction(X, Y);
84 EXPECT_EQ(&XOrY1, &XOrY2);
85
86 auto &YOrX = Context.getOrCreateDisjunction(Y, X);
87 EXPECT_EQ(&XOrY1, &YOrX);
88
89 auto &Z = Context.createAtomicBoolValue();
90 auto &XOrZ = Context.getOrCreateDisjunction(X, Z);
91 EXPECT_NE(&XOrY1, &XOrZ);
92 }
93
94 TEST_F(DataflowAnalysisContextTest,
95 GetOrCreateNegationReturnsSameExprOnSubsequentCalls) {
96 auto &X = Context.createAtomicBoolValue();
97 auto &NotX1 = Context.getOrCreateNegation(X);
98 auto &NotX2 = Context.getOrCreateNegation(X);
99 EXPECT_EQ(&NotX1, &NotX2);
100
101 auto &Y = Context.createAtomicBoolValue();
102 auto &NotY = Context.getOrCreateNegation(Y);
103 EXPECT_NE(&NotX1, &NotY);
104 }
105
106 TEST_F(DataflowAnalysisContextTest,
107 GetOrCreateImplicationReturnsTrueGivenSameArgs) {
108 auto &X = Context.createAtomicBoolValue();
109 auto &XImpliesX = Context.getOrCreateImplication(X, X);
110 EXPECT_EQ(&XImpliesX, &Context.getBoolLiteralValue(true));
111 }
112
113 TEST_F(DataflowAnalysisContextTest,
114 GetOrCreateImplicationReturnsSameExprOnSubsequentCalls) {
115 auto &X = Context.createAtomicBoolValue();
116 auto &Y = Context.createAtomicBoolValue();
117 auto &XImpliesY1 = Context.getOrCreateImplication(X, Y);
118 auto &XImpliesY2 = Context.getOrCreateImplication(X, Y);
119 EXPECT_EQ(&XImpliesY1, &XImpliesY2);
120
121 auto &YImpliesX = Context.getOrCreateImplication(Y, X);
122 EXPECT_NE(&XImpliesY1, &YImpliesX);
123
124 auto &Z = Context.createAtomicBoolValue();
125 auto &XImpliesZ = Context.getOrCreateImplication(X, Z);
126 EXPECT_NE(&XImpliesY1, &XImpliesZ);
127 }
128
129 TEST_F(DataflowAnalysisContextTest, GetOrCreateIffReturnsTrueGivenSameArgs) {
130 auto &X = Context.createAtomicBoolValue();
131 auto &XIffX = Context.getOrCreateIff(X, X);
132 EXPECT_EQ(&XIffX, &Context.getBoolLiteralValue(true));
133 }
134
135 TEST_F(DataflowAnalysisContextTest,
136 GetOrCreateIffReturnsSameExprOnSubsequentCalls) {
137 auto &X = Context.createAtomicBoolValue();
138 auto &Y = Context.createAtomicBoolValue();
139 auto &XIffY1 = Context.getOrCreateIff(X, Y);
140 auto &XIffY2 = Context.getOrCreateIff(X, Y);
141 EXPECT_EQ(&XIffY1, &XIffY2);
142
143 auto &YIffX = Context.getOrCreateIff(Y, X);
144 EXPECT_EQ(&XIffY1, &YIffX);
145
146 auto &Z = Context.createAtomicBoolValue();
147 auto &XIffZ = Context.getOrCreateIff(X, Z);
148 EXPECT_NE(&XIffY1, &XIffZ);
149 }
150
151 TEST_F(DataflowAnalysisContextTest, EmptyFlowCondition) {
152 auto &FC = Context.makeFlowConditionToken();
153 auto &C = Context.createAtomicBoolValue();
154 EXPECT_FALSE(Context.flowConditionImplies(FC, C));
155 }
156
157 TEST_F(DataflowAnalysisContextTest, AddFlowConditionConstraint) {
158 auto &FC = Context.makeFlowConditionToken();
159 auto &C = Context.createAtomicBoolValue();
160 Context.addFlowConditionConstraint(FC, C);
161 EXPECT_TRUE(Context.flowConditionImplies(FC, C));
162 }
163
164 TEST_F(DataflowAnalysisContextTest, ForkFlowCondition) {
165 auto &FC1 = Context.makeFlowConditionToken();
166 auto &C1 = Context.createAtomicBoolValue();
167 Context.addFlowConditionConstraint(FC1, C1);
168
169 // Forked flow condition inherits the constraints of its parent flow
170 // condition.
171 auto &FC2 = Context.forkFlowCondition(FC1);
172 EXPECT_TRUE(Context.flowConditionImplies(FC2, C1));
173
174 // Adding a new constraint to the forked flow condition does not affect its
175 // parent flow condition.
176 auto &C2 = Context.createAtomicBoolValue();
177 Context.addFlowConditionConstraint(FC2, C2);
178 EXPECT_TRUE(Context.flowConditionImplies(FC2, C2));
179 EXPECT_FALSE(Context.flowConditionImplies(FC1, C2));
180 }
181
182 TEST_F(DataflowAnalysisContextTest, JoinFlowConditions) {
183 auto &C1 = Context.createAtomicBoolValue();
184 auto &C2 = Context.createAtomicBoolValue();
185 auto &C3 = Context.createAtomicBoolValue();
186
187 auto &FC1 = Context.makeFlowConditionToken();
188 Context.addFlowConditionConstraint(FC1, C1);
189 Context.addFlowConditionConstraint(FC1, C3);
190
191 auto &FC2 = Context.makeFlowConditionToken();
192 Context.addFlowConditionConstraint(FC2, C2);
193 Context.addFlowConditionConstraint(FC2, C3);
194
195 auto &FC3 = Context.joinFlowConditions(FC1, FC2);
196 EXPECT_FALSE(Context.flowConditionImplies(FC3, C1));
197 EXPECT_FALSE(Context.flowConditionImplies(FC3, C2));
198 EXPECT_TRUE(Context.flowConditionImplies(FC3, C3));
199 }
200
201 TEST_F(DataflowAnalysisContextTest, FlowConditionTautologies) {
202 // Fresh flow condition with empty/no constraints is always true.
203 auto &FC1 = Context.makeFlowConditionToken();
204 EXPECT_TRUE(Context.flowConditionIsTautology(FC1));
205
206 // Literal `true` is always true.
207 auto &FC2 = Context.makeFlowConditionToken();
208 Context.addFlowConditionConstraint(FC2, Context.getBoolLiteralValue(true));
209 EXPECT_TRUE(Context.flowConditionIsTautology(FC2));
210
211 // Literal `false` is never true.
212 auto &FC3 = Context.makeFlowConditionToken();
213 Context.addFlowConditionConstraint(FC3, Context.getBoolLiteralValue(false));
214 EXPECT_FALSE(Context.flowConditionIsTautology(FC3));
215
216 // We can't prove that an arbitrary bool A is always true...
217 auto &C1 = Context.createAtomicBoolValue();
218 auto &FC4 = Context.makeFlowConditionToken();
219 Context.addFlowConditionConstraint(FC4, C1);
220 EXPECT_FALSE(Context.flowConditionIsTautology(FC4));
221
222 // ... but we can prove A || !A is true.
223 auto &FC5 = Context.makeFlowConditionToken();
224 Context.addFlowConditionConstraint(
225 FC5, Context.getOrCreateDisjunction(C1, Context.getOrCreateNegation(C1)));
226 EXPECT_TRUE(Context.flowConditionIsTautology(FC5));
227 }
228
229 TEST_F(DataflowAnalysisContextTest, EquivBoolVals) {
230 auto &X = Context.createAtomicBoolValue();
231 auto &Y = Context.createAtomicBoolValue();
232 auto &Z = Context.createAtomicBoolValue();
233 auto &True = Context.getBoolLiteralValue(true);
234 auto &False = Context.getBoolLiteralValue(false);
235
236 // X == X
237 EXPECT_TRUE(Context.equivalentBoolValues(X, X));
238 // X != Y
239 EXPECT_FALSE(Context.equivalentBoolValues(X, Y));
240
241 // !X != X
242 EXPECT_FALSE(Context.equivalentBoolValues(Context.getOrCreateNegation(X), X));
243 // !(!X) = X
244 EXPECT_TRUE(Context.equivalentBoolValues(
245 Context.getOrCreateNegation(Context.getOrCreateNegation(X)), X));
246
247 // (X || X) == X
248 EXPECT_TRUE(
249 Context.equivalentBoolValues(Context.getOrCreateDisjunction(X, X), X));
250 // (X || Y) != X
251 EXPECT_FALSE(
252 Context.equivalentBoolValues(Context.getOrCreateDisjunction(X, Y), X));
253 // (X || True) == True
254 EXPECT_TRUE(Context.equivalentBoolValues(
255 Context.getOrCreateDisjunction(X, True), True));
256 // (X || False) == X
257 EXPECT_TRUE(Context.equivalentBoolValues(
258 Context.getOrCreateDisjunction(X, False), X));
259
260 // (X && X) == X
261 EXPECT_TRUE(
262 Context.equivalentBoolValues(Context.getOrCreateConjunction(X, X), X));
263 // (X && Y) != X
264 EXPECT_FALSE(
265 Context.equivalentBoolValues(Context.getOrCreateConjunction(X, Y), X));
266 // (X && True) == X
267 EXPECT_TRUE(
268 Context.equivalentBoolValues(Context.getOrCreateConjunction(X, True), X));
269 // (X && False) == False
270 EXPECT_TRUE(Context.equivalentBoolValues(
271 Context.getOrCreateConjunction(X, False), False));
272
273 // (X || Y) == (Y || X)
274 EXPECT_TRUE(
275 Context.equivalentBoolValues(Context.getOrCreateDisjunction(X, Y),
276 Context.getOrCreateDisjunction(Y, X)));
277 // (X && Y) == (Y && X)
278 EXPECT_TRUE(
279 Context.equivalentBoolValues(Context.getOrCreateConjunction(X, Y),
280 Context.getOrCreateConjunction(Y, X)));
281
282 // ((X || Y) || Z) == (X || (Y || Z))
283 EXPECT_TRUE(Context.equivalentBoolValues(
284 Context.getOrCreateDisjunction(Context.getOrCreateDisjunction(X, Y), Z),
285 Context.getOrCreateDisjunction(X, Context.getOrCreateDisjunction(Y, Z))));
286 // ((X && Y) && Z) == (X && (Y && Z))
287 EXPECT_TRUE(Context.equivalentBoolValues(
288 Context.getOrCreateConjunction(Context.getOrCreateConjunction(X, Y), Z),
289 Context.getOrCreateConjunction(X, Context.getOrCreateConjunction(Y, Z))));
290 }
291
292 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
293 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionsTrueUnchanged) {
294 auto &True = Context.getBoolLiteralValue(true);
295 auto &Other = Context.createAtomicBoolValue();
296
297 // FC = True
298 auto &FC = Context.makeFlowConditionToken();
299 Context.addFlowConditionConstraint(FC, True);
300
301 // `True` should never be substituted
302 EXPECT_DEATH(Context.buildAndSubstituteFlowCondition(FC, {{&True, &Other}}),
303 "Do not substitute true/false boolean literals");
304 }
305
306 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionsFalseUnchanged) {
307 auto &False = Context.getBoolLiteralValue(false);
308 auto &Other = Context.createAtomicBoolValue();
309
310 // FC = False
311 auto &FC = Context.makeFlowConditionToken();
312 Context.addFlowConditionConstraint(FC, False);
313
314 // `False` should never be substituted
315 EXPECT_DEATH(Context.buildAndSubstituteFlowCondition(FC, {{&False, &Other}}),
316 "Do not substitute true/false boolean literals");
317 }
318 #endif
319
320 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionContainingAtomic) {
321 auto &X = Context.createAtomicBoolValue();
322 auto &True = Context.getBoolLiteralValue(true);
323 auto &False = Context.getBoolLiteralValue(false);
324
325 // FC = X
326 auto &FC = Context.makeFlowConditionToken();
327 Context.addFlowConditionConstraint(FC, X);
328
329 // If X is true, FC is true
330 auto &FCWithXTrue =
331 Context.buildAndSubstituteFlowCondition(FC, {{&X, &True}});
332 EXPECT_TRUE(Context.equivalentBoolValues(FCWithXTrue, True));
333
334 // If X is false, FC is false
335 auto &FC1WithXFalse =
336 Context.buildAndSubstituteFlowCondition(FC, {{&X, &False}});
337 EXPECT_TRUE(Context.equivalentBoolValues(FC1WithXFalse, False));
338 }
339
340 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionContainingNegation) {
341 auto &X = Context.createAtomicBoolValue();
342 auto &True = Context.getBoolLiteralValue(true);
343 auto &False = Context.getBoolLiteralValue(false);
344
345 // FC = !X
346 auto &FC = Context.makeFlowConditionToken();
347 Context.addFlowConditionConstraint(FC, Context.getOrCreateNegation(X));
348
349 // If X is true, FC is false
350 auto &FCWithXTrue =
351 Context.buildAndSubstituteFlowCondition(FC, {{&X, &True}});
352 EXPECT_TRUE(Context.equivalentBoolValues(FCWithXTrue, False));
353
354 // If X is false, FC is true
355 auto &FC1WithXFalse =
356 Context.buildAndSubstituteFlowCondition(FC, {{&X, &False}});
357 EXPECT_TRUE(Context.equivalentBoolValues(FC1WithXFalse, True));
358 }
359
360 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionContainingDisjunction) {
361 auto &X = Context.createAtomicBoolValue();
362 auto &Y = Context.createAtomicBoolValue();
363 auto &True = Context.getBoolLiteralValue(true);
364 auto &False = Context.getBoolLiteralValue(false);
365
366 // FC = X || Y
367 auto &FC = Context.makeFlowConditionToken();
368 Context.addFlowConditionConstraint(FC, Context.getOrCreateDisjunction(X, Y));
369
370 // If X is true, FC is true
371 auto &FCWithXTrue =
372 Context.buildAndSubstituteFlowCondition(FC, {{&X, &True}});
373 EXPECT_TRUE(Context.equivalentBoolValues(FCWithXTrue, True));
374
375 // If X is false, FC is equivalent to Y
376 auto &FC1WithXFalse =
377 Context.buildAndSubstituteFlowCondition(FC, {{&X, &False}});
378 EXPECT_TRUE(Context.equivalentBoolValues(FC1WithXFalse, Y));
379 }
380
381 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionContainingConjunction) {
382 auto &X = Context.createAtomicBoolValue();
383 auto &Y = Context.createAtomicBoolValue();
384 auto &True = Context.getBoolLiteralValue(true);
385 auto &False = Context.getBoolLiteralValue(false);
386
387 // FC = X && Y
388 auto &FC = Context.makeFlowConditionToken();
389 Context.addFlowConditionConstraint(FC, Context.getOrCreateConjunction(X, Y));
390
391 // If X is true, FC is equivalent to Y
392 auto &FCWithXTrue =
393 Context.buildAndSubstituteFlowCondition(FC, {{&X, &True}});
394 EXPECT_TRUE(Context.equivalentBoolValues(FCWithXTrue, Y));
395
396 // If X is false, FC is false
397 auto &FCWithXFalse =
398 Context.buildAndSubstituteFlowCondition(FC, {{&X, &False}});
399 EXPECT_TRUE(Context.equivalentBoolValues(FCWithXFalse, False));
400 }
401
402 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionContainingImplication) {
403 auto &X = Context.createAtomicBoolValue();
404 auto &Y = Context.createAtomicBoolValue();
405 auto &True = Context.getBoolLiteralValue(true);
406 auto &False = Context.getBoolLiteralValue(false);
407
408 // FC = (X => Y)
409 auto &FC = Context.makeFlowConditionToken();
410 Context.addFlowConditionConstraint(FC, Context.getOrCreateImplication(X, Y));
411
412 // If X is true, FC is equivalent to Y
413 auto &FCWithXTrue =
414 Context.buildAndSubstituteFlowCondition(FC, {{&X, &True}});
415 EXPECT_TRUE(Context.equivalentBoolValues(FCWithXTrue, Y));
416
417 // If X is false, FC is true
418 auto &FC1WithXFalse =
419 Context.buildAndSubstituteFlowCondition(FC, {{&X, &False}});
420 EXPECT_TRUE(Context.equivalentBoolValues(FC1WithXFalse, True));
421
422 // If Y is true, FC is true
423 auto &FCWithYTrue =
424 Context.buildAndSubstituteFlowCondition(FC, {{&Y, &True}});
425 EXPECT_TRUE(Context.equivalentBoolValues(FCWithYTrue, True));
426
427 // If Y is false, FC is equivalent to !X
428 auto &FCWithYFalse =
429 Context.buildAndSubstituteFlowCondition(FC, {{&Y, &False}});
430 EXPECT_TRUE(Context.equivalentBoolValues(FCWithYFalse,
431 Context.getOrCreateNegation(X)));
432 }
433
434 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionContainingBiconditional) {
435 auto &X = Context.createAtomicBoolValue();
436 auto &Y = Context.createAtomicBoolValue();
437 auto &True = Context.getBoolLiteralValue(true);
438 auto &False = Context.getBoolLiteralValue(false);
439
440 // FC = (X <=> Y)
441 auto &FC = Context.makeFlowConditionToken();
442 Context.addFlowConditionConstraint(FC, Context.getOrCreateIff(X, Y));
443
444 // If X is true, FC is equivalent to Y
445 auto &FCWithXTrue =
446 Context.buildAndSubstituteFlowCondition(FC, {{&X, &True}});
447 EXPECT_TRUE(Context.equivalentBoolValues(FCWithXTrue, Y));
448
449 // If X is false, FC is equivalent to !Y
450 auto &FC1WithXFalse =
451 Context.buildAndSubstituteFlowCondition(FC, {{&X, &False}});
452 EXPECT_TRUE(Context.equivalentBoolValues(FC1WithXFalse,
453 Context.getOrCreateNegation(Y)));
454
455 // If Y is true, FC is equivalent to X
456 auto &FCWithYTrue =
457 Context.buildAndSubstituteFlowCondition(FC, {{&Y, &True}});
458 EXPECT_TRUE(Context.equivalentBoolValues(FCWithYTrue, X));
459
460 // If Y is false, FC is equivalent to !X
461 auto &FCWithYFalse =
462 Context.buildAndSubstituteFlowCondition(FC, {{&Y, &False}});
463 EXPECT_TRUE(Context.equivalentBoolValues(FCWithYFalse,
464 Context.getOrCreateNegation(X)));
465 }
466
467 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionsForkedFC) {
468 auto &X = Context.createAtomicBoolValue();
469 auto &Y = Context.createAtomicBoolValue();
470 auto &Z = Context.createAtomicBoolValue();
471 auto &True = Context.getBoolLiteralValue(true);
472 auto &False = Context.getBoolLiteralValue(false);
473
474 // FC = X && Y
475 auto &FC = Context.makeFlowConditionToken();
476 Context.addFlowConditionConstraint(FC, Context.getOrCreateConjunction(X, Y));
477 // ForkedFC = FC && Z = X && Y && Z
478 auto &ForkedFC = Context.forkFlowCondition(FC);
479 Context.addFlowConditionConstraint(ForkedFC, Z);
480
481 // If any of X,Y,Z is true in ForkedFC, ForkedFC = X && Y && Z is equivalent
482 // to evaluating the conjunction of the remaining values
483 auto &ForkedFCWithZTrue =
484 Context.buildAndSubstituteFlowCondition(ForkedFC, {{&Z, &True}});
485 EXPECT_TRUE(Context.equivalentBoolValues(
486 ForkedFCWithZTrue, Context.getOrCreateConjunction(X, Y)));
487 auto &ForkedFCWithYAndZTrue = Context.buildAndSubstituteFlowCondition(
488 ForkedFC, {{&Y, &True}, {&Z, &True}});
489 EXPECT_TRUE(Context.equivalentBoolValues(ForkedFCWithYAndZTrue, X));
490
491 // If any of X,Y,Z is false, ForkedFC is false
492 auto &ForkedFCWithXFalse =
493 Context.buildAndSubstituteFlowCondition(ForkedFC, {{&X, &False}});
494 auto &ForkedFCWithYFalse =
495 Context.buildAndSubstituteFlowCondition(ForkedFC, {{&Y, &False}});
496 auto &ForkedFCWithZFalse =
497 Context.buildAndSubstituteFlowCondition(ForkedFC, {{&Z, &False}});
498 EXPECT_TRUE(Context.equivalentBoolValues(ForkedFCWithXFalse, False));
499 EXPECT_TRUE(Context.equivalentBoolValues(ForkedFCWithYFalse, False));
500 EXPECT_TRUE(Context.equivalentBoolValues(ForkedFCWithZFalse, False));
501 }
502
503 TEST_F(DataflowAnalysisContextTest, SubstituteFlowConditionsJoinedFC) {
504 auto &X = Context.createAtomicBoolValue();
505 auto &Y = Context.createAtomicBoolValue();
506 auto &Z = Context.createAtomicBoolValue();
507 auto &True = Context.getBoolLiteralValue(true);
508 auto &False = Context.getBoolLiteralValue(false);
509
510 // FC1 = X
511 auto &FC1 = Context.makeFlowConditionToken();
512 Context.addFlowConditionConstraint(FC1, X);
513 // FC2 = Y
514 auto &FC2 = Context.makeFlowConditionToken();
515 Context.addFlowConditionConstraint(FC2, Y);
516 // JoinedFC = (FC1 || FC2) && Z = (X || Y) && Z
517 auto &JoinedFC = Context.joinFlowConditions(FC1, FC2);
518 Context.addFlowConditionConstraint(JoinedFC, Z);
519
520 // If any of X, Y is true, JoinedFC is equivalent to Z
521 auto &JoinedFCWithXTrue =
522 Context.buildAndSubstituteFlowCondition(JoinedFC, {{&X, &True}});
523 auto &JoinedFCWithYTrue =
524 Context.buildAndSubstituteFlowCondition(JoinedFC, {{&Y, &True}});
525 EXPECT_TRUE(Context.equivalentBoolValues(JoinedFCWithXTrue, Z));
526 EXPECT_TRUE(Context.equivalentBoolValues(JoinedFCWithYTrue, Z));
527
528 // If Z is true, JoinedFC is equivalent to (X || Y)
529 auto &JoinedFCWithZTrue =
530 Context.buildAndSubstituteFlowCondition(JoinedFC, {{&Z, &True}});
531 EXPECT_TRUE(Context.equivalentBoolValues(
532 JoinedFCWithZTrue, Context.getOrCreateDisjunction(X, Y)));
533
534 // If any of X, Y is false, JoinedFC is equivalent to the conjunction of the
535 // other value and Z
536 auto &JoinedFCWithXFalse =
537 Context.buildAndSubstituteFlowCondition(JoinedFC, {{&X, &False}});
538 auto &JoinedFCWithYFalse =
539 Context.buildAndSubstituteFlowCondition(JoinedFC, {{&Y, &False}});
540 EXPECT_TRUE(Context.equivalentBoolValues(
541 JoinedFCWithXFalse, Context.getOrCreateConjunction(Y, Z)));
542 EXPECT_TRUE(Context.equivalentBoolValues(
543 JoinedFCWithYFalse, Context.getOrCreateConjunction(X, Z)));
544
545 // If Z is false, JoinedFC is false
546 auto &JoinedFCWithZFalse =
547 Context.buildAndSubstituteFlowCondition(JoinedFC, {{&Z, &False}});
548 EXPECT_TRUE(Context.equivalentBoolValues(JoinedFCWithZFalse, False));
549 }
550
551 } // namespace