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" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Casting.h" |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 17 | #include <sstream> |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 18 | using namespace clang; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 19 | using llvm::dyn_cast; |
| 20 | using llvm::isa; |
| 21 | |
| 22 | bool PathDiagnosticMacroPiece::containsEvent() const { |
| 23 | for (const_iterator I = begin(), E = end(); I!=E; ++I) { |
| 24 | if (isa<PathDiagnosticEventPiece>(*I)) |
| 25 | return true; |
| 26 | |
| 27 | if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 28 | if (MP->containsEvent()) |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | return false; |
| 33 | } |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 34 | |
| 35 | static size_t GetNumCharsToLastNonPeriod(const char *s) { |
| 36 | const char *start = s; |
| 37 | const char *lastNonPeriod = 0; |
| 38 | |
| 39 | for ( ; *s != '\0' ; ++s) |
| 40 | if (*s != '.') lastNonPeriod = s; |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 42 | if (!lastNonPeriod) |
| 43 | return 0; |
| 44 | |
| 45 | return (lastNonPeriod - start) + 1; |
| 46 | } |
| 47 | |
| 48 | static inline size_t GetNumCharsToLastNonPeriod(const std::string &s) { |
| 49 | return s.empty () ? 0 : GetNumCharsToLastNonPeriod(&s[0]); |
| 50 | } |
| 51 | |
| 52 | PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos, |
| 53 | const std::string& s, |
Ted Kremenek | e3ce265 | 2009-03-02 19:39:50 +0000 | [diff] [blame] | 54 | Kind k, DisplayHint hint) |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 55 | : Pos(pos), str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) { |
| 56 | assert(Pos.isValid() && |
| 57 | "PathDiagnosticPiece's must have a valid location."); |
| 58 | } |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 59 | |
| 60 | PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos, |
Ted Kremenek | e3ce265 | 2009-03-02 19:39:50 +0000 | [diff] [blame] | 61 | const char* s, Kind k, |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 62 | DisplayHint hint) |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 63 | : Pos(pos), str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) { |
| 64 | assert(Pos.isValid() && |
| 65 | "PathDiagnosticPiece's must have a valid location."); |
| 66 | } |
| 67 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 68 | PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos, Kind k, |
| 69 | DisplayHint hint) |
| 70 | : Pos(pos), kind(k), Hint(hint) { |
| 71 | assert(Pos.isValid() && |
| 72 | "PathDiagnosticPiece's must have a valid location."); |
| 73 | } |
| 74 | |
Ted Kremenek | 4e06387 | 2009-03-06 22:10:49 +0000 | [diff] [blame] | 75 | PathDiagnosticPiece::~PathDiagnosticPiece() {} |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 76 | PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {} |
| 77 | PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {} |
Ted Kremenek | 4e06387 | 2009-03-06 22:10:49 +0000 | [diff] [blame] | 78 | |
| 79 | PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() { |
| 80 | for (iterator I = begin(), E = end(); I != E; ++I) delete *I; |
| 81 | } |
| 82 | |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 83 | PathDiagnostic::PathDiagnostic() : Size(0) {} |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 84 | |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 85 | PathDiagnostic::~PathDiagnostic() { |
| 86 | for (iterator I = begin(), E = end(); I != E; ++I) delete &*I; |
| 87 | } |
| 88 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 89 | void PathDiagnostic::resetPath(bool deletePieces) { |
| 90 | Size = 0; |
| 91 | |
| 92 | if (deletePieces) |
| 93 | for (iterator I=begin(), E=end(); I!=E; ++I) |
| 94 | delete &*I; |
| 95 | |
| 96 | path.clear(); |
| 97 | } |
| 98 | |
Ted Kremenek | a127cca | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 99 | |
| 100 | PathDiagnostic::PathDiagnostic(const char* bugtype, const char* desc, |
| 101 | const char* category) |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 102 | : Size(0), |
| 103 | BugType(bugtype, GetNumCharsToLastNonPeriod(bugtype)), |
Ted Kremenek | a127cca | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 104 | Desc(desc, GetNumCharsToLastNonPeriod(desc)), |
| 105 | Category(category, GetNumCharsToLastNonPeriod(category)) {} |
| 106 | |
| 107 | PathDiagnostic::PathDiagnostic(const std::string& bugtype, |
| 108 | const std::string& desc, |
| 109 | const std::string& category) |
Ted Kremenek | 4850451 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 110 | : Size(0), |
| 111 | BugType(bugtype, 0, GetNumCharsToLastNonPeriod(bugtype)), |
Ted Kremenek | a127cca | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 112 | Desc(desc, 0, GetNumCharsToLastNonPeriod(desc)), |
| 113 | Category(category, 0, GetNumCharsToLastNonPeriod(category)) {} |
| 114 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 115 | void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 116 | const DiagnosticInfo &Info) { |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 117 | |
| 118 | // Create a PathDiagnostic with a single piece. |
| 119 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 120 | PathDiagnostic* D = new PathDiagnostic(); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 121 | |
Chris Lattner | e837f93 | 2008-11-18 04:44:58 +0000 | [diff] [blame] | 122 | const char *LevelStr; |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 123 | switch (DiagLevel) { |
Mike Stump | e87f5c1 | 2009-02-07 03:46:08 +0000 | [diff] [blame] | 124 | default: |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 125 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
Chris Lattner | e837f93 | 2008-11-18 04:44:58 +0000 | [diff] [blame] | 126 | case Diagnostic::Note: LevelStr = "note: "; break; |
| 127 | case Diagnostic::Warning: LevelStr = "warning: "; break; |
| 128 | case Diagnostic::Error: LevelStr = "error: "; break; |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 129 | case Diagnostic::Fatal: LevelStr = "fatal error: "; break; |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 130 | } |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 131 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 132 | llvm::SmallString<100> StrC; |
| 133 | StrC += LevelStr; |
| 134 | Info.FormatDiagnostic(StrC); |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 136 | PathDiagnosticPiece *P = |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 137 | new PathDiagnosticEventPiece(Info.getLocation(), |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 138 | std::string(StrC.begin(), StrC.end())); |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 140 | for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) |
| 141 | P->addRange(Info.getRange(i)); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 142 | for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i) |
| 143 | P->addCodeModificationHint(Info.getCodeModificationHint(i)); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 144 | D->push_front(P); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 146 | HandlePathDiagnostic(D); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 147 | } |