Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 1 | // GRSimpleVals.cpp - Transfer functions for tracking simple 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 | // |
Gabor Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines GRSimpleVals, a sub-class of GRTransferFuncs that |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 11 | // provides transfer functions for performing simple value tracking with |
| 12 | // limited support for symbolics. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "GRSimpleVals.h" |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 17 | #include "BasicObjCFoundationChecks.h" |
Ted Kremenek | 87abc03 | 2008-04-02 22:03:53 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathSensitive/ValueState.h" |
| 21 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame^] | 23 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 5c06121 | 2008-02-27 17:56:16 +0000 | [diff] [blame] | 25 | #include <sstream> |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
Ted Kremenek | dd59811 | 2008-04-02 07:05:46 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dd59811 | 2008-04-02 07:05:46 +0000 | [diff] [blame] | 30 | // Utility functions. |
| 31 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 3ae30f8 | 2008-04-06 04:22:39 +0000 | [diff] [blame] | 33 | template <typename ITERATOR> inline |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 34 | ExplodedNode<ValueState>* GetNode(ITERATOR I) { |
Ted Kremenek | 503d613 | 2008-04-02 05:15:22 +0000 | [diff] [blame] | 35 | return *I; |
| 36 | } |
| 37 | |
Chris Lattner | 3ae30f8 | 2008-04-06 04:22:39 +0000 | [diff] [blame] | 38 | template <> inline |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 39 | ExplodedNode<ValueState>* GetNode(GRExprEngine::undef_arg_iterator I) { |
Ted Kremenek | 503d613 | 2008-04-02 05:15:22 +0000 | [diff] [blame] | 40 | return I->first; |
| 41 | } |
Ted Kremenek | dd59811 | 2008-04-02 07:05:46 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 43 | template <typename ITER> |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 44 | void GenericEmitWarnings(BugReporter& BR, BugType& D, ITER I, ITER E) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 45 | |
| 46 | for (; I != E; ++I) { |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 47 | BugReport R(D, GetNode(I)); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 48 | BR.EmitWarning(R); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | // Bug Descriptions. |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | namespace { |
| 57 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 58 | class VISIBILITY_HIDDEN NullDeref : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 59 | public: |
| 60 | virtual const char* getName() const { |
| 61 | return "null dereference"; |
| 62 | } |
| 63 | |
| 64 | virtual const char* getDescription() const { |
| 65 | return "Dereference of null pointer."; |
| 66 | } |
| 67 | |
| 68 | virtual void EmitWarnings(BugReporter& BR) { |
| 69 | GRExprEngine& Eng = BR.getEngine(); |
| 70 | GenericEmitWarnings(BR, *this, Eng.null_derefs_begin(), |
| 71 | Eng.null_derefs_end()); |
| 72 | } |
| 73 | }; |
| 74 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 75 | class VISIBILITY_HIDDEN UndefDeref : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 76 | public: |
| 77 | virtual const char* getName() const { |
| 78 | return "bad dereference"; |
| 79 | } |
| 80 | |
| 81 | virtual const char* getDescription() const { |
| 82 | return "Dereference of undefined value."; |
| 83 | } |
| 84 | |
| 85 | virtual void EmitWarnings(BugReporter& BR) { |
| 86 | GRExprEngine& Eng = BR.getEngine(); |
| 87 | GenericEmitWarnings(BR, *this, Eng.undef_derefs_begin(), |
| 88 | Eng.undef_derefs_end()); |
| 89 | } |
| 90 | }; |
| 91 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 92 | class VISIBILITY_HIDDEN UndefBranch : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 93 | public: |
| 94 | virtual const char* getName() const { |
| 95 | return "uninitialized value"; |
| 96 | } |
| 97 | |
| 98 | virtual const char* getDescription() const { |
| 99 | return "Branch condition evaluates to an uninitialized value."; |
| 100 | } |
| 101 | |
| 102 | virtual void EmitWarnings(BugReporter& BR) { |
| 103 | GRExprEngine& Eng = BR.getEngine(); |
| 104 | GenericEmitWarnings(BR, *this, Eng.undef_branches_begin(), |
| 105 | Eng.undef_branches_end()); |
| 106 | } |
| 107 | }; |
| 108 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 109 | class VISIBILITY_HIDDEN DivZero : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 110 | public: |
| 111 | virtual const char* getName() const { |
| 112 | return "divide-by-zero"; |
| 113 | } |
| 114 | |
| 115 | virtual const char* getDescription() const { |
| 116 | return "Division by zero/undefined value."; |
| 117 | } |
| 118 | |
| 119 | virtual void EmitWarnings(BugReporter& BR) { |
| 120 | GRExprEngine& Eng = BR.getEngine(); |
| 121 | GenericEmitWarnings(BR, *this, Eng.explicit_bad_divides_begin(), |
| 122 | Eng.explicit_bad_divides_end()); |
| 123 | } |
| 124 | }; |
| 125 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 126 | class VISIBILITY_HIDDEN UndefResult : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 127 | public: |
| 128 | virtual const char* getName() const { |
| 129 | return "undefined result"; |
| 130 | } |
| 131 | |
| 132 | virtual const char* getDescription() const { |
| 133 | return "Result of operation is undefined."; |
| 134 | } |
| 135 | |
| 136 | virtual void EmitWarnings(BugReporter& BR) { |
| 137 | GRExprEngine& Eng = BR.getEngine(); |
| 138 | GenericEmitWarnings(BR, *this, Eng.undef_results_begin(), |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 139 | Eng.undef_results_end()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 140 | } |
| 141 | }; |
| 142 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 143 | class VISIBILITY_HIDDEN BadCall : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 144 | public: |
| 145 | virtual const char* getName() const { |
| 146 | return "invalid function call"; |
| 147 | } |
| 148 | |
| 149 | virtual const char* getDescription() const { |
| 150 | return "Called function is a NULL or undefined function pointer value."; |
| 151 | } |
| 152 | |
| 153 | virtual void EmitWarnings(BugReporter& BR) { |
| 154 | GRExprEngine& Eng = BR.getEngine(); |
| 155 | GenericEmitWarnings(BR, *this, Eng.bad_calls_begin(), |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 156 | Eng.bad_calls_end()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 157 | } |
| 158 | }; |
| 159 | |
| 160 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 161 | class VISIBILITY_HIDDEN BadArg : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 162 | public: |
| 163 | |
| 164 | virtual ~BadArg() {} |
| 165 | |
| 166 | virtual const char* getName() const { |
| 167 | return "bad argument"; |
| 168 | } |
| 169 | |
| 170 | virtual const char* getDescription() const { |
| 171 | return "Pass-by-value argument in function is undefined."; |
| 172 | } |
| 173 | |
| 174 | virtual void EmitWarnings(BugReporter& BR) { |
| 175 | GRExprEngine& Eng = BR.getEngine(); |
| 176 | |
| 177 | for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(), |
| 178 | E = Eng.undef_arg_end(); I!=E; ++I) { |
| 179 | |
| 180 | // Generate a report for this bug. |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 181 | RangedBugReport report(*this, I->first); |
| 182 | report.addRange(I->second->getSourceRange()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 183 | |
| 184 | // Emit the warning. |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 185 | BR.EmitWarning(report); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg { |
| 192 | public: |
| 193 | virtual const char* getName() const { |
| 194 | return "bad argument"; |
| 195 | } |
| 196 | |
| 197 | virtual const char* getDescription() const { |
| 198 | return "Pass-by-value argument in message expression is undefined."; |
| 199 | } |
| 200 | |
| 201 | virtual void EmitWarnings(BugReporter& BR) { |
| 202 | GRExprEngine& Eng = BR.getEngine(); |
| 203 | |
| 204 | for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(), |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 205 | E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 206 | |
| 207 | // Generate a report for this bug. |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 208 | RangedBugReport report(*this, I->first); |
| 209 | report.addRange(I->second->getSourceRange()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 210 | |
| 211 | // Emit the warning. |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 212 | BR.EmitWarning(report); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 213 | } |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 214 | } |
| 215 | }; |
| 216 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 217 | class VISIBILITY_HIDDEN BadReceiver : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 218 | public: |
| 219 | virtual const char* getName() const { |
| 220 | return "bad receiver"; |
| 221 | } |
| 222 | |
| 223 | virtual const char* getDescription() const { |
| 224 | return "Receiver in message expression is an uninitialized value."; |
| 225 | } |
| 226 | |
| 227 | virtual void EmitWarnings(BugReporter& BR) { |
| 228 | GRExprEngine& Eng = BR.getEngine(); |
| 229 | |
| 230 | for (GRExprEngine::UndefReceiversTy::iterator I=Eng.undef_receivers_begin(), |
Argyrios Kyrtzidis | afe1091 | 2008-04-15 16:30:10 +0000 | [diff] [blame] | 231 | End = Eng.undef_receivers_end(); I!=End; ++I) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 232 | |
| 233 | // Generate a report for this bug. |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 234 | RangedBugReport report(*this, *I); |
| 235 | |
| 236 | ExplodedNode<ValueState>* N = *I; |
| 237 | Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 238 | Expr* E = cast<ObjCMessageExpr>(S)->getReceiver(); |
| 239 | assert (E && "Receiver cannot be NULL"); |
| 240 | report.addRange(E->getSourceRange()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 241 | |
| 242 | // Emit the warning. |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 243 | BR.EmitWarning(report); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | }; |
| 247 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 248 | class VISIBILITY_HIDDEN RetStack : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 249 | public: |
| 250 | virtual const char* getName() const { |
| 251 | return "return of stack address"; |
| 252 | } |
| 253 | |
| 254 | virtual const char* getDescription() const { |
| 255 | return "Address of stack-allocated variable returned."; |
| 256 | } |
| 257 | |
| 258 | virtual void EmitWarnings(BugReporter& BR) { |
| 259 | GRExprEngine& Eng = BR.getEngine(); |
| 260 | GenericEmitWarnings(BR, *this, Eng.ret_stackaddr_begin(), |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 261 | Eng.ret_stackaddr_end()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 262 | } |
| 263 | }; |
| 264 | |
| 265 | } // end anonymous namespace |
| 266 | |
| 267 | void GRSimpleVals::RegisterChecks(GRExprEngine& Eng) { |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 268 | |
| 269 | // Path-sensitive checks. |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 270 | Eng.Register(new NullDeref()); |
| 271 | Eng.Register(new UndefDeref()); |
| 272 | Eng.Register(new UndefBranch()); |
| 273 | Eng.Register(new DivZero()); |
| 274 | Eng.Register(new UndefResult()); |
| 275 | Eng.Register(new BadCall()); |
| 276 | Eng.Register(new RetStack()); |
| 277 | Eng.Register(new BadArg()); |
| 278 | Eng.Register(new BadMsgExprArg()); |
| 279 | Eng.Register(new BadReceiver()); |
| 280 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 281 | // Flow-sensitive checks. |
| 282 | Eng.Register(MakeDeadStoresChecker()); |
| 283 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 284 | // Add extra checkers. |
| 285 | |
| 286 | GRSimpleAPICheck* FoundationCheck = |
| 287 | CreateBasicObjCFoundationChecks(Eng.getContext(), &Eng.getStateManager()); |
| 288 | |
| 289 | Eng.AddObjCMessageExprCheck(FoundationCheck); |
| 290 | } |
| 291 | |
Ted Kremenek | dd59811 | 2008-04-02 07:05:46 +0000 | [diff] [blame] | 292 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 293 | // Transfer Function creation for External clients. |
Ted Kremenek | 503d613 | 2008-04-02 05:15:22 +0000 | [diff] [blame] | 294 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 296 | GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); } |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 298 | //===----------------------------------------------------------------------===// |
| 299 | // Transfer function for Casts. |
| 300 | //===----------------------------------------------------------------------===// |
| 301 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 302 | RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 304 | if (!isa<nonlval::ConcreteInt>(X)) |
| 305 | return UnknownVal(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 306 | |
| 307 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 308 | |
| 309 | llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue(); |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame^] | 310 | V.setIsUnsigned(T->isUnsignedIntegerType() || IsPointerType(T)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 311 | V.extOrTrunc(Eng.getContext().getTypeSize(T)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame^] | 313 | if (IsPointerType(T)) |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 314 | return lval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 315 | else |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 316 | return nonlval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Casts. |
| 320 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 321 | RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, LVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 322 | |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame^] | 323 | if (IsPointerType(T)) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 324 | return X; |
| 325 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 326 | assert (T->isIntegerType()); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 327 | |
| 328 | if (!isa<lval::ConcreteInt>(X)) |
| 329 | return UnknownVal(); |
| 330 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 331 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 332 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 333 | llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue(); |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame^] | 334 | V.setIsUnsigned(T->isUnsignedIntegerType() || IsPointerType(T)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 335 | V.extOrTrunc(Eng.getContext().getTypeSize(T)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 337 | return nonlval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // Unary operators. |
| 341 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 342 | RVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLVal X){ |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 343 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 344 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 346 | case nonlval::ConcreteIntKind: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 347 | return cast<nonlval::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 349 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 350 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 354 | RVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLVal X) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 355 | |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 356 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 358 | case nonlval::ConcreteIntKind: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 359 | return cast<nonlval::ConcreteInt>(X).EvalComplement(Eng.getBasicVals()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 361 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 362 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 365 | |
| 366 | // Binary operators. |
| 367 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 368 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
| 369 | NonLVal L, NonLVal R) { |
| 370 | |
| 371 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 372 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 373 | while (1) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 375 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 376 | default: |
Ted Kremenek | 9258a64 | 2008-02-21 19:10:12 +0000 | [diff] [blame] | 377 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 378 | |
| 379 | case nonlval::ConcreteIntKind: |
| 380 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 381 | if (isa<nonlval::ConcreteInt>(R)) { |
| 382 | const nonlval::ConcreteInt& L_CI = cast<nonlval::ConcreteInt>(L); |
| 383 | const nonlval::ConcreteInt& R_CI = cast<nonlval::ConcreteInt>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 384 | return L_CI.EvalBinOp(BasicVals, Op, R_CI); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 385 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 386 | else { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 387 | NonLVal tmp = R; |
| 388 | R = L; |
| 389 | L = tmp; |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 390 | continue; |
| 391 | } |
| 392 | |
| 393 | case nonlval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 394 | |
| 395 | if (isa<nonlval::ConcreteInt>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 396 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 397 | BasicVals.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 398 | cast<nonlval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 399 | |
| 400 | return nonlval::SymIntConstraintVal(C); |
| 401 | } |
| 402 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 403 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 410 | // Binary Operators (except assignments and comma). |
| 411 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 412 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 413 | LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 414 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 415 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 416 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 417 | default: |
| 418 | return UnknownVal(); |
| 419 | |
| 420 | case BinaryOperator::EQ: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 421 | return EvalEQ(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 422 | |
| 423 | case BinaryOperator::NE: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 424 | return EvalNE(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 428 | // Pointer arithmetic. |
| 429 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 430 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 431 | LVal L, NonLVal R) { |
| 432 | return UnknownVal(); |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 435 | // Equality operators for LVals. |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 437 | RVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, LVal L, LVal R) { |
| 438 | |
| 439 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 440 | |
| 441 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 443 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 444 | assert(false && "EQ not implemented for this LVal."); |
| 445 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 446 | |
| 447 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 448 | |
| 449 | if (isa<lval::ConcreteInt>(R)) { |
| 450 | bool b = cast<lval::ConcreteInt>(L).getValue() == |
| 451 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 453 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 454 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 455 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 456 | |
| 457 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 458 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 459 | BinaryOperator::EQ, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 460 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 461 | |
| 462 | return nonlval::SymIntConstraintVal(C); |
| 463 | } |
| 464 | |
| 465 | break; |
| 466 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 467 | case lval::SymbolValKind: { |
| 468 | |
| 469 | if (isa<lval::ConcreteInt>(R)) { |
| 470 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 471 | BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 472 | BinaryOperator::EQ, |
| 473 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 474 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 475 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 478 | // FIXME: Implement == for lval Symbols. This is mainly useful |
| 479 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 480 | // Since this is not useful for many checkers we'll punt on this for |
| 481 | // now. |
| 482 | |
| 483 | return UnknownVal(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 484 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | c3b7f0e | 2008-04-30 16:07:22 +0000 | [diff] [blame] | 486 | // FIXME: Different offsets can map to the same memory cell. |
| 487 | case lval::ArrayOffsetKind: |
| 488 | case lval::FieldOffsetKind: |
| 489 | // Fall-through. |
| 490 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 491 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 492 | case lval::FuncValKind: |
| 493 | case lval::GotoLabelKind: |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 494 | return NonLVal::MakeIntTruthVal(BasicVals, L == R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 497 | return NonLVal::MakeIntTruthVal(BasicVals, false); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 500 | RVal GRSimpleVals::EvalNE(GRExprEngine& Eng, LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 501 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 502 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 503 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 504 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 505 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 506 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 507 | assert(false && "NE not implemented for this LVal."); |
| 508 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 509 | |
| 510 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 511 | |
| 512 | if (isa<lval::ConcreteInt>(R)) { |
| 513 | bool b = cast<lval::ConcreteInt>(L).getValue() != |
| 514 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 515 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 516 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 517 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 518 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 519 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 520 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 521 | BinaryOperator::NE, |
| 522 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 523 | |
| 524 | return nonlval::SymIntConstraintVal(C); |
| 525 | } |
| 526 | |
| 527 | break; |
| 528 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 529 | case lval::SymbolValKind: { |
| 530 | if (isa<lval::ConcreteInt>(R)) { |
| 531 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 532 | BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 533 | BinaryOperator::NE, |
| 534 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 535 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 536 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 539 | // FIXME: Implement != for lval Symbols. This is mainly useful |
| 540 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 541 | // Since this is not useful for many checkers we'll punt on this for |
| 542 | // now. |
| 543 | |
| 544 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 545 | |
| 546 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Ted Kremenek | c3b7f0e | 2008-04-30 16:07:22 +0000 | [diff] [blame] | 549 | // FIXME: Different offsets can map to the same memory cell. |
| 550 | case lval::ArrayOffsetKind: |
| 551 | case lval::FieldOffsetKind: |
| 552 | // Fall-through. |
| 553 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 554 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 555 | case lval::FuncValKind: |
| 556 | case lval::GotoLabelKind: |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 557 | return NonLVal::MakeIntTruthVal(BasicVals, L != R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 560 | return NonLVal::MakeIntTruthVal(BasicVals, true); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 561 | } |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 562 | |
| 563 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 564 | // Transfer function for function calls. |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 565 | //===----------------------------------------------------------------------===// |
| 566 | |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 567 | void GRSimpleVals::EvalCall(ExplodedNodeSet<ValueState>& Dst, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 568 | GRExprEngine& Eng, |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 569 | GRStmtNodeBuilder<ValueState>& Builder, |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 570 | CallExpr* CE, RVal L, |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 571 | ExplodedNode<ValueState>* Pred) { |
| 572 | |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 573 | ValueStateManager& StateMgr = Eng.getStateManager(); |
| 574 | ValueState* St = Builder.GetState(Pred); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 575 | |
| 576 | // Invalidate all arguments passed in by reference (LVals). |
| 577 | |
| 578 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 579 | I != E; ++I) { |
| 580 | |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 581 | RVal V = StateMgr.GetRVal(St, *I); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 582 | |
| 583 | if (isa<LVal>(V)) |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 584 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 585 | else if (isa<nonlval::LValAsInteger>(V)) |
| 586 | St = StateMgr.SetRVal(St, cast<nonlval::LValAsInteger>(V).getLVal(), |
| 587 | UnknownVal()); |
| 588 | |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 589 | } |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 590 | |
| 591 | // Make up a symbol for the return value of this function. |
| 592 | |
| 593 | if (CE->getType() != Eng.getContext().VoidTy) { |
| 594 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 595 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count); |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 596 | |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame^] | 597 | RVal X = IsPointerType(CE->getType()) |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 598 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 599 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
| 600 | |
| 601 | St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false); |
| 602 | } |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 603 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 604 | Builder.MakeNode(Dst, CE, Pred, St); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 605 | } |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 606 | |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | // Transfer function for Objective-C message expressions. |
| 609 | //===----------------------------------------------------------------------===// |
| 610 | |
| 611 | void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst, |
| 612 | GRExprEngine& Eng, |
| 613 | GRStmtNodeBuilder<ValueState>& Builder, |
| 614 | ObjCMessageExpr* ME, |
| 615 | ExplodedNode<ValueState>* Pred) { |
| 616 | |
| 617 | |
| 618 | // The basic transfer function logic for message expressions does nothing. |
| 619 | // We just invalidate all arguments passed in by references. |
| 620 | |
| 621 | ValueStateManager& StateMgr = Eng.getStateManager(); |
| 622 | ValueState* St = Builder.GetState(Pred); |
| 623 | |
| 624 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 625 | I != E; ++I) { |
| 626 | |
| 627 | RVal V = StateMgr.GetRVal(St, *I); |
| 628 | |
| 629 | if (isa<LVal>(V)) |
| 630 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 631 | } |
| 632 | |
| 633 | Builder.MakeNode(Dst, ME, Pred, St); |
| 634 | } |