blob: 4fa88ed595b4a2f1d97f5347c18048843e98a249 [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"
Chris Lattnerf4c83962008-11-19 06:51:40 +000015#include "llvm/ADT/SmallString.h"
Ted Kremenek120187d2008-03-27 06:16:40 +000016#include <sstream>
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000017using namespace clang;
18
19PathDiagnostic::~PathDiagnostic() {
20 for (iterator I = begin(), E = end(); I != E; ++I) delete &*I;
21}
22
Chris Lattner0a14eee2008-11-18 07:04:44 +000023void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
24 const DiagnosticInfo &Info) {
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000025
26 // Create a PathDiagnostic with a single piece.
27
Ted Kremenek55851142008-04-22 16:15:03 +000028 PathDiagnostic* D = new PathDiagnostic();
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000029
Chris Lattnere837f932008-11-18 04:44:58 +000030 const char *LevelStr;
Ted Kremenek120187d2008-03-27 06:16:40 +000031 switch (DiagLevel) {
Mike Stumpe87f5c12009-02-07 03:46:08 +000032 default:
Chris Lattner41327582009-02-06 03:57:44 +000033 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Chris Lattnere837f932008-11-18 04:44:58 +000034 case Diagnostic::Note: LevelStr = "note: "; break;
35 case Diagnostic::Warning: LevelStr = "warning: "; break;
36 case Diagnostic::Error: LevelStr = "error: "; break;
Chris Lattner41327582009-02-06 03:57:44 +000037 case Diagnostic::Fatal: LevelStr = "fatal error: "; break;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000038 }
Ted Kremenek120187d2008-03-27 06:16:40 +000039
Chris Lattnerf4c83962008-11-19 06:51:40 +000040 llvm::SmallString<100> StrC;
41 StrC += LevelStr;
42 Info.FormatDiagnostic(StrC);
Ted Kremenek120187d2008-03-27 06:16:40 +000043
Chris Lattner0a14eee2008-11-18 07:04:44 +000044 PathDiagnosticPiece *P =
Chris Lattnerf4c83962008-11-19 06:51:40 +000045 new PathDiagnosticPiece(Info.getLocation(),
46 std::string(StrC.begin(), StrC.end()));
Ted Kremenek120187d2008-03-27 06:16:40 +000047
Chris Lattner0a14eee2008-11-18 07:04:44 +000048 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i)
49 P->addRange(Info.getRange(i));
Ted Kremenek55851142008-04-22 16:15:03 +000050 D->push_front(P);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000051
Ted Kremenek120187d2008-03-27 06:16:40 +000052 HandlePathDiagnostic(D);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000053}