blob: 57105f15a30af9b5def98d4936dda803a4c91a2d [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is a concrete diagnostic client, which buffers the diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/TextDiagnosticBuffer.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000015#include "llvm/ADT/SmallString.h"
David Blaikie548f6c82011-09-23 05:57:42 +000016#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using namespace clang;
18
Douglas Gregor94b1dd22008-10-24 04:54:22 +000019/// HandleDiagnostic - Store the errors, warnings, and notes that are
20/// reported.
Mike Stump1eb44332009-09-09 15:08:12 +000021///
David Blaikied6471f72011-09-25 23:23:43 +000022void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +000023 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +000024 // Default implementation (Warnings/errors count).
David Blaikie78ad0b92011-09-25 23:39:51 +000025 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +000026
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000027 SmallString<100> Buf;
Daniel Dunbar4cbe3b62009-11-29 20:58:39 +000028 Info.FormatDiagnostic(Buf);
Reid Spencer5f016e22007-07-11 17:01:13 +000029 switch (Level) {
David Blaikieb219cfc2011-09-23 05:06:16 +000030 default: llvm_unreachable(
31 "Diagnostic not handled during diagnostic buffering!");
David Blaikied6471f72011-09-25 23:23:43 +000032 case DiagnosticsEngine::Note:
Daniel Dunbar4cbe3b62009-11-29 20:58:39 +000033 Notes.push_back(std::make_pair(Info.getLocation(), Buf.str()));
Douglas Gregor233f74b2008-09-11 02:46:36 +000034 break;
David Blaikied6471f72011-09-25 23:23:43 +000035 case DiagnosticsEngine::Warning:
Daniel Dunbar4cbe3b62009-11-29 20:58:39 +000036 Warnings.push_back(std::make_pair(Info.getLocation(), Buf.str()));
Reid Spencer5f016e22007-07-11 17:01:13 +000037 break;
David Blaikied6471f72011-09-25 23:23:43 +000038 case DiagnosticsEngine::Error:
39 case DiagnosticsEngine::Fatal:
Daniel Dunbar4cbe3b62009-11-29 20:58:39 +000040 Errors.push_back(std::make_pair(Info.getLocation(), Buf.str()));
Reid Spencer5f016e22007-07-11 17:01:13 +000041 break;
42 }
43}
Daniel Dunbarc2389552009-11-30 08:41:34 +000044
David Blaikied6471f72011-09-25 23:23:43 +000045void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
Daniel Dunbarc2389552009-11-30 08:41:34 +000046 // FIXME: Flush the diagnostics in order.
47 for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it)
David Blaikied6471f72011-09-25 23:23:43 +000048 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error,
49 it->second.c_str()));
Daniel Dunbarc2389552009-11-30 08:41:34 +000050 for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
David Blaikied6471f72011-09-25 23:23:43 +000051 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning,
52 it->second.c_str()));
Daniel Dunbarc2389552009-11-30 08:41:34 +000053 for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
David Blaikied6471f72011-09-25 23:23:43 +000054 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note,
55 it->second.c_str()));
Daniel Dunbarc2389552009-11-30 08:41:34 +000056}
Douglas Gregoraee526e2011-09-29 00:38:00 +000057
58DiagnosticConsumer *TextDiagnosticBuffer::clone(DiagnosticsEngine &) const {
59 return new TextDiagnosticBuffer();
60}