ThreadSafetyLogical.cpp 4.1 KB

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