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" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Casting.h" |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 20 | #include <sstream> |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 21 | using namespace clang; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 22 | using llvm::dyn_cast; |
| 23 | using llvm::isa; |
| 24 | |
| 25 | bool PathDiagnosticMacroPiece::containsEvent() const { |
| 26 | for (const_iterator I = begin(), E = end(); I!=E; ++I) { |
| 27 | if (isa<PathDiagnosticEventPiece>(*I)) |
| 28 | return true; |
| 29 | |
| 30 | if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 31 | if (MP->containsEvent()) |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | return false; |
| 36 | } |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 37 | |
| 38 | static size_t GetNumCharsToLastNonPeriod(const char *s) { |
| 39 | const char *start = s; |
| 40 | const char *lastNonPeriod = 0; |
| 41 | |
| 42 | for ( ; *s != '\0' ; ++s) |
| 43 | if (*s != '.') lastNonPeriod = s; |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 45 | if (!lastNonPeriod) |
| 46 | return 0; |
| 47 | |
| 48 | return (lastNonPeriod - start) + 1; |
| 49 | } |
| 50 | |
| 51 | static inline size_t GetNumCharsToLastNonPeriod(const std::string &s) { |
| 52 | return s.empty () ? 0 : GetNumCharsToLastNonPeriod(&s[0]); |
| 53 | } |
| 54 | |
Ted Kremenek | 1f9bd0f | 2009-03-26 21:21:35 +0000 | [diff] [blame] | 55 | PathDiagnosticPiece::PathDiagnosticPiece(const std::string& s, |
Ted Kremenek | e3ce265 | 2009-03-02 19:39:50 +0000 | [diff] [blame] | 56 | Kind k, DisplayHint hint) |
Ted Kremenek | 1f9bd0f | 2009-03-26 21:21:35 +0000 | [diff] [blame] | 57 | : str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {} |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | 1f9bd0f | 2009-03-26 21:21:35 +0000 | [diff] [blame] | 59 | PathDiagnosticPiece::PathDiagnosticPiece(const char* s, Kind k, |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 60 | DisplayHint hint) |
Ted Kremenek | 1f9bd0f | 2009-03-26 21:21:35 +0000 | [diff] [blame] | 61 | : str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {} |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 1f9bd0f | 2009-03-26 21:21:35 +0000 | [diff] [blame] | 63 | PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint) |
| 64 | : kind(k), Hint(hint) {} |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 4e06387 | 2009-03-06 22:10:49 +0000 | [diff] [blame] | 66 | PathDiagnosticPiece::~PathDiagnosticPiece() {} |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 67 | PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {} |
| 68 | PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {} |
Ted Kremenek | 4e06387 | 2009-03-06 22:10:49 +0000 | [diff] [blame] | 69 | |
| 70 | PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() { |
| 71 | for (iterator I = begin(), E = end(); I != E; ++I) delete *I; |
| 72 | } |
| 73 | |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 74 | PathDiagnostic::PathDiagnostic() : Size(0) {} |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 76 | PathDiagnostic::~PathDiagnostic() { |
| 77 | for (iterator I = begin(), E = end(); I != E; ++I) delete &*I; |
| 78 | } |
| 79 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 80 | void PathDiagnostic::resetPath(bool deletePieces) { |
| 81 | Size = 0; |
| 82 | |
| 83 | if (deletePieces) |
| 84 | for (iterator I=begin(), E=end(); I!=E; ++I) |
| 85 | delete &*I; |
| 86 | |
| 87 | path.clear(); |
| 88 | } |
| 89 | |
Ted Kremenek | a127cca | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 90 | |
| 91 | PathDiagnostic::PathDiagnostic(const char* bugtype, const char* desc, |
| 92 | const char* category) |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 93 | : Size(0), |
| 94 | BugType(bugtype, GetNumCharsToLastNonPeriod(bugtype)), |
Ted Kremenek | a127cca | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 95 | Desc(desc, GetNumCharsToLastNonPeriod(desc)), |
| 96 | Category(category, GetNumCharsToLastNonPeriod(category)) {} |
| 97 | |
| 98 | PathDiagnostic::PathDiagnostic(const std::string& bugtype, |
| 99 | const std::string& desc, |
| 100 | const std::string& category) |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 101 | : Size(0), |
| 102 | BugType(bugtype, 0, GetNumCharsToLastNonPeriod(bugtype)), |
Ted Kremenek | a127cca | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 103 | Desc(desc, 0, GetNumCharsToLastNonPeriod(desc)), |
| 104 | Category(category, 0, GetNumCharsToLastNonPeriod(category)) {} |
| 105 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 106 | void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 107 | const DiagnosticInfo &Info) { |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 108 | |
| 109 | // Create a PathDiagnostic with a single piece. |
| 110 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 111 | PathDiagnostic* D = new PathDiagnostic(); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 112 | |
Chris Lattner | e837f93 | 2008-11-18 04:44:58 +0000 | [diff] [blame] | 113 | const char *LevelStr; |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 114 | switch (DiagLevel) { |
Mike Stump | e87f5c1 | 2009-02-07 03:46:08 +0000 | [diff] [blame] | 115 | default: |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 116 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
Chris Lattner | e837f93 | 2008-11-18 04:44:58 +0000 | [diff] [blame] | 117 | case Diagnostic::Note: LevelStr = "note: "; break; |
| 118 | case Diagnostic::Warning: LevelStr = "warning: "; break; |
| 119 | case Diagnostic::Error: LevelStr = "error: "; break; |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 120 | case Diagnostic::Fatal: LevelStr = "fatal error: "; break; |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 121 | } |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 122 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 123 | llvm::SmallString<100> StrC; |
| 124 | StrC += LevelStr; |
| 125 | Info.FormatDiagnostic(StrC); |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 127 | PathDiagnosticPiece *P = |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 128 | new PathDiagnosticEventPiece(Info.getLocation(), |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 129 | std::string(StrC.begin(), StrC.end())); |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 131 | for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) |
| 132 | P->addRange(Info.getRange(i)); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 133 | for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i) |
| 134 | P->addCodeModificationHint(Info.getCodeModificationHint(i)); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 135 | D->push_front(P); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 137 | HandlePathDiagnostic(D); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 138 | } |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 139 | |
| 140 | //===----------------------------------------------------------------------===// |
| 141 | // PathDiagnosticLocation methods. |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
| 144 | FullSourceLoc PathDiagnosticLocation::asLocation() const { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 145 | assert(isValid()); |
Ted Kremenek | b773702 | 2009-03-26 21:42:51 +0000 | [diff] [blame] | 146 | // Note that we want a 'switch' here so that the compiler can warn us in |
| 147 | // case we add more cases. |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 148 | switch (K) { |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 149 | case SingleLocK: |
| 150 | case RangeK: |
Ted Kremenek | a546500 | 2009-03-26 21:42:00 +0000 | [diff] [blame] | 151 | break; |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 152 | case StmtK: |
Ted Kremenek | 6f00204 | 2009-03-26 23:12:02 +0000 | [diff] [blame] | 153 | return FullSourceLoc(S->getLocStart(), const_cast<SourceManager&>(*SM)); |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 154 | case DeclK: |
| 155 | return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM)); |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 156 | } |
Ted Kremenek | a546500 | 2009-03-26 21:42:00 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | 6f00204 | 2009-03-26 23:12:02 +0000 | [diff] [blame] | 158 | return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM)); |
Ted Kremenek | 97b4003 | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 159 | } |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 160 | |
| 161 | SourceRange PathDiagnosticLocation::asRange() const { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 162 | assert(isValid()); |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 163 | // Note that we want a 'switch' here so that the compiler can warn us in |
| 164 | // case we add more cases. |
| 165 | switch (K) { |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 166 | case SingleLocK: |
| 167 | case RangeK: |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 168 | break; |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 169 | case StmtK: |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 170 | return S->getSourceRange(); |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 171 | case DeclK: |
| 172 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 173 | return MD->getSourceRange(); |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame^] | 174 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 175 | // FIXME: We would like to always get the function body, even |
| 176 | // when it needs to be de-serialized, but getting the |
| 177 | // ASTContext here requires significant changes. |
| 178 | if (CompoundStmt *Body = FD->getBodyIfAvailable()) |
| 179 | return Body->getSourceRange(); |
| 180 | } |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 181 | else { |
| 182 | SourceLocation L = D->getLocation(); |
| 183 | return SourceRange(L, L); |
| 184 | } |
Ted Kremenek | becf888 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return R; |
| 188 | } |
| 189 | |
Ted Kremenek | fea5f5a | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 190 | void PathDiagnosticLocation::flatten() { |
| 191 | if (K == StmtK) { |
| 192 | R = asRange(); |
| 193 | K = RangeK; |
| 194 | S = 0; |
| 195 | D = 0; |
| 196 | } |
| 197 | else if (K == DeclK) { |
| 198 | SourceLocation L = D->getLocation(); |
| 199 | R = SourceRange(L, L); |
| 200 | K = SingleLocK; |
| 201 | S = 0; |
| 202 | D = 0; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |