ThreadSafetyLogical.cpp 4.1 KB

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