blob: e4228c77443c6442fb727b5fa36e5193a25b3730 [file] [log] [blame]
Ted Kremenekd3abcdf2008-03-27 03:49:32 +00001//===--- 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 Kremenek120187d2008-03-27 06:16:40 +000015#include <sstream>
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000016
17using namespace clang;
18
19PathDiagnostic::~PathDiagnostic() {
20 for (iterator I = begin(), E = end(); I != E; ++I) delete &*I;
21}
22
23void 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 Kremenek120187d2008-03-27 06:16:40 +000034 PathDiagnostic D;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000035
Ted Kremenek120187d2008-03-27 06:16:40 +000036 // Ripped from TextDiagnostics::FormatDiagnostic. Perhaps we should
37 // centralize it somewhere?
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000038
Ted Kremenek120187d2008-03-27 06:16:40 +000039 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 Kremenekd3abcdf2008-03-27 03:49:32 +000048 }
49
Ted Kremenek120187d2008-03-27 06:16:40 +000050 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 Kremenekd3abcdf2008-03-27 03:49:32 +000065 while (NumRanges) {
66 P->addRange(*Ranges);
67 --NumRanges;
68 ++Ranges;
69 }
70
71 D.push_front(P);
72
Ted Kremenek120187d2008-03-27 06:16:40 +000073 HandlePathDiagnostic(D);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000074}