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