Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 1 | //== Environment.cpp - Map from Stmt* to Locations/Values -------*- C++ -*--==// |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 13 | |
| 14 | #include "clang/Analysis/AnalysisContext.h" |
| 15 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/GRState.h" |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 20 | SVal Environment::GetSVal(const Stmt *E, ValueManager& ValMgr) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 22 | for (;;) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 24 | switch (E->getStmtClass()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | |
| 26 | case Stmt::AddrLabelExprClass: |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 27 | return ValMgr.makeLoc(cast<AddrLabelExpr>(E)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 29 | // ParenExprs are no-ops. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
| 31 | case Stmt::ParenExprClass: |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 32 | E = cast<ParenExpr>(E)->getSubExpr(); |
| 33 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 35 | case Stmt::CharacterLiteralClass: { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 36 | const CharacterLiteral* C = cast<CharacterLiteral>(E); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 37 | return ValMgr.makeIntVal(C->getValue(), C->getType()); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 38 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 40 | case Stmt::IntegerLiteralClass: { |
Zhongxing Xu | bc37b8d | 2010-01-09 09:16:47 +0000 | [diff] [blame] | 41 | // In C++, this expression may have been bound to a temporary object. |
| 42 | SVal const *X = ExprBindings.lookup(E); |
| 43 | if (X) |
| 44 | return *X; |
| 45 | else |
| 46 | return ValMgr.makeIntVal(cast<IntegerLiteral>(E)); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 47 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 49 | // Casts where the source and target type are the same |
| 50 | // are no-ops. We blast through these to get the descendant |
| 51 | // subexpression that has a value. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 53 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 54 | case Stmt::CStyleCastExprClass: { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 55 | const CastExpr* C = cast<CastExpr>(E); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 56 | QualType CT = C->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 58 | if (CT->isVoidType()) |
| 59 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 61 | break; |
| 62 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 64 | // Handle all other Stmt* using a lookup. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 66 | default: |
| 67 | break; |
| 68 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 70 | break; |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 73 | return LookupExpr(E); |
| 74 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 76 | Environment EnvironmentManager::BindExpr(Environment Env, const Stmt *S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | SVal V, bool Invalidate) { |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 78 | assert(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
| 80 | if (V.isUnknown()) { |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 81 | if (Invalidate) |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 82 | return Environment(F.Remove(Env.ExprBindings, S)); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 83 | else |
| 84 | return Env; |
| 85 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 86 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 87 | return Environment(F.Add(Env.ExprBindings, S, V)); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 88 | } |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 90 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 91 | class MarkLiveCallback : public SymbolVisitor { |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 92 | SymbolReaper &SymReaper; |
| 93 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | MarkLiveCallback(SymbolReaper &symreaper) : SymReaper(symreaper) {} |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 95 | bool VisitSymbol(SymbolRef sym) { SymReaper.markLive(sym); return true; } |
| 96 | }; |
| 97 | } // end anonymous namespace |
| 98 | |
Zhongxing Xu | 7b73b92 | 2010-04-05 13:16:29 +0000 | [diff] [blame^] | 99 | static bool isBlockExprInCallers(const Stmt *E, const LocationContext *LC) { |
| 100 | const LocationContext *ParentLC = LC->getParent(); |
| 101 | while (ParentLC) { |
| 102 | CFG &C = *ParentLC->getCFG(); |
| 103 | if (C.isBlkExpr(E)) |
| 104 | return true; |
| 105 | ParentLC = ParentLC->getParent(); |
| 106 | } |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | |
Zhongxing Xu | 9d8d0fc | 2009-03-12 07:54:17 +0000 | [diff] [blame] | 112 | // RemoveDeadBindings: |
| 113 | // - Remove subexpression bindings. |
| 114 | // - Remove dead block expression bindings. |
| 115 | // - Keep live block expression bindings: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | // - Mark their reachable symbols live in SymbolReaper, |
Zhongxing Xu | 9d8d0fc | 2009-03-12 07:54:17 +0000 | [diff] [blame] | 117 | // see ScanReachableSymbols. |
| 118 | // - Mark the region in DRoots if the binding is a loc::MemRegionVal. |
| 119 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | Environment |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 121 | EnvironmentManager::RemoveDeadBindings(Environment Env, const Stmt *S, |
| 122 | SymbolReaper &SymReaper, |
| 123 | const GRState *ST, |
| 124 | llvm::SmallVectorImpl<const MemRegion*> &DRoots) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 126 | CFG &C = *SymReaper.getLocationContext()->getCFG(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 128 | // We construct a new Environment object entirely, as this is cheaper than |
| 129 | // individually removing all the subexpression bindings (which will greatly |
| 130 | // outnumber block-level expression bindings). |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 131 | Environment NewEnv = getInitialEnvironment(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 133 | // Iterate over the block-expr bindings. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | for (Environment::iterator I = Env.begin(), E = Env.end(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 135 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 137 | const Stmt *BlkExpr = I.getKey(); |
Zhongxing Xu | 7b73b92 | 2010-04-05 13:16:29 +0000 | [diff] [blame^] | 138 | const SVal &X = I.getData(); |
| 139 | |
| 140 | // Block-level expressions in callers are assumed always live. |
| 141 | if (isBlockExprInCallers(BlkExpr, SymReaper.getLocationContext())) { |
| 142 | NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, BlkExpr, X); |
| 143 | |
| 144 | if (isa<loc::MemRegionVal>(X)) { |
| 145 | const MemRegion* R = cast<loc::MemRegionVal>(X).getRegion(); |
| 146 | DRoots.push_back(R); |
| 147 | } |
| 148 | |
| 149 | // Mark all symbols in the block expr's value live. |
| 150 | MarkLiveCallback cb(SymReaper); |
| 151 | ST->scanReachableSymbols(X, cb); |
| 152 | continue; |
| 153 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 155 | // Not a block-level expression? |
| 156 | if (!C.isBlkExpr(BlkExpr)) |
| 157 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 159 | if (SymReaper.isLive(S, BlkExpr)) { |
| 160 | // Copy the binding to the new map. |
| 161 | NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, BlkExpr, X); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 163 | // If the block expr's value is a memory region, then mark that region. |
Zhongxing Xu | ce2f9bd | 2009-06-30 13:00:53 +0000 | [diff] [blame] | 164 | if (isa<loc::MemRegionVal>(X)) { |
| 165 | const MemRegion* R = cast<loc::MemRegionVal>(X).getRegion(); |
| 166 | DRoots.push_back(R); |
Zhongxing Xu | ce2f9bd | 2009-06-30 13:00:53 +0000 | [diff] [blame] | 167 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 169 | // Mark all symbols in the block expr's value live. |
| 170 | MarkLiveCallback cb(SymReaper); |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 171 | ST->scanReachableSymbols(X, cb); |
| 172 | continue; |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 173 | } |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 174 | |
| 175 | // Otherwise the expression is dead with a couple exceptions. |
| 176 | // Do not misclean LogicalExpr or ConditionalOperator. It is dead at the |
| 177 | // beginning of itself, but we need its UndefinedVal to determine its |
| 178 | // SVal. |
| 179 | if (X.isUndef() && cast<UndefinedVal>(X).getData()) |
| 180 | NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, BlkExpr, X); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 183 | return NewEnv; |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 184 | } |