Path: blob/main/contrib/llvm-project/clang/lib/Analysis/ThreadSafetyLogical.cpp
35233 views
//===- ThreadSafetyLogical.cpp ---------------------------------*- C++ --*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// This file defines a representation for logical expressions with SExpr leaves8// that are used as part of fact-checking capability expressions.9//===----------------------------------------------------------------------===//1011#include "clang/Analysis/Analyses/ThreadSafetyLogical.h"1213using namespace llvm;14using namespace clang::threadSafety::lexpr;1516// Implication. We implement De Morgan's Laws by maintaining LNeg and RNeg17// to keep track of whether LHS and RHS are negated.18static bool implies(const LExpr *LHS, bool LNeg, const LExpr *RHS, bool RNeg) {19// In comments below, we write => for implication.2021// Calculates the logical AND implication operator.22const auto LeftAndOperator = [=](const BinOp *A) {23return implies(A->left(), LNeg, RHS, RNeg) &&24implies(A->right(), LNeg, RHS, RNeg);25};26const auto RightAndOperator = [=](const BinOp *A) {27return implies(LHS, LNeg, A->left(), RNeg) &&28implies(LHS, LNeg, A->right(), RNeg);29};3031// Calculates the logical OR implication operator.32const auto LeftOrOperator = [=](const BinOp *A) {33return implies(A->left(), LNeg, RHS, RNeg) ||34implies(A->right(), LNeg, RHS, RNeg);35};36const auto RightOrOperator = [=](const BinOp *A) {37return implies(LHS, LNeg, A->left(), RNeg) ||38implies(LHS, LNeg, A->right(), RNeg);39};4041// Recurse on right.42switch (RHS->kind()) {43case LExpr::And:44// When performing right recursion:45// C => A & B [if] C => A and C => B46// When performing right recursion (negated):47// C => !(A & B) [if] C => !A | !B [===] C => !A or C => !B48return RNeg ? RightOrOperator(cast<And>(RHS))49: RightAndOperator(cast<And>(RHS));50case LExpr::Or:51// When performing right recursion:52// C => (A | B) [if] C => A or C => B53// When performing right recursion (negated):54// C => !(A | B) [if] C => !A & !B [===] C => !A and C => !B55return RNeg ? RightAndOperator(cast<Or>(RHS))56: RightOrOperator(cast<Or>(RHS));57case LExpr::Not:58// Note that C => !A is very different from !(C => A). It would be incorrect59// to return !implies(LHS, RHS).60return implies(LHS, LNeg, cast<Not>(RHS)->exp(), !RNeg);61case LExpr::Terminal:62// After reaching the terminal, it's time to recurse on the left.63break;64}6566// RHS is now a terminal. Recurse on Left.67switch (LHS->kind()) {68case LExpr::And:69// When performing left recursion:70// A & B => C [if] A => C or B => C71// When performing left recursion (negated):72// !(A & B) => C [if] !A | !B => C [===] !A => C and !B => C73return LNeg ? LeftAndOperator(cast<And>(LHS))74: LeftOrOperator(cast<And>(LHS));75case LExpr::Or:76// When performing left recursion:77// A | B => C [if] A => C and B => C78// When performing left recursion (negated):79// !(A | B) => C [if] !A & !B => C [===] !A => C or !B => C80return LNeg ? LeftOrOperator(cast<Or>(LHS))81: LeftAndOperator(cast<Or>(LHS));82case LExpr::Not:83// Note that A => !C is very different from !(A => C). It would be incorrect84// to return !implies(LHS, RHS).85return implies(cast<Not>(LHS)->exp(), !LNeg, RHS, RNeg);86case LExpr::Terminal:87// After reaching the terminal, it's time to perform identity comparisons.88break;89}9091// A => A92// !A => !A93if (LNeg != RNeg)94return false;9596// FIXME -- this should compare SExprs for equality, not pointer equality.97return cast<Terminal>(LHS)->expr() == cast<Terminal>(RHS)->expr();98}99100namespace clang {101namespace threadSafety {102namespace lexpr {103104bool implies(const LExpr *LHS, const LExpr *RHS) {105// Start out by assuming that LHS and RHS are not negated.106return ::implies(LHS, false, RHS, false);107}108}109}110}111112113