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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 69 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 86 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 116 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 133 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 150 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 171 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 198 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 224 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 255 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 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; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 268 | const ValueState* St; |
Ted Kremenek | 5c454ab | 2008-05-05 15:56:53 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 270 | FindUndefExpr(ValueStateManager& V, const ValueState* S) : VM(V), St(S) {} |
Ted Kremenek | 5c454ab | 2008-05-05 15:56:53 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 294 | GRExprEngine& Eng = cast<GRBugReporter>(BR).getEngine(); |
Ted Kremenek | 5c454ab | 2008-05-05 15:56:53 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 322 | const ValueState* St = (*I)->getState(); |
Ted Kremenek | 5c454ab | 2008-05-05 15:56:53 +0000 | [diff] [blame] | 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 | |
| 353 | // Add extra checkers. |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 354 | ASTContext& Ctx = Eng.getContext(); |
| 355 | ValueStateManager* VMgr = &Eng.getStateManager(); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 357 | GRSimpleAPICheck* Check = CreateBasicObjCFoundationChecks(Ctx, VMgr); |
| 358 | Eng.AddCheck(Check, Stmt::ObjCMessageExprClass); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 360 | Check = CreateAuditCFNumberCreate(Ctx, VMgr); |
| 361 | Eng.AddCheck(Check, Stmt::CallExprClass); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Ted Kremenek | dd59811 | 2008-04-02 07:05:46 +0000 | [diff] [blame] | 365 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 366 | // Transfer Function creation for External clients. |
Ted Kremenek | 503d613 | 2008-04-02 05:15:22 +0000 | [diff] [blame] | 367 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 369 | GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); } |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 371 | //===----------------------------------------------------------------------===// |
| 372 | // Transfer function for Casts. |
| 373 | //===----------------------------------------------------------------------===// |
| 374 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 375 | RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 377 | if (!isa<nonlval::ConcreteInt>(X)) |
| 378 | return UnknownVal(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | cd512dc | 2008-07-16 00:23:49 +0000 | [diff] [blame] | 380 | bool isLValType = LVal::IsLValType(T); |
| 381 | |
Ted Kremenek | f496ee1 | 2008-07-15 23:17:54 +0000 | [diff] [blame] | 382 | // Only handle casts from integers to integers. |
Ted Kremenek | cd512dc | 2008-07-16 00:23:49 +0000 | [diff] [blame] | 383 | if (!isLValType && !T->isIntegerType()) |
Ted Kremenek | f496ee1 | 2008-07-15 23:17:54 +0000 | [diff] [blame] | 384 | return UnknownVal(); |
| 385 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 386 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 387 | |
| 388 | llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue(); |
Ted Kremenek | 0e470a5 | 2008-05-09 23:45:33 +0000 | [diff] [blame] | 389 | V.setIsUnsigned(T->isUnsignedIntegerType() || LVal::IsLValType(T)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 390 | V.extOrTrunc(Eng.getContext().getTypeSize(T)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | cd512dc | 2008-07-16 00:23:49 +0000 | [diff] [blame] | 392 | if (isLValType) |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 393 | return lval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 394 | else |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 395 | return nonlval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // Casts. |
| 399 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 400 | RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, LVal X, QualType T) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | e8c2bde | 2008-04-30 21:10:19 +0000 | [diff] [blame] | 402 | // Casts from pointers -> pointers, just return the lval. |
| 403 | // |
| 404 | // Casts from pointers -> references, just return the lval. These |
| 405 | // can be introduced by the frontend for corner cases, e.g |
| 406 | // casting from va_list* to __builtin_va_list&. |
| 407 | // |
Ted Kremenek | 0e470a5 | 2008-05-09 23:45:33 +0000 | [diff] [blame] | 408 | if (LVal::IsLValType(T) || T->isReferenceType()) |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 409 | return X; |
| 410 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 411 | assert (T->isIntegerType()); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 412 | |
| 413 | if (!isa<lval::ConcreteInt>(X)) |
| 414 | return UnknownVal(); |
| 415 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 416 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 417 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 418 | llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue(); |
Ted Kremenek | 0e470a5 | 2008-05-09 23:45:33 +0000 | [diff] [blame] | 419 | V.setIsUnsigned(T->isUnsignedIntegerType() || LVal::IsLValType(T)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 420 | V.extOrTrunc(Eng.getContext().getTypeSize(T)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 422 | return nonlval::ConcreteInt(BasicVals.getValue(V)); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | // Unary operators. |
| 426 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 427 | RVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLVal X){ |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 428 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 429 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 430 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 431 | case nonlval::ConcreteIntKind: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 432 | return cast<nonlval::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 434 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 435 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 439 | RVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLVal X) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 441 | switch (X.getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 443 | case nonlval::ConcreteIntKind: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 444 | return cast<nonlval::ConcreteInt>(X).EvalComplement(Eng.getBasicVals()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 445 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 446 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 447 | return UnknownVal(); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 450 | |
| 451 | // Binary operators. |
| 452 | |
Ted Kremenek | ad8329e | 2008-07-18 15:27:58 +0000 | [diff] [blame^] | 453 | RVal GRSimpleVals::DetermEvalBinOpNN(ValueStateManager& StateMgr, |
| 454 | BinaryOperator::Opcode Op, |
| 455 | NonLVal L, NonLVal R) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 456 | |
Ted Kremenek | 6297a8e | 2008-07-18 05:53:58 +0000 | [diff] [blame] | 457 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 458 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 459 | while (1) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 460 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 461 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 462 | default: |
Ted Kremenek | 9258a64 | 2008-02-21 19:10:12 +0000 | [diff] [blame] | 463 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 464 | |
| 465 | case nonlval::ConcreteIntKind: |
| 466 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 467 | if (isa<nonlval::ConcreteInt>(R)) { |
| 468 | const nonlval::ConcreteInt& L_CI = cast<nonlval::ConcreteInt>(L); |
| 469 | const nonlval::ConcreteInt& R_CI = cast<nonlval::ConcreteInt>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 470 | return L_CI.EvalBinOp(BasicVals, Op, R_CI); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 471 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 472 | else { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 473 | NonLVal tmp = R; |
| 474 | R = L; |
| 475 | L = tmp; |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 476 | continue; |
| 477 | } |
| 478 | |
| 479 | case nonlval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 480 | |
| 481 | if (isa<nonlval::ConcreteInt>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 482 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 483 | BasicVals.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 484 | cast<nonlval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 485 | |
| 486 | return nonlval::SymIntConstraintVal(C); |
| 487 | } |
| 488 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 489 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 495 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 496 | // Binary Operators (except assignments and comma). |
| 497 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 498 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 499 | LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 500 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 501 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 503 | default: |
| 504 | return UnknownVal(); |
| 505 | |
| 506 | case BinaryOperator::EQ: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 507 | return EvalEQ(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 508 | |
| 509 | case BinaryOperator::NE: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 510 | return EvalNE(Eng, L, R); |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 514 | // Pointer arithmetic. |
| 515 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 516 | RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 517 | LVal L, NonLVal R) { |
| 518 | return UnknownVal(); |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 521 | // Equality operators for LVals. |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 523 | RVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, LVal L, LVal R) { |
| 524 | |
| 525 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 526 | |
| 527 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 528 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 529 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 530 | assert(false && "EQ not implemented for this LVal."); |
| 531 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 532 | |
| 533 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 534 | |
| 535 | if (isa<lval::ConcreteInt>(R)) { |
| 536 | bool b = cast<lval::ConcreteInt>(L).getValue() == |
| 537 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 538 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 539 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 540 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 541 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 542 | |
| 543 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 544 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 545 | BinaryOperator::EQ, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 546 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 547 | |
| 548 | return nonlval::SymIntConstraintVal(C); |
| 549 | } |
| 550 | |
| 551 | break; |
| 552 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 553 | case lval::SymbolValKind: { |
| 554 | |
| 555 | if (isa<lval::ConcreteInt>(R)) { |
| 556 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 557 | BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 558 | BinaryOperator::EQ, |
| 559 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 560 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 561 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 564 | // FIXME: Implement == for lval Symbols. This is mainly useful |
| 565 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 566 | // Since this is not useful for many checkers we'll punt on this for |
| 567 | // now. |
| 568 | |
| 569 | return UnknownVal(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 570 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 571 | |
Ted Kremenek | c3b7f0e | 2008-04-30 16:07:22 +0000 | [diff] [blame] | 572 | // FIXME: Different offsets can map to the same memory cell. |
| 573 | case lval::ArrayOffsetKind: |
| 574 | case lval::FieldOffsetKind: |
| 575 | // Fall-through. |
| 576 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 577 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 578 | case lval::FuncValKind: |
| 579 | case lval::GotoLabelKind: |
Ted Kremenek | 666de3b | 2008-05-12 17:41:30 +0000 | [diff] [blame] | 580 | case lval::StringLiteralValKind: |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 581 | return NonLVal::MakeIntTruthVal(BasicVals, L == R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 584 | return NonLVal::MakeIntTruthVal(BasicVals, false); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 587 | RVal GRSimpleVals::EvalNE(GRExprEngine& Eng, LVal L, LVal R) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 588 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 589 | BasicValueFactory& BasicVals = Eng.getBasicVals(); |
| 590 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 591 | switch (L.getSubKind()) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 592 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 593 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 594 | assert(false && "NE not implemented for this LVal."); |
| 595 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 596 | |
| 597 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 598 | |
| 599 | if (isa<lval::ConcreteInt>(R)) { |
| 600 | bool b = cast<lval::ConcreteInt>(L).getValue() != |
| 601 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 602 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 603 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 604 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 605 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 606 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 607 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 608 | BinaryOperator::NE, |
| 609 | cast<lval::ConcreteInt>(L).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 610 | |
| 611 | return nonlval::SymIntConstraintVal(C); |
| 612 | } |
| 613 | |
| 614 | break; |
| 615 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 616 | case lval::SymbolValKind: { |
| 617 | if (isa<lval::ConcreteInt>(R)) { |
| 618 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 619 | BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(), |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 620 | BinaryOperator::NE, |
| 621 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 622 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 623 | return nonlval::SymIntConstraintVal(C); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Ted Kremenek | f700df2 | 2008-02-22 18:41:59 +0000 | [diff] [blame] | 626 | // FIXME: Implement != for lval Symbols. This is mainly useful |
| 627 | // in iterator loops when traversing a buffer, e.g. while(z != zTerm). |
| 628 | // Since this is not useful for many checkers we'll punt on this for |
| 629 | // now. |
| 630 | |
| 631 | return UnknownVal(); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 632 | |
| 633 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Ted Kremenek | c3b7f0e | 2008-04-30 16:07:22 +0000 | [diff] [blame] | 636 | // FIXME: Different offsets can map to the same memory cell. |
| 637 | case lval::ArrayOffsetKind: |
| 638 | case lval::FieldOffsetKind: |
| 639 | // Fall-through. |
| 640 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 641 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 642 | case lval::FuncValKind: |
| 643 | case lval::GotoLabelKind: |
Ted Kremenek | 666de3b | 2008-05-12 17:41:30 +0000 | [diff] [blame] | 644 | case lval::StringLiteralValKind: |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 645 | return NonLVal::MakeIntTruthVal(BasicVals, L != R); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 648 | return NonLVal::MakeIntTruthVal(BasicVals, true); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 649 | } |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 650 | |
| 651 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 652 | // Transfer function for function calls. |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 653 | //===----------------------------------------------------------------------===// |
| 654 | |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 655 | void GRSimpleVals::EvalCall(ExplodedNodeSet<ValueState>& Dst, |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 656 | GRExprEngine& Eng, |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 657 | GRStmtNodeBuilder<ValueState>& Builder, |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 658 | CallExpr* CE, RVal L, |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 659 | ExplodedNode<ValueState>* Pred) { |
| 660 | |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 661 | ValueStateManager& StateMgr = Eng.getStateManager(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 662 | const ValueState* St = Builder.GetState(Pred); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 663 | |
| 664 | // Invalidate all arguments passed in by reference (LVals). |
| 665 | |
| 666 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 667 | I != E; ++I) { |
| 668 | |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 669 | RVal V = StateMgr.GetRVal(St, *I); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 670 | |
| 671 | if (isa<LVal>(V)) |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 672 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 673 | else if (isa<nonlval::LValAsInteger>(V)) |
| 674 | St = StateMgr.SetRVal(St, cast<nonlval::LValAsInteger>(V).getLVal(), |
| 675 | UnknownVal()); |
| 676 | |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 677 | } |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 678 | |
| 679 | // Make up a symbol for the return value of this function. |
| 680 | |
| 681 | if (CE->getType() != Eng.getContext().VoidTy) { |
| 682 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 683 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count); |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 684 | |
Ted Kremenek | 0e470a5 | 2008-05-09 23:45:33 +0000 | [diff] [blame] | 685 | RVal X = LVal::IsLValType(CE->getType()) |
Ted Kremenek | f923a91 | 2008-03-12 21:04:07 +0000 | [diff] [blame] | 686 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 687 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
| 688 | |
| 689 | St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false); |
| 690 | } |
Ted Kremenek | 330dddd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 691 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 692 | Builder.MakeNode(Dst, CE, Pred, St); |
Ted Kremenek | 0674769 | 2008-02-26 23:04:29 +0000 | [diff] [blame] | 693 | } |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 694 | |
| 695 | //===----------------------------------------------------------------------===// |
| 696 | // Transfer function for Objective-C message expressions. |
| 697 | //===----------------------------------------------------------------------===// |
| 698 | |
| 699 | void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst, |
| 700 | GRExprEngine& Eng, |
| 701 | GRStmtNodeBuilder<ValueState>& Builder, |
| 702 | ObjCMessageExpr* ME, |
| 703 | ExplodedNode<ValueState>* Pred) { |
| 704 | |
| 705 | |
| 706 | // The basic transfer function logic for message expressions does nothing. |
| 707 | // We just invalidate all arguments passed in by references. |
| 708 | |
| 709 | ValueStateManager& StateMgr = Eng.getStateManager(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 710 | const ValueState* St = Builder.GetState(Pred); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 711 | |
| 712 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 713 | I != E; ++I) { |
| 714 | |
| 715 | RVal V = StateMgr.GetRVal(St, *I); |
| 716 | |
| 717 | if (isa<LVal>(V)) |
| 718 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 719 | } |
| 720 | |
| 721 | Builder.MakeNode(Dst, ME, Pred, St); |
| 722 | } |