ThreadSafetyLogical.cpp revision 1.1 1 1.1 joerg //===- ThreadSafetyLogical.cpp ---------------------------------*- C++ --*-===//
2 1.1 joerg //
3 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 1.1 joerg // See https://llvm.org/LICENSE.txt for license information.
5 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 1.1 joerg //
7 1.1 joerg //===----------------------------------------------------------------------===//
8 1.1 joerg // This file defines a representation for logical expressions with SExpr leaves
9 1.1 joerg // that are used as part of fact-checking capability expressions.
10 1.1 joerg //===----------------------------------------------------------------------===//
11 1.1 joerg
12 1.1 joerg #include "clang/Analysis/Analyses/ThreadSafetyLogical.h"
13 1.1 joerg
14 1.1 joerg using namespace llvm;
15 1.1 joerg using namespace clang::threadSafety::lexpr;
16 1.1 joerg
17 1.1 joerg // Implication. We implement De Morgan's Laws by maintaining LNeg and RNeg
18 1.1 joerg // to keep track of whether LHS and RHS are negated.
19 1.1 joerg static bool implies(const LExpr *LHS, bool LNeg, const LExpr *RHS, bool RNeg) {
20 1.1 joerg // In comments below, we write => for implication.
21 1.1 joerg
22 1.1 joerg // Calculates the logical AND implication operator.
23 1.1 joerg const auto LeftAndOperator = [=](const BinOp *A) {
24 1.1 joerg return implies(A->left(), LNeg, RHS, RNeg) &&
25 1.1 joerg implies(A->right(), LNeg, RHS, RNeg);
26 1.1 joerg };
27 1.1 joerg const auto RightAndOperator = [=](const BinOp *A) {
28 1.1 joerg return implies(LHS, LNeg, A->left(), RNeg) &&
29 1.1 joerg implies(LHS, LNeg, A->right(), RNeg);
30 1.1 joerg };
31 1.1 joerg
32 1.1 joerg // Calculates the logical OR implication operator.
33 1.1 joerg const auto LeftOrOperator = [=](const BinOp *A) {
34 1.1 joerg return implies(A->left(), LNeg, RHS, RNeg) ||
35 1.1 joerg implies(A->right(), LNeg, RHS, RNeg);
36 1.1 joerg };
37 1.1 joerg const auto RightOrOperator = [=](const BinOp *A) {
38 1.1 joerg return implies(LHS, LNeg, A->left(), RNeg) ||
39 1.1 joerg implies(LHS, LNeg, A->right(), RNeg);
40 1.1 joerg };
41 1.1 joerg
42 1.1 joerg // Recurse on right.
43 1.1 joerg switch (RHS->kind()) {
44 1.1 joerg case LExpr::And:
45 1.1 joerg // When performing right recursion:
46 1.1 joerg // C => A & B [if] C => A and C => B
47 1.1 joerg // When performing right recursion (negated):
48 1.1 joerg // C => !(A & B) [if] C => !A | !B [===] C => !A or C => !B
49 1.1 joerg return RNeg ? RightOrOperator(cast<And>(RHS))
50 1.1 joerg : RightAndOperator(cast<And>(RHS));
51 1.1 joerg case LExpr::Or:
52 1.1 joerg // When performing right recursion:
53 1.1 joerg // C => (A | B) [if] C => A or C => B
54 1.1 joerg // When performing right recursion (negated):
55 1.1 joerg // C => !(A | B) [if] C => !A & !B [===] C => !A and C => !B
56 1.1 joerg return RNeg ? RightAndOperator(cast<Or>(RHS))
57 1.1 joerg : RightOrOperator(cast<Or>(RHS));
58 1.1 joerg case LExpr::Not:
59 1.1 joerg // Note that C => !A is very different from !(C => A). It would be incorrect
60 1.1 joerg // to return !implies(LHS, RHS).
61 1.1 joerg return implies(LHS, LNeg, cast<Not>(RHS)->exp(), !RNeg);
62 1.1 joerg case LExpr::Terminal:
63 1.1 joerg // After reaching the terminal, it's time to recurse on the left.
64 1.1 joerg break;
65 1.1 joerg }
66 1.1 joerg
67 1.1 joerg // RHS is now a terminal. Recurse on Left.
68 1.1 joerg switch (LHS->kind()) {
69 1.1 joerg case LExpr::And:
70 1.1 joerg // When performing left recursion:
71 1.1 joerg // A & B => C [if] A => C or B => C
72 1.1 joerg // When performing left recursion (negated):
73 1.1 joerg // !(A & B) => C [if] !A | !B => C [===] !A => C and !B => C
74 1.1 joerg return LNeg ? LeftAndOperator(cast<And>(LHS))
75 1.1 joerg : LeftOrOperator(cast<And>(LHS));
76 1.1 joerg case LExpr::Or:
77 1.1 joerg // When performing left recursion:
78 1.1 joerg // A | B => C [if] A => C and B => C
79 1.1 joerg // When performing left recursion (negated):
80 1.1 joerg // !(A | B) => C [if] !A & !B => C [===] !A => C or !B => C
81 1.1 joerg return LNeg ? LeftOrOperator(cast<Or>(LHS))
82 1.1 joerg : LeftAndOperator(cast<Or>(LHS));
83 1.1 joerg case LExpr::Not:
84 1.1 joerg // Note that A => !C is very different from !(A => C). It would be incorrect
85 1.1 joerg // to return !implies(LHS, RHS).
86 1.1 joerg return implies(cast<Not>(LHS)->exp(), !LNeg, RHS, RNeg);
87 1.1 joerg case LExpr::Terminal:
88 1.1 joerg // After reaching the terminal, it's time to perform identity comparisons.
89 1.1 joerg break;
90 1.1 joerg }
91 1.1 joerg
92 1.1 joerg // A => A
93 1.1 joerg // !A => !A
94 1.1 joerg if (LNeg != RNeg)
95 1.1 joerg return false;
96 1.1 joerg
97 1.1 joerg // FIXME -- this should compare SExprs for equality, not pointer equality.
98 1.1 joerg return cast<Terminal>(LHS)->expr() == cast<Terminal>(RHS)->expr();
99 1.1 joerg }
100 1.1 joerg
101 1.1 joerg namespace clang {
102 1.1 joerg namespace threadSafety {
103 1.1 joerg namespace lexpr {
104 1.1 joerg
105 1.1 joerg bool implies(const LExpr *LHS, const LExpr *RHS) {
106 1.1 joerg // Start out by assuming that LHS and RHS are not negated.
107 1.1 joerg return ::implies(LHS, false, RHS, false);
108 1.1 joerg }
109 1.1 joerg }
110 1.1 joerg }
111 1.1 joerg }
112