Ted Kremenek | 61f3e05 | 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 | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 61f3e05 | 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 | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 29 | BugType::~BugType() {} |
| 30 | BugReport::~BugReport() {} |
Ted Kremenek | 5e55cda | 2008-04-11 18:40:29 +0000 | [diff] [blame] | 31 | RangedBugReport::~RangedBugReport() {} |
| 32 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 33 | ExplodedGraph<ValueState>& BugReporter::getGraph() { return Eng.getGraph(); } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 34 | |
| 35 | static inline Stmt* GetStmt(const ProgramPoint& P) { |
| 36 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 37 | return PS->getStmt(); |
| 38 | } |
| 39 | else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 40 | return BE->getSrc()->getTerminator(); |
| 41 | } |
| 42 | else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) { |
| 43 | return BE->getFirstStmt(); |
| 44 | } |
| 45 | |
| 46 | assert (false && "Unsupported ProgramPoint."); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 50 | static inline Stmt* GetStmt(const CFGBlock* B) { |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 51 | if (B->empty()) |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 52 | return const_cast<Stmt*>(B->getTerminator()); |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 53 | else |
| 54 | return (*B)[0]; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 57 | static inline ExplodedNode<ValueState>* |
| 58 | GetNextNode(ExplodedNode<ValueState>* N) { |
| 59 | return N->pred_empty() ? NULL : *(N->pred_begin()); |
| 60 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 62 | static Stmt* GetLastStmt(ExplodedNode<ValueState>* N) { |
| 63 | assert (isa<BlockEntrance>(N->getLocation())); |
| 64 | |
| 65 | for (N = GetNextNode(N); N; N = GetNextNode(N)) { |
| 66 | |
| 67 | ProgramPoint P = N->getLocation(); |
| 68 | |
| 69 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 70 | return PS->getStmt(); |
| 71 | } |
| 72 | |
| 73 | return NULL; |
| 74 | } |
| 75 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 76 | static void ExecutionContinues(std::ostringstream& os, SourceManager& SMgr, |
| 77 | Stmt* S) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 78 | |
| 79 | if (!S) |
| 80 | return; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 81 | |
| 82 | // Slow, but probably doesn't matter. |
| 83 | if (os.str().empty()) |
| 84 | os << ' '; |
| 85 | |
| 86 | os << "Execution continues on line " |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 87 | << SMgr.getLogicalLineNumber(S->getLocStart()) << '.'; |
| 88 | } |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | static inline void ExecutionContinues(std::ostringstream& os, |
| 92 | SourceManager& SMgr, |
| 93 | ExplodedNode<ValueState>* N) { |
| 94 | |
| 95 | ExecutionContinues(os, SMgr, GetStmt(N->getLocation())); |
| 96 | } |
| 97 | |
| 98 | static inline void ExecutionContinues(std::ostringstream& os, |
| 99 | SourceManager& SMgr, |
| 100 | const CFGBlock* B) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 102 | ExecutionContinues(os, SMgr, GetStmt(B)); |
| 103 | } |
| 104 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 105 | |
| 106 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
| 107 | |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 108 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 109 | Stmt *S = NULL; |
| 110 | |
| 111 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) |
| 112 | if (BE->getBlock() == &BR.getCFG().getExit()) |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 113 | S = GetLastStmt(EndNode); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 114 | if (!S) |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 115 | S = GetStmt(ProgP); |
| 116 | |
| 117 | return S; |
| 118 | } |
| 119 | |
| 120 | PathDiagnosticPiece* |
| 121 | BugReport::getEndPath(BugReporter& BR, |
| 122 | ExplodedNode<ValueState>* EndPathNode) { |
| 123 | |
| 124 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 125 | |
| 126 | if (!S) |
| 127 | return NULL; |
| 128 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 129 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
| 130 | PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 132 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 133 | getRanges(BR, Beg, End); |
| 134 | |
| 135 | for (; Beg != End; ++Beg) |
| 136 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 137 | |
| 138 | return P; |
| 139 | } |
| 140 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 141 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 142 | const SourceRange*& end) { |
| 143 | |
| 144 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 145 | R = E->getSourceRange(); |
| 146 | beg = &R; |
| 147 | end = beg+1; |
| 148 | } |
| 149 | else |
| 150 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 153 | FullSourceLoc BugReport::getLocation(SourceManager& Mgr) { |
| 154 | |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 155 | if (!EndNode) |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 156 | return FullSourceLoc(); |
| 157 | |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 158 | Stmt* S = GetStmt(EndNode->getLocation()); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 159 | |
| 160 | if (!S) |
| 161 | return FullSourceLoc(); |
| 162 | |
| 163 | return FullSourceLoc(S->getLocStart(), Mgr); |
| 164 | } |
| 165 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 166 | PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N, |
| 167 | ExplodedNode<ValueState>* PrevN, |
| 168 | ExplodedGraph<ValueState>& G, |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 169 | BugReporter& BR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 170 | return NULL; |
| 171 | } |
| 172 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 173 | static std::pair<ExplodedGraph<ValueState>*, ExplodedNode<ValueState>*> |
| 174 | MakeReportGraph(ExplodedGraph<ValueState>* G, ExplodedNode<ValueState>* N) { |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 176 | llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(G->Trim(&N, &N+1)); |
| 177 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 178 | // Find the error node in the trimmed graph. |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 180 | ExplodedNode<ValueState>* NOld = N; |
| 181 | N = 0; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 183 | for (ExplodedGraph<ValueState>::node_iterator |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 184 | I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 186 | if (I->getState() == NOld->getState() && |
| 187 | I->getLocation() == NOld->getLocation()) { |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 188 | N = &*I; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 193 | assert(N); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 195 | // Create a new graph with a single path. |
| 196 | |
| 197 | G = new ExplodedGraph<ValueState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 198 | GTrim->getContext()); |
| 199 | |
| 200 | |
Ted Kremenek | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 201 | ExplodedNode<ValueState> *Last = 0, *First = 0; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 202 | |
| 203 | while (N) { |
| 204 | ExplodedNode<ValueState>* NewN = |
| 205 | G->getNode(N->getLocation(), N->getState()); |
| 206 | |
Ted Kremenek | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 207 | if (!First) First = NewN; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 208 | if (Last) Last->addPredecessor(NewN); |
| 209 | |
| 210 | Last = NewN; |
| 211 | N = N->pred_empty() ? 0 : *(N->pred_begin()); |
| 212 | } |
| 213 | |
Ted Kremenek | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 214 | return std::make_pair(G, First); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
| 218 | BugReport& R) { |
| 219 | |
| 220 | ExplodedNode<ValueState>* N = R.getEndNode(); |
| 221 | |
| 222 | if (!N) return; |
| 223 | |
| 224 | // Construct a new graph that contains only a single path from the error |
| 225 | // node to a root. |
| 226 | |
| 227 | const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*> |
| 228 | GPair = MakeReportGraph(&getGraph(), N); |
| 229 | |
| 230 | llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first); |
| 231 | assert(GPair.second->getLocation() == N->getLocation()); |
| 232 | N = GPair.second; |
| 233 | |
| 234 | // Start building the path diagnostic... |
| 235 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 236 | if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N)) |
| 237 | PD.push_back(Piece); |
| 238 | else |
| 239 | return; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 240 | |
| 241 | ExplodedNode<ValueState>* NextNode = N->pred_empty() |
| 242 | ? NULL : *(N->pred_begin()); |
| 243 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 244 | SourceManager& SMgr = Ctx.getSourceManager(); |
| 245 | |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 246 | while (NextNode) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 247 | |
| 248 | ExplodedNode<ValueState>* LastNode = N; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 249 | N = NextNode; |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 250 | NextNode = GetNextNode(N); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 251 | |
| 252 | ProgramPoint P = N->getLocation(); |
| 253 | |
| 254 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 255 | |
| 256 | CFGBlock* Src = BE->getSrc(); |
| 257 | CFGBlock* Dst = BE->getDst(); |
| 258 | |
| 259 | Stmt* T = Src->getTerminator(); |
| 260 | |
| 261 | if (!T) |
| 262 | continue; |
| 263 | |
| 264 | FullSourceLoc L(T->getLocStart(), SMgr); |
| 265 | |
| 266 | switch (T->getStmtClass()) { |
| 267 | default: |
| 268 | break; |
| 269 | |
| 270 | case Stmt::GotoStmtClass: |
| 271 | case Stmt::IndirectGotoStmtClass: { |
| 272 | |
| 273 | Stmt* S = GetStmt(LastNode->getLocation()); |
| 274 | |
| 275 | if (!S) |
| 276 | continue; |
| 277 | |
| 278 | std::ostringstream os; |
| 279 | |
| 280 | os << "Control jumps to line " |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 281 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 282 | |
| 283 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | case Stmt::SwitchStmtClass: { |
| 288 | |
| 289 | // Figure out what case arm we took. |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 291 | std::ostringstream os; |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 292 | |
| 293 | if (Stmt* S = Dst->getLabel()) |
| 294 | switch (S->getStmtClass()) { |
| 295 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 296 | default: |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 297 | assert(false && "Not a valid switch label."); |
| 298 | continue; |
| 299 | |
Ted Kremenek | 8b90455 | 2008-04-22 22:29:46 +0000 | [diff] [blame] | 300 | case Stmt::DefaultStmtClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 301 | |
| 302 | os << "Control jumps to the 'default' case at line " |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 303 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 304 | |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | case Stmt::CaseStmtClass: { |
| 309 | |
| 310 | os << "Control jumps to 'case "; |
| 311 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 312 | CaseStmt* Case = cast<CaseStmt>(S); |
| 313 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 315 | // Determine if it is an enum. |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 316 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 317 | bool GetRawInt = true; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 318 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 319 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 320 | |
| 321 | // FIXME: Maybe this should be an assertion. Are there cases |
| 322 | // were it is not an EnumConstantDecl? |
| 323 | |
| 324 | EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 325 | |
| 326 | if (D) { |
| 327 | GetRawInt = false; |
| 328 | os << D->getName(); |
| 329 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 330 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 331 | |
| 332 | if (GetRawInt) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 333 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 334 | // Not an enum. |
| 335 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 336 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 337 | llvm::APSInt V(bits, false); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 339 | if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) { |
| 340 | assert (false && "Case condition must be constant."); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 341 | continue; |
| 342 | } |
| 343 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 344 | os << V.toString(); |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 345 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 347 | os << ":' at line " |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 348 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 349 | |
| 350 | break; |
| 351 | |
| 352 | } |
| 353 | } |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 354 | else { |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 355 | os << "'Default' branch taken."; |
| 356 | ExecutionContinues(os, SMgr, LastNode); |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 357 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 358 | |
| 359 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 360 | break; |
| 361 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 362 | |
| 363 | case Stmt::BreakStmtClass: |
| 364 | case Stmt::ContinueStmtClass: { |
| 365 | std::ostringstream os; |
| 366 | ExecutionContinues(os, SMgr, LastNode); |
| 367 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 368 | break; |
| 369 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 370 | |
| 371 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 373 | std::ostringstream os; |
| 374 | os << "'?' condition evaluates to "; |
| 375 | |
| 376 | if (*(Src->succ_begin()+1) == Dst) |
| 377 | os << "false."; |
| 378 | else |
| 379 | os << "true."; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 381 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 382 | |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | case Stmt::DoStmtClass: { |
| 387 | |
| 388 | if (*(Src->succ_begin()) == Dst) { |
| 389 | |
| 390 | std::ostringstream os; |
| 391 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 392 | os << "Loop condition is true."; |
| 393 | ExecutionContinues(os, SMgr, Dst); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 394 | |
| 395 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 396 | } |
| 397 | else |
| 398 | PD.push_front(new PathDiagnosticPiece(L, |
| 399 | "Loop condition is false. Exiting loop.")); |
| 400 | |
| 401 | break; |
| 402 | } |
| 403 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 404 | case Stmt::WhileStmtClass: |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 405 | case Stmt::ForStmtClass: { |
| 406 | |
| 407 | if (*(Src->succ_begin()+1) == Dst) { |
| 408 | |
| 409 | std::ostringstream os; |
| 410 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 411 | os << "Loop condition is false."; |
| 412 | ExecutionContinues(os, SMgr, Dst); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 413 | |
| 414 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 415 | } |
| 416 | else |
| 417 | PD.push_front(new PathDiagnosticPiece(L, |
| 418 | "Loop condition is true. Entering loop body.")); |
| 419 | |
| 420 | break; |
| 421 | } |
| 422 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 423 | case Stmt::IfStmtClass: { |
| 424 | |
| 425 | if (*(Src->succ_begin()+1) == Dst) |
| 426 | PD.push_front(new PathDiagnosticPiece(L, "Taking false branch.")); |
| 427 | else |
| 428 | PD.push_front(new PathDiagnosticPiece(L, "Taking true branch.")); |
| 429 | |
| 430 | break; |
| 431 | } |
| 432 | } |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 433 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 434 | |
| 435 | if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this)) |
| 436 | PD.push_front(p); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 440 | bool BugTypeCacheLocation::isCached(BugReport& R) { |
| 441 | |
| 442 | ExplodedNode<ValueState>* N = R.getEndNode(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 443 | |
Ted Kremenek | 329d2cc | 2008-04-18 02:24:50 +0000 | [diff] [blame] | 444 | if (!N) |
| 445 | return false; |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 446 | |
| 447 | // Cache the location of the error. Don't emit the same |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 448 | // warning for the same error type that occurs at the same program |
| 449 | // location but along a different path. |
| 450 | |
| 451 | void* p = N->getLocation().getRawData(); |
| 452 | |
| 453 | if (CachedErrors.count(p)) |
| 454 | return true; |
| 455 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 456 | CachedErrors.insert(p); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 460 | void BugReporter::EmitWarning(BugReport& R) { |
| 461 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 462 | if (R.getBugType().isCached(R)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 463 | return; |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 465 | llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName())); |
| 466 | GeneratePathDiagnostic(*D.get(), R); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 467 | |
| 468 | // Get the meta data. |
| 469 | |
| 470 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
| 471 | |
| 472 | for (const char** s = Meta.first; s != Meta.second; ++s) |
| 473 | D->addMeta(*s); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 474 | |
| 475 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
Ted Kremenek | 70d1722 | 2008-04-03 07:33:55 +0000 | [diff] [blame] | 476 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 477 | if (PD && !D->empty()) { |
| 478 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 479 | return; |
| 480 | } |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 481 | |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 482 | // We don't have a PathDiagnosticClient, but we can still emit a single |
| 483 | // line diagnostic. Determine the location. |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 484 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 485 | FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager()) |
| 486 | : D->back()->getLocation(); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 487 | |
| 488 | |
| 489 | // Determine the range. |
Ted Kremenek | 5fcca68 | 2008-04-14 18:06:42 +0000 | [diff] [blame] | 490 | |
Ted Kremenek | 4bb6ac2 | 2008-04-10 16:12:38 +0000 | [diff] [blame] | 491 | const SourceRange *Beg, *End; |
Ted Kremenek | 5fcca68 | 2008-04-14 18:06:42 +0000 | [diff] [blame] | 492 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 493 | if (!D->empty()) { |
| 494 | Beg = D->back()->ranges_begin(); |
| 495 | End = D->back()->ranges_end(); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 496 | } |
| 497 | else |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 498 | R.getRanges(*this, Beg, End); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 499 | |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 500 | if (PD) { |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 501 | PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription()); |
| 502 | |
| 503 | for ( ; Beg != End; ++Beg) |
| 504 | piece->addRange(*Beg); |
| 505 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 506 | D->push_back(piece); |
| 507 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 508 | } |
| 509 | else { |
| 510 | std::ostringstream os; |
| 511 | os << "[CHECKER] "; |
| 512 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 513 | if (D->empty()) |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 514 | os << R.getDescription(); |
| 515 | else |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 516 | os << D->back()->getString(); |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 517 | |
| 518 | |
| 519 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 520 | os.str().c_str()); |
| 521 | |
| 522 | Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg); |
| 523 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 524 | } |