Environment.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //== Environment.cpp - Map from Stmt* to Locations/Values -------*- 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. //
  10. // This file defined the Environment and EnvironmentManager classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ExprCXX.h"
  14. #include "clang/AST/ExprObjC.h"
  15. #include "clang/Analysis/AnalysisContext.h"
  16. #include "clang/Analysis/CFG.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace clang;
  20. using namespace ento;
  21. static const Expr *ignoreTransparentExprs(const Expr *E) {
  22. E = E->IgnoreParens();
  23. switch (E->getStmtClass()) {
  24. case Stmt::OpaqueValueExprClass:
  25. E = cast<OpaqueValueExpr>(E)->getSourceExpr();
  26. break;
  27. case Stmt::ExprWithCleanupsClass:
  28. E = cast<ExprWithCleanups>(E)->getSubExpr();
  29. break;
  30. case Stmt::CXXBindTemporaryExprClass:
  31. E = cast<CXXBindTemporaryExpr>(E)->getSubExpr();
  32. break;
  33. case Stmt::SubstNonTypeTemplateParmExprClass:
  34. E = cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement();
  35. break;
  36. default:
  37. // This is the base case: we can't look through more than we already have.
  38. return E;
  39. }
  40. return ignoreTransparentExprs(E);
  41. }
  42. static const Stmt *ignoreTransparentExprs(const Stmt *S) {
  43. if (const Expr *E = dyn_cast<Expr>(S))
  44. return ignoreTransparentExprs(E);
  45. return S;
  46. }
  47. EnvironmentEntry::EnvironmentEntry(const Stmt *S, const LocationContext *L)
  48. : std::pair<const Stmt *,
  49. const StackFrameContext *>(ignoreTransparentExprs(S),
  50. L ? L->getCurrentStackFrame()
  51. : nullptr) {}
  52. SVal Environment::lookupExpr(const EnvironmentEntry &E) const {
  53. const SVal* X = ExprBindings.lookup(E);
  54. if (X) {
  55. SVal V = *X;
  56. return V;
  57. }
  58. return UnknownVal();
  59. }
  60. SVal Environment::getSVal(const EnvironmentEntry &Entry,
  61. SValBuilder& svalBuilder) const {
  62. const Stmt *S = Entry.getStmt();
  63. const LocationContext *LCtx = Entry.getLocationContext();
  64. switch (S->getStmtClass()) {
  65. case Stmt::CXXBindTemporaryExprClass:
  66. case Stmt::ExprWithCleanupsClass:
  67. case Stmt::GenericSelectionExprClass:
  68. case Stmt::OpaqueValueExprClass:
  69. case Stmt::ParenExprClass:
  70. case Stmt::SubstNonTypeTemplateParmExprClass:
  71. llvm_unreachable("Should have been handled by ignoreTransparentExprs");
  72. case Stmt::AddrLabelExprClass:
  73. case Stmt::CharacterLiteralClass:
  74. case Stmt::CXXBoolLiteralExprClass:
  75. case Stmt::CXXScalarValueInitExprClass:
  76. case Stmt::ImplicitValueInitExprClass:
  77. case Stmt::IntegerLiteralClass:
  78. case Stmt::ObjCBoolLiteralExprClass:
  79. case Stmt::CXXNullPtrLiteralExprClass:
  80. case Stmt::ObjCStringLiteralClass:
  81. case Stmt::StringLiteralClass:
  82. // Known constants; defer to SValBuilder.
  83. return svalBuilder.getConstantVal(cast<Expr>(S)).getValue();
  84. case Stmt::ReturnStmtClass: {
  85. const ReturnStmt *RS = cast<ReturnStmt>(S);
  86. if (const Expr *RE = RS->getRetValue())
  87. return getSVal(EnvironmentEntry(RE, LCtx), svalBuilder);
  88. return UndefinedVal();
  89. }
  90. // Handle all other Stmt* using a lookup.
  91. default:
  92. return lookupExpr(EnvironmentEntry(S, LCtx));
  93. }
  94. }
  95. Environment EnvironmentManager::bindExpr(Environment Env,
  96. const EnvironmentEntry &E,
  97. SVal V,
  98. bool Invalidate) {
  99. if (V.isUnknown()) {
  100. if (Invalidate)
  101. return Environment(F.remove(Env.ExprBindings, E));
  102. else
  103. return Env;
  104. }
  105. return Environment(F.add(Env.ExprBindings, E, V));
  106. }
  107. namespace {
  108. class MarkLiveCallback : public SymbolVisitor {
  109. SymbolReaper &SymReaper;
  110. public:
  111. MarkLiveCallback(SymbolReaper &symreaper) : SymReaper(symreaper) {}
  112. bool VisitSymbol(SymbolRef sym) override {
  113. SymReaper.markLive(sym);
  114. return true;
  115. }
  116. bool VisitMemRegion(const MemRegion *R) override {
  117. SymReaper.markLive(R);
  118. return true;
  119. }
  120. };
  121. } // end anonymous namespace
  122. // removeDeadBindings:
  123. // - Remove subexpression bindings.
  124. // - Remove dead block expression bindings.
  125. // - Keep live block expression bindings:
  126. // - Mark their reachable symbols live in SymbolReaper,
  127. // see ScanReachableSymbols.
  128. // - Mark the region in DRoots if the binding is a loc::MemRegionVal.
  129. Environment
  130. EnvironmentManager::removeDeadBindings(Environment Env,
  131. SymbolReaper &SymReaper,
  132. ProgramStateRef ST) {
  133. // We construct a new Environment object entirely, as this is cheaper than
  134. // individually removing all the subexpression bindings (which will greatly
  135. // outnumber block-level expression bindings).
  136. Environment NewEnv = getInitialEnvironment();
  137. MarkLiveCallback CB(SymReaper);
  138. ScanReachableSymbols RSScaner(ST, CB);
  139. llvm::ImmutableMapRef<EnvironmentEntry,SVal>
  140. EBMapRef(NewEnv.ExprBindings.getRootWithoutRetain(),
  141. F.getTreeFactory());
  142. // Iterate over the block-expr bindings.
  143. for (Environment::iterator I = Env.begin(), E = Env.end();
  144. I != E; ++I) {
  145. const EnvironmentEntry &BlkExpr = I.getKey();
  146. const SVal &X = I.getData();
  147. if (SymReaper.isLive(BlkExpr.getStmt(), BlkExpr.getLocationContext())) {
  148. // Copy the binding to the new map.
  149. EBMapRef = EBMapRef.add(BlkExpr, X);
  150. // If the block expr's value is a memory region, then mark that region.
  151. if (Optional<loc::MemRegionVal> R = X.getAs<loc::MemRegionVal>())
  152. SymReaper.markLive(R->getRegion());
  153. // Mark all symbols in the block expr's value live.
  154. RSScaner.scan(X);
  155. continue;
  156. } else {
  157. SymExpr::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
  158. for (; SI != SE; ++SI)
  159. SymReaper.maybeDead(*SI);
  160. }
  161. }
  162. NewEnv.ExprBindings = EBMapRef.asImmutableMap();
  163. return NewEnv;
  164. }
  165. void Environment::print(raw_ostream &Out, const char *NL,
  166. const char *Sep) const {
  167. bool isFirst = true;
  168. for (Environment::iterator I = begin(), E = end(); I != E; ++I) {
  169. const EnvironmentEntry &En = I.getKey();
  170. if (isFirst) {
  171. Out << NL << NL
  172. << "Expressions:"
  173. << NL;
  174. isFirst = false;
  175. } else {
  176. Out << NL;
  177. }
  178. const Stmt *S = En.getStmt();
  179. Out << " (" << (const void*) En.getLocationContext() << ','
  180. << (const void*) S << ") ";
  181. LangOptions LO; // FIXME.
  182. S->printPretty(Out, nullptr, PrintingPolicy(LO));
  183. Out << " : " << I.getData();
  184. }
  185. }