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) |
| 39 | : Pos(pos), str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {} |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 40 | |
| 41 | PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos, |
Ted Kremenek | e3ce265 | 2009-03-02 19:39:50 +0000 | [diff] [blame] | 42 | const char* s, Kind k, |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 43 | DisplayHint hint) |
Ted Kremenek | e3ce265 | 2009-03-02 19:39:50 +0000 | [diff] [blame] | 44 | : Pos(pos), str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {} |
Ted Kremenek | 8af2975 | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 46 | PathDiagnostic::~PathDiagnostic() { |
| 47 | for (iterator I = begin(), E = end(); I != E; ++I) delete &*I; |
| 48 | } |
| 49 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 50 | void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 51 | const DiagnosticInfo &Info) { |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 52 | |
| 53 | // Create a PathDiagnostic with a single piece. |
| 54 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 55 | PathDiagnostic* D = new PathDiagnostic(); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 56 | |
Chris Lattner | e837f93 | 2008-11-18 04:44:58 +0000 | [diff] [blame] | 57 | const char *LevelStr; |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 58 | switch (DiagLevel) { |
Mike Stump | e87f5c1 | 2009-02-07 03:46:08 +0000 | [diff] [blame] | 59 | default: |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 60 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
Chris Lattner | e837f93 | 2008-11-18 04:44:58 +0000 | [diff] [blame] | 61 | case Diagnostic::Note: LevelStr = "note: "; break; |
| 62 | case Diagnostic::Warning: LevelStr = "warning: "; break; |
| 63 | case Diagnostic::Error: LevelStr = "error: "; break; |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 64 | case Diagnostic::Fatal: LevelStr = "fatal error: "; break; |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 65 | } |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 66 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 67 | llvm::SmallString<100> StrC; |
| 68 | StrC += LevelStr; |
| 69 | Info.FormatDiagnostic(StrC); |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 71 | PathDiagnosticPiece *P = |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 72 | new PathDiagnosticPiece(Info.getLocation(), |
| 73 | std::string(StrC.begin(), StrC.end())); |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 75 | for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) |
| 76 | P->addRange(Info.getRange(i)); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 77 | for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i) |
| 78 | P->addCodeModificationHint(Info.getCodeModificationHint(i)); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 79 | D->push_front(P); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame] | 81 | HandlePathDiagnostic(D); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 82 | } |