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 | |
Ted Kremenek | 5c454ab | 2008-05-05 15:56:53 +0000 | [diff] [blame^] | 102 | virtual void EmitWarnings(BugReporter& BR); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 105 | class VISIBILITY_HIDDEN DivZero : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 106 | public: |
| 107 | virtual const char* getName() const { |
| 108 | return "divide-by-zero"; |
| 109 | } |
| 110 | |
| 111 | virtual const char* getDescription() const { |
| 112 | return "Division by zero/undefined value."; |
| 113 | } |
| 114 | |
| 115 | virtual void EmitWarnings(BugReporter& BR) { |
| 116 | GRExprEngine& Eng = BR.getEngine(); |
| 117 | GenericEmitWarnings(BR, *this, Eng.explicit_bad_divides_begin(), |
| 118 | Eng.explicit_bad_divides_end()); |
| 119 | } |
| 120 | }; |
| 121 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 122 | class VISIBILITY_HIDDEN UndefResult : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 123 | public: |
| 124 | virtual const char* getName() const { |
| 125 | return "undefined result"; |
| 126 | } |
| 127 | |
| 128 | virtual const char* getDescription() const { |
| 129 | return "Result of operation is undefined."; |
| 130 | } |
| 131 | |
| 132 | virtual void EmitWarnings(BugReporter& BR) { |
| 133 | GRExprEngine& Eng = BR.getEngine(); |
| 134 | GenericEmitWarnings(BR, *this, Eng.undef_results_begin(), |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 135 | Eng.undef_results_end()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 136 | } |
| 137 | }; |
| 138 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 139 | class VISIBILITY_HIDDEN BadCall : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 140 | public: |
| 141 | virtual const char* getName() const { |
| 142 | return "invalid function call"; |
| 143 | } |
| 144 | |
| 145 | virtual const char* getDescription() const { |
| 146 | return "Called function is a NULL or undefined function pointer value."; |
| 147 | } |
| 148 | |
| 149 | virtual void EmitWarnings(BugReporter& BR) { |
| 150 | GRExprEngine& Eng = BR.getEngine(); |
| 151 | GenericEmitWarnings(BR, *this, Eng.bad_calls_begin(), |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 152 | Eng.bad_calls_end()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 153 | } |
| 154 | }; |
| 155 | |
| 156 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 157 | class VISIBILITY_HIDDEN BadArg : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 158 | public: |
| 159 | |
| 160 | virtual ~BadArg() {} |
| 161 | |
| 162 | virtual const char* getName() const { |
| 163 | return "bad argument"; |
| 164 | } |
| 165 | |
| 166 | virtual const char* getDescription() const { |
| 167 | return "Pass-by-value argument in function is undefined."; |
| 168 | } |
| 169 | |
| 170 | virtual void EmitWarnings(BugReporter& BR) { |
| 171 | GRExprEngine& Eng = BR.getEngine(); |
| 172 | |
| 173 | for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(), |
| 174 | E = Eng.undef_arg_end(); I!=E; ++I) { |
| 175 | |
| 176 | // Generate a report for this bug. |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 177 | RangedBugReport report(*this, I->first); |
| 178 | report.addRange(I->second->getSourceRange()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 179 | |
| 180 | // Emit the warning. |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 181 | BR.EmitWarning(report); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg { |
| 188 | public: |
| 189 | virtual const char* getName() const { |
| 190 | return "bad argument"; |
| 191 | } |
| 192 | |
| 193 | virtual const char* getDescription() const { |
| 194 | return "Pass-by-value argument in message expression is undefined."; |
| 195 | } |
| 196 | |
| 197 | virtual void EmitWarnings(BugReporter& BR) { |
| 198 | GRExprEngine& Eng = BR.getEngine(); |
| 199 | |
| 200 | for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(), |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 201 | E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 202 | |
| 203 | // Generate a report for this bug. |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 204 | RangedBugReport report(*this, I->first); |
| 205 | report.addRange(I->second->getSourceRange()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 206 | |
| 207 | // Emit the warning. |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 208 | BR.EmitWarning(report); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 209 | } |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 210 | } |
| 211 | }; |
| 212 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 213 | class VISIBILITY_HIDDEN BadReceiver : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 214 | public: |
| 215 | virtual const char* getName() const { |
| 216 | return "bad receiver"; |
| 217 | } |
| 218 | |
| 219 | virtual const char* getDescription() const { |
| 220 | return "Receiver in message expression is an uninitialized value."; |
| 221 | } |
| 222 | |
| 223 | virtual void EmitWarnings(BugReporter& BR) { |
| 224 | GRExprEngine& Eng = BR.getEngine(); |
| 225 | |
| 226 | for (GRExprEngine::UndefReceiversTy::iterator I=Eng.undef_receivers_begin(), |
Argyrios Kyrtzidis | afe1091 | 2008-04-15 16:30:10 +0000 | [diff] [blame] | 227 | End = Eng.undef_receivers_end(); I!=End; ++I) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 228 | |
| 229 | // Generate a report for this bug. |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 230 | RangedBugReport report(*this, *I); |
| 231 | |
| 232 | ExplodedNode<ValueState>* N = *I; |
| 233 | Stmt *S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 234 | Expr* E = cast<ObjCMessageExpr>(S)->getReceiver(); |
| 235 | assert (E && "Receiver cannot be NULL"); |
| 236 | report.addRange(E->getSourceRange()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 237 | |
| 238 | // Emit the warning. |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 239 | BR.EmitWarning(report); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | }; |
| 243 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 244 | class VISIBILITY_HIDDEN RetStack : public BugTypeCacheLocation { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 245 | public: |
| 246 | virtual const char* getName() const { |
| 247 | return "return of stack address"; |
| 248 | } |
| 249 | |
| 250 | virtual const char* getDescription() const { |
| 251 | return "Address of stack-allocated variable returned."; |
| 252 | } |
| 253 | |
| 254 | virtual void EmitWarnings(BugReporter& BR) { |
| 255 | GRExprEngine& Eng = BR.getEngine(); |
| 256 | GenericEmitWarnings(BR, *this, Eng.ret_stackaddr_begin(), |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 257 | Eng.ret_stackaddr_end()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 258 | } |
| 259 | }; |
| 260 | |
| 261 | } // end anonymous namespace |
| 262 | |
Ted Kremenek | 5c454ab | 2008-05-05 15:56:53 +0000 | [diff] [blame^] | 263 | |
| 264 | namespace { |
| 265 | |
| 266 | struct VISIBILITY_HIDDEN FindUndefExpr { |
| 267 | ValueStateManager& VM; |
| 268 | ValueState* St; |
| 269 | |
| 270 | FindUndefExpr(ValueStateManager& V, ValueState* S) : VM(V), St(S) {} |
| 271 | |
| 272 | Expr* FindExpr(Expr* Ex) { |
| 273 | |
| 274 | if (!MatchesCriteria(Ex)) |
| 275 | return 0; |
| 276 | |
| 277 | for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end(); I!=E; ++I) |
| 278 | if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) { |
| 279 | Expr* E2 = FindExpr(ExI); |
| 280 | if (E2) return E2; |
| 281 | } |
| 282 | |
| 283 | return Ex; |
| 284 | } |
| 285 | |
| 286 | bool MatchesCriteria(Expr* Ex) { return VM.GetRVal(St, Ex).isUndef(); } |
| 287 | }; |
| 288 | |
| 289 | } // end anonymous namespace |
| 290 | |
| 291 | |
| 292 | void UndefBranch::EmitWarnings(BugReporter& BR) { |
| 293 | |
| 294 | GRExprEngine& Eng = BR.getEngine(); |
| 295 | |
| 296 | for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(), |
| 297 | E=Eng.undef_branches_end(); I!=E; ++I) { |
| 298 | |
| 299 | // What's going on here: we want to highlight the subexpression of the |
| 300 | // condition that is the most likely source of the "uninitialized |
| 301 | // branch condition." We do a recursive walk of the condition's |
| 302 | // subexpressions and roughly look for the most nested subexpression |
| 303 | // that binds to Undefined. We then highlight that expression's range. |
| 304 | |
| 305 | BlockEdge B = cast<BlockEdge>((*I)->getLocation()); |
| 306 | Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition()); |
| 307 | assert (Ex && "Block must have a terminator."); |
| 308 | |
| 309 | // Get the predecessor node and check if is a PostStmt with the Stmt |
| 310 | // being the terminator condition. We want to inspect the state |
| 311 | // of that node instead because it will contain main information about |
| 312 | // the subexpressions. |
| 313 | |
| 314 | assert (!(*I)->pred_empty()); |
| 315 | |
| 316 | // Note: any predecessor will do. They should have identical state, |
| 317 | // since all the BlockEdge did was act as an error sink since the value |
| 318 | // had to already be undefined. |
| 319 | ExplodedNode<ValueState> *N = *(*I)->pred_begin(); |
| 320 | ProgramPoint P = N->getLocation(); |
| 321 | |
| 322 | ValueState* St = (*I)->getState(); |
| 323 | |
| 324 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 325 | if (PS->getStmt() == Ex) |
| 326 | St = N->getState(); |
| 327 | |
| 328 | FindUndefExpr FindIt(Eng.getStateManager(), St); |
| 329 | Ex = FindIt.FindExpr(Ex); |
| 330 | |
| 331 | RangedBugReport R(*this, *I); |
| 332 | R.addRange(Ex->getSourceRange()); |
| 333 | |
| 334 | BR.EmitWarning(R); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 339 | void GRSimpleVals::RegisterChecks(GRExprEngine& Eng) { |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 340 | |
| 341 | // Path-sensitive checks. |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 342 | Eng.Register(new NullDeref()); |
| 343 | Eng.Register(new UndefDeref()); |
| 344 | Eng.Register(new UndefBranch()); |
| 345 | Eng.Register(new DivZero()); |
| 346 | Eng.Register(new UndefResult()); |
| 347 | Eng.Register(new BadCall()); |
| 348 | Eng.Register(new RetStack()); |
| 349 | Eng.Register(new BadArg()); |
| 350 | Eng.Register(new BadMsgExprArg()); |
| 351 | Eng.Register(new BadReceiver()); |
| 352 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 353 | // Flow-sensitive checks. |
| 354 | Eng.Register(MakeDeadStoresChecker()); |
| 355 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 356 | // Add extra checkers. |
| 357 | |
| 358 | GRSimpleAPICheck* FoundationCheck = |
| 359 | CreateBasicObjCFoundationChecks(Eng.getContext(), &Eng.getStateManager()); |
| 360 | |
| 361 | Eng.AddObjCMessageExprCheck(FoundationCheck); |
| 362 | } |
| 363 | |
Ted Kremenek | dd59811 | 2008-04-02 07:05:46 +0000 | [diff] [blame] | 364 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 365 | // Transfer Function creation for External clients. |
Ted Kremenek | 503d613 | 2008-04-02 05:15:22 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 368 | GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); } |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 370 | //===----------------------------------------------------------------------===// |
| 371 | // Transfer function for Casts. |
| 372 | //===----------------------------------------------------------------------===// |
| 373 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 374 | RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 375 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 376 | if (!isa<nonlval::ConcreteInt>(X)) |
| 377 | return UnknownVal(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 378 | |
| 379 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 380 | |
| 381 | llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue(); |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame] | 382 | V.setIsUnsigned(T->isUnsignedIntegerType() || IsPointerType(T)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 383 | V.extOrTrunc(Eng.getContext().getTypeSize(T)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 384 | |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame] | 385 | if (IsPointerType(T)) |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 386 | return lval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 387 | else |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 388 | return nonlval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // Casts. |
| 392 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 393 | RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, LVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | e8c2bde | 2008-04-30 21:10:19 +0000 | [diff] [blame] | 395 | // Casts from pointers -> pointers, just return the lval. |
| 396 | // |
| 397 | // Casts from pointers -> references, just return the lval. These |
| 398 | // can be introduced by the frontend for corner cases, e.g |
| 399 | // casting from va_list* to __builtin_va_list&. |
| 400 | // |
| 401 | if (IsPointerType(T) || T->isReferenceType()) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 402 | return X; |
| 403 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 404 | assert (T->isIntegerType()); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 405 | |
| 406 | if (!isa<lval::ConcreteInt>(X)) |
| 407 | return UnknownVal(); |
| 408 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 409 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 410 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 411 | llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue(); |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame] | 412 | V.setIsUnsigned(T->isUnsignedIntegerType() || IsPointerType(T)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 413 | V.extOrTrunc(Eng.getContext().getTypeSize(T)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 414 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 415 | return nonlval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Unary operators. |
| 419 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 420 | RVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLVal X){ |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 422 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 423 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 424 | case nonlval::ConcreteIntKind: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 425 | return cast<nonlval::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 426 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 427 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 428 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 432 | RVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLVal X) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 434 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 436 | case nonlval::ConcreteIntKind: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 437 | return cast<nonlval::ConcreteInt>(X).EvalComplement(Eng.getBasicVals()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 439 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 440 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 443 | |
| 444 | // Binary operators. |
| 445 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 446 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
| 447 | NonLVal L, NonLVal R) { |
| 448 | |
| 449 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 450 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 451 | while (1) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 453 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 454 | default: |
Ted Kremenek | 9258a64 | 2008-02-21 19:10:12 +0000 | [diff] [blame] | 455 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 456 | |
| 457 | case nonlval::ConcreteIntKind: |
| 458 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 459 | if (isa<nonlval::ConcreteInt>(R)) { |
| 460 | const nonlval::ConcreteInt& L_CI = cast<nonlval::ConcreteInt>(L); |
| 461 | const nonlval::ConcreteInt& R_CI = cast<nonlval::ConcreteInt>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 462 | return L_CI.EvalBinOp(BasicVals, Op, R_CI); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 463 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 464 | else { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 465 | NonLVal tmp = R; |
| 466 | R = L; |
| 467 | L = tmp; |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 468 | continue; |
| 469 | } |
| 470 | |
| 471 | case nonlval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 472 | |
| 473 | if (isa<nonlval::ConcreteInt>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 474 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 475 | BasicVals.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 476 | cast<nonlval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 477 | |
| 478 | return nonlval::SymIntConstraintVal(C); |
| 479 | } |
| 480 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 481 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 487 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 488 | // Binary Operators (except assignments and comma). |
| 489 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 490 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 491 | LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 492 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 493 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 494 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 495 | default: |
| 496 | return UnknownVal(); |
| 497 | |
| 498 | case BinaryOperator::EQ: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 499 | return EvalEQ(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 500 | |
| 501 | case BinaryOperator::NE: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 502 | return EvalNE(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 506 | // Pointer arithmetic. |
| 507 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 508 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 509 | LVal L, NonLVal R) { |
| 510 | return UnknownVal(); |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 513 | // Equality operators for LVals. |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 515 | RVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, LVal L, LVal R) { |
| 516 | |
| 517 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 518 | |
| 519 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 520 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 521 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 522 | assert(false && "EQ not implemented for this LVal."); |
| 523 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 524 | |
| 525 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 526 | |
| 527 | if (isa<lval::ConcreteInt>(R)) { |
| 528 | bool b = cast<lval::ConcreteInt>(L).getValue() == |
| 529 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 530 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 531 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 532 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 533 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 534 | |
| 535 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 536 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 537 | BinaryOperator::EQ, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 538 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 539 | |
| 540 | return nonlval::SymIntConstraintVal(C); |
| 541 | } |
| 542 | |
| 543 | break; |
| 544 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 545 | case lval::SymbolValKind: { |
| 546 | |
| 547 | if (isa<lval::ConcreteInt>(R)) { |
| 548 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 549 | BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 550 | BinaryOperator::EQ, |
| 551 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 552 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 553 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 556 | // FIXME: Implement == for lval Symbols. This is mainly useful |
| 557 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 558 | // Since this is not useful for many checkers we'll punt on this for |
| 559 | // now. |
| 560 | |
| 561 | return UnknownVal(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 562 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 563 | |
Ted Kremenek | c3b7f0e | 2008-04-30 16:07:22 +0000 | [diff] [blame] | 564 | // FIXME: Different offsets can map to the same memory cell. |
| 565 | case lval::ArrayOffsetKind: |
| 566 | case lval::FieldOffsetKind: |
| 567 | // Fall-through. |
| 568 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 569 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 570 | case lval::FuncValKind: |
| 571 | case lval::GotoLabelKind: |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 572 | return NonLVal::MakeIntTruthVal(BasicVals, L == R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 575 | return NonLVal::MakeIntTruthVal(BasicVals, false); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 578 | RVal GRSimpleVals::EvalNE(GRExprEngine& Eng, LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 579 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 580 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 581 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 582 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 583 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 584 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 585 | assert(false && "NE not implemented for this LVal."); |
| 586 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 587 | |
| 588 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 589 | |
| 590 | if (isa<lval::ConcreteInt>(R)) { |
| 591 | bool b = cast<lval::ConcreteInt>(L).getValue() != |
| 592 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 593 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 594 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 595 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 596 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 597 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 598 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 599 | BinaryOperator::NE, |
| 600 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 601 | |
| 602 | return nonlval::SymIntConstraintVal(C); |
| 603 | } |
| 604 | |
| 605 | break; |
| 606 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 607 | case lval::SymbolValKind: { |
| 608 | if (isa<lval::ConcreteInt>(R)) { |
| 609 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 610 | BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 611 | BinaryOperator::NE, |
| 612 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 613 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 614 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 617 | // FIXME: Implement != for lval Symbols. This is mainly useful |
| 618 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 619 | // Since this is not useful for many checkers we'll punt on this for |
| 620 | // now. |
| 621 | |
| 622 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 623 | |
| 624 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Ted Kremenek | c3b7f0e | 2008-04-30 16:07:22 +0000 | [diff] [blame] | 627 | // FIXME: Different offsets can map to the same memory cell. |
| 628 | case lval::ArrayOffsetKind: |
| 629 | case lval::FieldOffsetKind: |
| 630 | // Fall-through. |
| 631 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 632 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 633 | case lval::FuncValKind: |
| 634 | case lval::GotoLabelKind: |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 635 | return NonLVal::MakeIntTruthVal(BasicVals, L != R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 638 | return NonLVal::MakeIntTruthVal(BasicVals, true); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 639 | } |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 640 | |
| 641 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 642 | // Transfer function for function calls. |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 643 | //===----------------------------------------------------------------------===// |
| 644 | |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 645 | void GRSimpleVals::EvalCall(ExplodedNodeSet<ValueState>& Dst, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 646 | GRExprEngine& Eng, |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 647 | GRStmtNodeBuilder<ValueState>& Builder, |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 648 | CallExpr* CE, RVal L, |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 649 | ExplodedNode<ValueState>* Pred) { |
| 650 | |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 651 | ValueStateManager& StateMgr = Eng.getStateManager(); |
| 652 | ValueState* St = Builder.GetState(Pred); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 653 | |
| 654 | // Invalidate all arguments passed in by reference (LVals). |
| 655 | |
| 656 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 657 | I != E; ++I) { |
| 658 | |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 659 | RVal V = StateMgr.GetRVal(St, *I); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 660 | |
| 661 | if (isa<LVal>(V)) |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 662 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 663 | else if (isa<nonlval::LValAsInteger>(V)) |
| 664 | St = StateMgr.SetRVal(St, cast<nonlval::LValAsInteger>(V).getLVal(), |
| 665 | UnknownVal()); |
| 666 | |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 667 | } |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 668 | |
| 669 | // Make up a symbol for the return value of this function. |
| 670 | |
| 671 | if (CE->getType() != Eng.getContext().VoidTy) { |
| 672 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 673 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count); |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 674 | |
Ted Kremenek | c0c3f5d | 2008-04-30 20:17:27 +0000 | [diff] [blame] | 675 | RVal X = IsPointerType(CE->getType()) |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 676 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 677 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
| 678 | |
| 679 | St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false); |
| 680 | } |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 681 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 682 | Builder.MakeNode(Dst, CE, Pred, St); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 683 | } |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 684 | |
| 685 | //===----------------------------------------------------------------------===// |
| 686 | // Transfer function for Objective-C message expressions. |
| 687 | //===----------------------------------------------------------------------===// |
| 688 | |
| 689 | void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst, |
| 690 | GRExprEngine& Eng, |
| 691 | GRStmtNodeBuilder<ValueState>& Builder, |
| 692 | ObjCMessageExpr* ME, |
| 693 | ExplodedNode<ValueState>* Pred) { |
| 694 | |
| 695 | |
| 696 | // The basic transfer function logic for message expressions does nothing. |
| 697 | // We just invalidate all arguments passed in by references. |
| 698 | |
| 699 | ValueStateManager& StateMgr = Eng.getStateManager(); |
| 700 | ValueState* St = Builder.GetState(Pred); |
| 701 | |
| 702 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 703 | I != E; ++I) { |
| 704 | |
| 705 | RVal V = StateMgr.GetRVal(St, *I); |
| 706 | |
| 707 | if (isa<LVal>(V)) |
| 708 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 709 | } |
| 710 | |
| 711 | Builder.MakeNode(Dst, ME, Pred, St); |
| 712 | } |