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 | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame^] | 15 | #include <sstream> |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang; |
| 18 | |
| 19 | PathDiagnostic::~PathDiagnostic() { |
| 20 | for (iterator I = begin(), E = end(); I != E; ++I) delete &*I; |
| 21 | } |
| 22 | |
| 23 | void PathDiagnosticClient::HandleDiagnostic(Diagnostic &Diags, |
| 24 | Diagnostic::Level DiagLevel, |
| 25 | FullSourceLoc Pos, |
| 26 | diag::kind ID, |
| 27 | const std::string *Strs, |
| 28 | unsigned NumStrs, |
| 29 | const SourceRange *Ranges, |
| 30 | unsigned NumRanges) { |
| 31 | |
| 32 | // Create a PathDiagnostic with a single piece. |
| 33 | |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame^] | 34 | PathDiagnostic D; |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame^] | 36 | // Ripped from TextDiagnostics::FormatDiagnostic. Perhaps we should |
| 37 | // centralize it somewhere? |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame^] | 39 | std::ostringstream os; |
| 40 | |
| 41 | switch (DiagLevel) { |
| 42 | default: assert(0 && "Unknown diagnostic type!"); |
| 43 | case Diagnostic::Note: os << "note: "; break; |
| 44 | case Diagnostic::Warning: os << "warning: "; break; |
| 45 | case Diagnostic::Error: os << "error: "; break; |
| 46 | case Diagnostic::Fatal: os << "fatal error: "; break; |
| 47 | break; |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame^] | 50 | std::string Msg = Diags.getDescription(ID); |
| 51 | |
| 52 | for (unsigned i = 0; i < Msg.size() - 1; ++i) { |
| 53 | if (Msg[i] == '%' && isdigit(Msg[i + 1])) { |
| 54 | unsigned StrNo = Msg[i + 1] - '0'; |
| 55 | Msg = std::string(Msg.begin(), Msg.begin() + i) + |
| 56 | (StrNo < NumStrs ? Strs[StrNo] : "<<<INTERNAL ERROR>>>") + |
| 57 | std::string(Msg.begin() + i + 2, Msg.end()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | os << Msg; |
| 62 | |
| 63 | PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str()); |
| 64 | |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 65 | while (NumRanges) { |
| 66 | P->addRange(*Ranges); |
| 67 | --NumRanges; |
| 68 | ++Ranges; |
| 69 | } |
| 70 | |
| 71 | D.push_front(P); |
| 72 | |
Ted Kremenek | 120187d | 2008-03-27 06:16:40 +0000 | [diff] [blame^] | 73 | HandlePathDiagnostic(D); |
Ted Kremenek | d3abcdf | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 74 | } |