Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 1 | //===--- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -*- 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 the PathDiagnostic-related interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtCXX.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Casting.h" |
Ted Kremenek | 1140186 | 2009-06-26 00:43:22 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 22 | using namespace clang; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 23 | using llvm::dyn_cast; |
| 24 | using llvm::isa; |
| 25 | |
| 26 | bool PathDiagnosticMacroPiece::containsEvent() const { |
| 27 | for (const_iterator I = begin(), E = end(); I!=E; ++I) { |
| 28 | if (isa<PathDiagnosticEventPiece>(*I)) |
| 29 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 31 | if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 32 | if (MP->containsEvent()) |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | return false; |
| 37 | } |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 38 | |
Benjamin Kramer | d8e494f | 2009-12-11 21:09:27 +0000 | [diff] [blame] | 39 | static llvm::StringRef StripTrailingDots(llvm::StringRef s) { |
| 40 | for (llvm::StringRef::size_type i = s.size(); i != 0; --i) |
| 41 | if (s[i - 1] != '.') |
| 42 | return s.substr(0, i); |
| 43 | return ""; |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Benjamin Kramer | d8e494f | 2009-12-11 21:09:27 +0000 | [diff] [blame] | 46 | PathDiagnosticPiece::PathDiagnosticPiece(llvm::StringRef s, |
Ted Kremenek | e3ce265 | 2009-03-02 19:39:50 +0000 | [diff] [blame] | 47 | Kind k, DisplayHint hint) |
Benjamin Kramer | d8e494f | 2009-12-11 21:09:27 +0000 | [diff] [blame] | 48 | : str(StripTrailingDots(s)), kind(k), Hint(hint) {} |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 1f9bd0f | 2009-03-26 21:21:35 +0000 | [diff] [blame] | 50 | PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint) |
| 51 | : kind(k), Hint(hint) {} |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | 4e06387 | 2009-03-06 22:10:49 +0000 | [diff] [blame] | 53 | PathDiagnosticPiece::~PathDiagnosticPiece() {} |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 54 | PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {} |
| 55 | PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {} |
Ted Kremenek | 4e06387 | 2009-03-06 22:10:49 +0000 | [diff] [blame] | 56 | |
| 57 | PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() { |
| 58 | for (iterator I = begin(), E = end(); I != E; ++I) delete *I; |
| 59 | } |
| 60 | |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 61 | PathDiagnostic::PathDiagnostic() : Size(0) {} |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 63 | PathDiagnostic::~PathDiagnostic() { |
| 64 | for (iterator I = begin(), E = end(); I != E; ++I) delete &*I; |
| 65 | } |
| 66 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 67 | void PathDiagnostic::resetPath(bool deletePieces) { |
| 68 | Size = 0; |
| 69 | |
| 70 | if (deletePieces) |
| 71 | for (iterator I=begin(), E=end(); I!=E; ++I) |
| 72 | delete &*I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 74 | path.clear(); |
| 75 | } |
| 76 | |
Ted Kremenek | a127cca | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 77 | |
Benjamin Kramer | d8e494f | 2009-12-11 21:09:27 +0000 | [diff] [blame] | 78 | PathDiagnostic::PathDiagnostic(llvm::StringRef bugtype, llvm::StringRef desc, |
| 79 | llvm::StringRef category) |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 80 | : Size(0), |
Benjamin Kramer | d8e494f | 2009-12-11 21:09:27 +0000 | [diff] [blame] | 81 | BugType(StripTrailingDots(bugtype)), |
| 82 | Desc(StripTrailingDots(desc)), |
| 83 | Category(StripTrailingDots(category)) {} |
Ted Kremenek | a127cca | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 85 | void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 86 | const DiagnosticInfo &Info) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 88 | // Create a PathDiagnostic with a single piece. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 90 | PathDiagnostic* D = new PathDiagnostic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | e837f93 | 2008-11-18 04:44:58 +0000 | [diff] [blame] | 92 | const char *LevelStr; |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 93 | switch (DiagLevel) { |
Mike Stump | e87f5c1 | 2009-02-07 03:46:08 +0000 | [diff] [blame] | 94 | default: |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 95 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
Chris Lattner | e837f93 | 2008-11-18 04:44:58 +0000 | [diff] [blame] | 96 | case Diagnostic::Note: LevelStr = "note: "; break; |
| 97 | case Diagnostic::Warning: LevelStr = "warning: "; break; |
| 98 | case Diagnostic::Error: LevelStr = "error: "; break; |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 99 | case Diagnostic::Fatal: LevelStr = "fatal error: "; break; |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 100 | } |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 101 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 102 | llvm::SmallString<100> StrC; |
| 103 | StrC += LevelStr; |
| 104 | Info.FormatDiagnostic(StrC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 106 | PathDiagnosticPiece *P = |
Benjamin Kramer | d8e494f | 2009-12-11 21:09:27 +0000 | [diff] [blame] | 107 | new PathDiagnosticEventPiece(Info.getLocation(), StrC.str()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 109 | for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) |
| 110 | P->addRange(Info.getRange(i)); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 111 | for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i) |
| 112 | P->addCodeModificationHint(Info.getCodeModificationHint(i)); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 113 | D->push_front(P); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 114 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | HandlePathDiagnostic(D); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 116 | } |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 117 | |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | // PathDiagnosticLocation methods. |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
| 122 | FullSourceLoc PathDiagnosticLocation::asLocation() const { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 123 | assert(isValid()); |
Ted Kremenek | b773702 | 2009-03-26 21:42:51 +0000 | [diff] [blame] | 124 | // Note that we want a 'switch' here so that the compiler can warn us in |
| 125 | // case we add more cases. |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 126 | switch (K) { |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 127 | case SingleLocK: |
| 128 | case RangeK: |
Ted Kremenek | a546500 | 2009-03-26 21:42:00 +0000 | [diff] [blame] | 129 | break; |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 130 | case StmtK: |
Ted Kremenek | 6f00204 | 2009-03-26 23:12:02 +0000 | [diff] [blame] | 131 | return FullSourceLoc(S->getLocStart(), const_cast<SourceManager&>(*SM)); |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 132 | case DeclK: |
| 133 | return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM)); |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 134 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 6f00204 | 2009-03-26 23:12:02 +0000 | [diff] [blame] | 136 | return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM)); |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 137 | } |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 4c5fcd9 | 2009-04-22 22:26:10 +0000 | [diff] [blame] | 139 | PathDiagnosticRange PathDiagnosticLocation::asRange() const { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 140 | assert(isValid()); |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 141 | // Note that we want a 'switch' here so that the compiler can warn us in |
| 142 | // case we add more cases. |
| 143 | switch (K) { |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 144 | case SingleLocK: |
Ted Kremenek | 4c5fcd9 | 2009-04-22 22:26:10 +0000 | [diff] [blame] | 145 | return PathDiagnosticRange(R, true); |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 146 | case RangeK: |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 147 | break; |
Ted Kremenek | 8f33290 | 2009-04-22 18:03:00 +0000 | [diff] [blame] | 148 | case StmtK: { |
| 149 | const Stmt *S = asStmt(); |
| 150 | switch (S->getStmtClass()) { |
| 151 | default: |
| 152 | break; |
Ted Kremenek | 1e63c29 | 2009-05-15 02:05:25 +0000 | [diff] [blame] | 153 | case Stmt::DeclStmtClass: { |
| 154 | const DeclStmt *DS = cast<DeclStmt>(S); |
| 155 | if (DS->isSingleDecl()) { |
| 156 | // Should always be the case, but we'll be defensive. |
| 157 | return SourceRange(DS->getLocStart(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | DS->getSingleDecl()->getLocation()); |
Ted Kremenek | 1e63c29 | 2009-05-15 02:05:25 +0000 | [diff] [blame] | 159 | } |
| 160 | break; |
| 161 | } |
Ted Kremenek | 8f33290 | 2009-04-22 18:03:00 +0000 | [diff] [blame] | 162 | // FIXME: Provide better range information for different |
| 163 | // terminators. |
| 164 | case Stmt::IfStmtClass: |
| 165 | case Stmt::WhileStmtClass: |
| 166 | case Stmt::DoStmtClass: |
| 167 | case Stmt::ForStmtClass: |
| 168 | case Stmt::ChooseExprClass: |
| 169 | case Stmt::IndirectGotoStmtClass: |
| 170 | case Stmt::SwitchStmtClass: |
| 171 | case Stmt::ConditionalOperatorClass: |
| 172 | case Stmt::ObjCForCollectionStmtClass: { |
| 173 | SourceLocation L = S->getLocStart(); |
| 174 | return SourceRange(L, L); |
| 175 | } |
| 176 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 178 | return S->getSourceRange(); |
Ted Kremenek | 8f33290 | 2009-04-22 18:03:00 +0000 | [diff] [blame] | 179 | } |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 180 | case DeclK: |
| 181 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 182 | return MD->getSourceRange(); |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 183 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 184 | // FIXME: We would like to always get the function body, even |
| 185 | // when it needs to be de-serialized, but getting the |
| 186 | // ASTContext here requires significant changes. |
Douglas Gregor | af3280f | 2009-09-12 00:08:48 +0000 | [diff] [blame] | 187 | if (Stmt *Body = FD->getBody()) { |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 188 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body)) |
| 189 | return CS->getSourceRange(); |
| 190 | else |
| 191 | return cast<CXXTryStmt>(Body)->getSourceRange(); |
| 192 | } |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 193 | } |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 194 | else { |
| 195 | SourceLocation L = D->getLocation(); |
Ted Kremenek | 4c5fcd9 | 2009-04-22 22:26:10 +0000 | [diff] [blame] | 196 | return PathDiagnosticRange(SourceRange(L, L), true); |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 197 | } |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 198 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 200 | return R; |
| 201 | } |
| 202 | |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 203 | void PathDiagnosticLocation::flatten() { |
| 204 | if (K == StmtK) { |
| 205 | R = asRange(); |
| 206 | K = RangeK; |
| 207 | S = 0; |
| 208 | D = 0; |
| 209 | } |
| 210 | else if (K == DeclK) { |
| 211 | SourceLocation L = D->getLocation(); |
| 212 | R = SourceRange(L, L); |
| 213 | K = SingleLocK; |
| 214 | S = 0; |
| 215 | D = 0; |
| 216 | } |
| 217 | } |
| 218 | |
Ted Kremenek | db8338a | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 219 | //===----------------------------------------------------------------------===// |
| 220 | // FoldingSet profiling methods. |
| 221 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | db8338a | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 223 | void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const { |
| 224 | ID.AddInteger((unsigned) K); |
| 225 | switch (K) { |
| 226 | case RangeK: |
| 227 | ID.AddInteger(R.getBegin().getRawEncoding()); |
| 228 | ID.AddInteger(R.getEnd().getRawEncoding()); |
| 229 | break; |
| 230 | case SingleLocK: |
| 231 | ID.AddInteger(R.getBegin().getRawEncoding()); |
| 232 | break; |
| 233 | case StmtK: |
| 234 | ID.Add(S); |
| 235 | break; |
| 236 | case DeclK: |
| 237 | ID.Add(D); |
| 238 | break; |
| 239 | } |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 244 | ID.AddInteger((unsigned) getKind()); |
| 245 | ID.AddString(str); |
| 246 | // FIXME: Add profiling support for code hints. |
| 247 | ID.AddInteger((unsigned) getDisplayHint()); |
| 248 | for (range_iterator I = ranges_begin(), E = ranges_end(); I != E; ++I) { |
| 249 | ID.AddInteger(I->getBegin().getRawEncoding()); |
| 250 | ID.AddInteger(I->getEnd().getRawEncoding()); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 255 | PathDiagnosticPiece::Profile(ID); |
| 256 | ID.Add(Pos); |
| 257 | } |
| 258 | |
| 259 | void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 260 | PathDiagnosticPiece::Profile(ID); |
| 261 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 262 | ID.Add(*I); |
| 263 | } |
| 264 | |
| 265 | void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 266 | PathDiagnosticSpotPiece::Profile(ID); |
| 267 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 268 | ID.Add(**I); |
| 269 | } |
| 270 | |
| 271 | void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const { |
| 272 | ID.AddInteger(Size); |
| 273 | ID.AddString(BugType); |
| 274 | ID.AddString(Desc); |
| 275 | ID.AddString(Category); |
| 276 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 277 | ID.Add(*I); |
| 278 | |
| 279 | for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I) |
| 280 | ID.AddString(*I); |
| 281 | } |