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