Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1 | // BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- 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 | // |
| 10 | // This file defines BugReporter, a utility class for generating |
| 11 | // PathDiagnostics for analyses based on GRSimpleVals. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Basic/SourceLocation.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/CFG.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/Analysis/ProgramPoint.h" |
| 23 | #include "clang/Analysis/PathDiagnostic.h" |
| 24 | #include <sstream> |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | BugReporter::~BugReporter() {} |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 29 | BugType::~BugType() {} |
| 30 | BugReport::~BugReport() {} |
| 31 | ExplodedGraph<ValueState>& BugReporter::getGraph() { return Eng.getGraph(); } |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 32 | |
| 33 | static inline Stmt* GetStmt(const ProgramPoint& P) { |
| 34 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 35 | return PS->getStmt(); |
| 36 | } |
| 37 | else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 38 | return BE->getSrc()->getTerminator(); |
| 39 | } |
| 40 | else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) { |
| 41 | return BE->getFirstStmt(); |
| 42 | } |
| 43 | |
| 44 | assert (false && "Unsupported ProgramPoint."); |
| 45 | return NULL; |
| 46 | } |
| 47 | |
Ted Kremenek | b121764 | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 48 | static inline Stmt* GetStmt(const CFGBlock* B) { |
| 49 | assert (!B->empty()); |
| 50 | return (*B)[0]; |
| 51 | } |
| 52 | |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 53 | |
| 54 | PathDiagnosticPiece* |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 55 | BugReport::getEndPath(ASTContext& Ctx, ExplodedNode<ValueState> *N) const { |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 56 | |
| 57 | Stmt* S = GetStmt(N->getLocation()); |
| 58 | |
| 59 | if (!S) |
| 60 | return NULL; |
| 61 | |
| 62 | FullSourceLoc L(S->getLocStart(), Ctx.getSourceManager()); |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 63 | |
| 64 | PathDiagnosticPiece* P = |
| 65 | new PathDiagnosticPiece(L, getDescription()); |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | 521b294 | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 67 | const SourceRange *Beg, *End; |
| 68 | getRanges(Beg, End); |
| 69 | |
| 70 | if (Beg == End) { |
| 71 | if (Expr* E = dyn_cast<Expr>(S)) |
| 72 | P->addRange(E->getSourceRange()); |
| 73 | } |
| 74 | else { |
| 75 | assert (Beg < End); |
| 76 | for (; Beg != End; ++Beg) |
| 77 | P->addRange(*Beg); |
| 78 | } |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 79 | |
| 80 | return P; |
| 81 | } |
| 82 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 83 | void BugReport::getRanges(const SourceRange*& beg, |
| 84 | const SourceRange*& end) const { |
Ted Kremenek | f00daf0 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 85 | beg = NULL; |
| 86 | end = NULL; |
| 87 | } |
| 88 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 89 | PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N, |
| 90 | ExplodedNode<ValueState>* PrevN, |
| 91 | ExplodedGraph<ValueState>& G, |
| 92 | ASTContext& Ctx) { |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
| 97 | BugReport& R, |
| 98 | ExplodedNode<ValueState>* N) { |
| 99 | |
| 100 | if (PathDiagnosticPiece* Piece = R.getEndPath(Ctx,N)) |
Ted Kremenek | 1f997b1 | 2008-04-03 07:33:55 +0000 | [diff] [blame] | 101 | PD.push_back(Piece); |
| 102 | else |
| 103 | return; |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 104 | |
| 105 | SourceManager& SMgr = Ctx.getSourceManager(); |
| 106 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 107 | llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(getGraph().Trim(&N, &N+1)); |
Ted Kremenek | 7d8674e | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 108 | |
| 109 | // Find the sink in the trimmed graph. |
| 110 | // FIXME: Should we eventually have a sink iterator? |
| 111 | |
| 112 | ExplodedNode<ValueState>* NewN = 0; |
| 113 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 114 | for (ExplodedGraph<ValueState>::node_iterator |
Ted Kremenek | 7d8674e | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 115 | I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) { |
| 116 | |
| 117 | if (I->isSink()) { |
| 118 | NewN = &*I; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | assert (NewN); |
| 124 | assert (NewN->getLocation() == N->getLocation()); |
| 125 | |
| 126 | N = NewN; |
Ted Kremenek | 4067480 | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 127 | |
| 128 | ExplodedNode<ValueState>* NextNode = N->pred_empty() |
| 129 | ? NULL : *(N->pred_begin()); |
| 130 | |
| 131 | while (NextNode) { |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 132 | |
| 133 | ExplodedNode<ValueState>* LastNode = N; |
Ted Kremenek | 4067480 | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 134 | N = NextNode; |
| 135 | NextNode = N->pred_empty() ? NULL : *(N->pred_begin()); |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 136 | |
| 137 | ProgramPoint P = N->getLocation(); |
| 138 | |
| 139 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 140 | |
| 141 | CFGBlock* Src = BE->getSrc(); |
| 142 | CFGBlock* Dst = BE->getDst(); |
| 143 | |
| 144 | Stmt* T = Src->getTerminator(); |
| 145 | |
| 146 | if (!T) |
| 147 | continue; |
| 148 | |
| 149 | FullSourceLoc L(T->getLocStart(), SMgr); |
| 150 | |
| 151 | switch (T->getStmtClass()) { |
| 152 | default: |
| 153 | break; |
| 154 | |
| 155 | case Stmt::GotoStmtClass: |
| 156 | case Stmt::IndirectGotoStmtClass: { |
| 157 | |
| 158 | Stmt* S = GetStmt(LastNode->getLocation()); |
| 159 | |
| 160 | if (!S) |
| 161 | continue; |
| 162 | |
| 163 | std::ostringstream os; |
| 164 | |
| 165 | os << "Control jumps to line " |
Ted Kremenek | b121764 | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 166 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 167 | |
| 168 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | case Stmt::SwitchStmtClass: { |
| 173 | |
| 174 | // Figure out what case arm we took. |
| 175 | |
| 176 | Stmt* S = Dst->getLabel(); |
| 177 | |
| 178 | if (!S) |
| 179 | continue; |
| 180 | |
| 181 | std::ostringstream os; |
| 182 | |
| 183 | switch (S->getStmtClass()) { |
| 184 | default: |
| 185 | continue; |
| 186 | |
| 187 | case Stmt::DefaultStmtClass: { |
| 188 | |
| 189 | os << "Control jumps to the 'default' case at line " |
Ted Kremenek | 4067480 | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 190 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 191 | |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | case Stmt::CaseStmtClass: { |
| 196 | |
| 197 | os << "Control jumps to 'case "; |
| 198 | |
| 199 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 200 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 201 | |
| 202 | llvm::APSInt V1(bits, false); |
| 203 | |
| 204 | CaseStmt* Case = cast<CaseStmt>(S); |
| 205 | |
| 206 | if (!Case->getLHS()->isIntegerConstantExpr(V1, Ctx, 0, true)) { |
| 207 | assert (false && |
| 208 | "Case condition must evaluate to an integer constant."); |
| 209 | continue; |
| 210 | } |
| 211 | |
| 212 | os << V1.toString(); |
| 213 | |
| 214 | // Get the RHS of the case, if it exists. |
| 215 | |
| 216 | if (Expr* E = Case->getRHS()) { |
| 217 | |
| 218 | llvm::APSInt V2(bits, false); |
| 219 | |
| 220 | if (!E->isIntegerConstantExpr(V2, Ctx, 0, true)) { |
| 221 | assert (false && |
| 222 | "Case condition (RHS) must evaluate to an integer constant."); |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | os << " .. " << V2.toString(); |
| 227 | } |
| 228 | |
| 229 | os << ":' at line " |
Ted Kremenek | b121764 | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 230 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 231 | |
| 232 | break; |
| 233 | |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 238 | break; |
| 239 | } |
Ted Kremenek | b121764 | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 240 | |
| 241 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | b121764 | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 243 | std::ostringstream os; |
| 244 | os << "'?' condition evaluates to "; |
| 245 | |
| 246 | if (*(Src->succ_begin()+1) == Dst) |
| 247 | os << "false."; |
| 248 | else |
| 249 | os << "true."; |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | b121764 | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 251 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 252 | |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | case Stmt::DoStmtClass: { |
| 257 | |
| 258 | if (*(Src->succ_begin()) == Dst) { |
| 259 | |
| 260 | std::ostringstream os; |
| 261 | |
| 262 | os << "Loop condition is true. Execution continues on line " |
| 263 | << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.'; |
| 264 | |
| 265 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 266 | } |
| 267 | else |
| 268 | PD.push_front(new PathDiagnosticPiece(L, |
| 269 | "Loop condition is false. Exiting loop.")); |
| 270 | |
| 271 | break; |
| 272 | } |
| 273 | |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 274 | case Stmt::WhileStmtClass: |
Ted Kremenek | b121764 | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 275 | case Stmt::ForStmtClass: { |
| 276 | |
| 277 | if (*(Src->succ_begin()+1) == Dst) { |
| 278 | |
| 279 | std::ostringstream os; |
| 280 | |
| 281 | os << "Loop condition is false. Execution continues on line " |
| 282 | << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.'; |
| 283 | |
| 284 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 285 | } |
| 286 | else |
| 287 | PD.push_front(new PathDiagnosticPiece(L, |
| 288 | "Loop condition is true. Entering loop body.")); |
| 289 | |
| 290 | break; |
| 291 | } |
| 292 | |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 293 | case Stmt::IfStmtClass: { |
| 294 | |
| 295 | if (*(Src->succ_begin()+1) == Dst) |
| 296 | PD.push_front(new PathDiagnosticPiece(L, "Taking false branch.")); |
| 297 | else |
| 298 | PD.push_front(new PathDiagnosticPiece(L, "Taking true branch.")); |
| 299 | |
| 300 | break; |
| 301 | } |
| 302 | } |
Ted Kremenek | 4067480 | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 303 | } |
| 304 | else |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 305 | if (PathDiagnosticPiece* piece = R.VisitNode(N, NextNode, *GTrim, Ctx)) |
| 306 | PD.push_front(piece); |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | bool BugReporter::IsCached(ExplodedNode<ValueState>* N) { |
| 311 | |
| 312 | // HACK: Cache the location of the error. Don't emit the same |
| 313 | // warning for the same error type that occurs at the same program |
| 314 | // location but along a different path. |
| 315 | |
| 316 | void* p = N->getLocation().getRawData(); |
| 317 | |
| 318 | if (CachedErrors.count(p)) |
| 319 | return true; |
| 320 | |
| 321 | CachedErrors.insert(p); |
| 322 | |
| 323 | return false; |
| 324 | } |
| 325 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 326 | void BugReporter::EmitPathWarning(BugReport& R, ExplodedNode<ValueState>* N) { |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 327 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 328 | if (!PD) { |
| 329 | EmitWarning(R, N); |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | |
| 333 | if (IsCached(N)) |
| 334 | return; |
| 335 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 336 | PathDiagnostic D(R.getName()); |
| 337 | GeneratePathDiagnostic(D, R, N); |
Ted Kremenek | 1f997b1 | 2008-04-03 07:33:55 +0000 | [diff] [blame] | 338 | |
| 339 | if (!D.empty()) |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 340 | PD->HandlePathDiagnostic(D); |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 344 | void BugReporter::EmitWarning(BugReport& R, ExplodedNode<ValueState>* N) { |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 345 | if (IsCached(N)) |
| 346 | return; |
| 347 | |
| 348 | std::ostringstream os; |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 349 | os << "[CHECKER] " << R.getDescription(); |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 350 | |
| 351 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 352 | os.str().c_str()); |
| 353 | |
| 354 | // FIXME: Add support for multiple ranges. |
| 355 | |
| 356 | Stmt* S = GetStmt(N->getLocation()); |
| 357 | |
| 358 | if (!S) |
| 359 | return; |
| 360 | |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 361 | SourceRange Range = S->getSourceRange(); |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 362 | |
| 363 | Diag.Report(FullSourceLoc(S->getLocStart(), Ctx.getSourceManager()), |
Ted Kremenek | 0e80dea | 2008-04-09 21:41:14 +0000 | [diff] [blame^] | 364 | ErrorDiag, NULL, 0, &Range, 1); |
Ted Kremenek | 7d88220 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 365 | } |