blob: 288507310baab3cdd3157664d9c8b20d84048775 [file] [log] [blame]
Bill Wendling469211a2007-06-27 03:19:45 +00001//===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling469211a2007-06-27 03:19:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is a concrete diagnostic client, which buffers the diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar51adf582009-03-02 06:16:29 +000014#include "clang/Frontend/TextDiagnosticBuffer.h"
Chris Lattner23be0672008-11-19 06:51:40 +000015#include "llvm/ADT/SmallString.h"
David Blaikie79000202011-09-23 05:57:42 +000016#include "llvm/Support/ErrorHandling.h"
Bill Wendling469211a2007-06-27 03:19:45 +000017using namespace clang;
18
Douglas Gregor39c16d42008-10-24 04:54:22 +000019/// HandleDiagnostic - Store the errors, warnings, and notes that are
20/// reported.
Mike Stump11289f42009-09-09 15:08:12 +000021///
David Blaikie9c902b52011-09-25 23:23:43 +000022void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +000023 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +000024 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +000025 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +000026
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000027 SmallString<100> Buf;
Daniel Dunbarecd04442009-11-29 20:58:39 +000028 Info.FormatDiagnostic(Buf);
Bill Wendling469211a2007-06-27 03:19:45 +000029 switch (Level) {
David Blaikie83d382b2011-09-23 05:06:16 +000030 default: llvm_unreachable(
31 "Diagnostic not handled during diagnostic buffering!");
David Blaikie9c902b52011-09-25 23:23:43 +000032 case DiagnosticsEngine::Note:
Hal Finkelcc152192017-12-16 01:40:19 +000033 All.emplace_back(Level, Notes.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000034 Notes.emplace_back(Info.getLocation(), Buf.str());
Douglas Gregore972aa42008-09-11 02:46:36 +000035 break;
David Blaikie9c902b52011-09-25 23:23:43 +000036 case DiagnosticsEngine::Warning:
Hal Finkelcc152192017-12-16 01:40:19 +000037 All.emplace_back(Level, Warnings.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000038 Warnings.emplace_back(Info.getLocation(), Buf.str());
Bill Wendling469211a2007-06-27 03:19:45 +000039 break;
Tobias Grosser86a85672014-05-01 14:06:01 +000040 case DiagnosticsEngine::Remark:
Hal Finkelcc152192017-12-16 01:40:19 +000041 All.emplace_back(Level, Remarks.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000042 Remarks.emplace_back(Info.getLocation(), Buf.str());
Tobias Grosser86a85672014-05-01 14:06:01 +000043 break;
David Blaikie9c902b52011-09-25 23:23:43 +000044 case DiagnosticsEngine::Error:
45 case DiagnosticsEngine::Fatal:
Hal Finkelcc152192017-12-16 01:40:19 +000046 All.emplace_back(Level, Errors.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000047 Errors.emplace_back(Info.getLocation(), Buf.str());
Bill Wendling469211a2007-06-27 03:19:45 +000048 break;
49 }
50}
Daniel Dunbar05762b12009-11-30 08:41:34 +000051
David Blaikie9c902b52011-09-25 23:23:43 +000052void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
Hal Finkelcc152192017-12-16 01:40:19 +000053 for (auto it = All.begin(), ie = All.end(); it != ie; ++it) {
54 auto Diag = Diags.Report(Diags.getCustomDiagID(it->first, "%0"));
55 switch (it->first) {
56 default: llvm_unreachable(
57 "Diagnostic not handled during diagnostic flushing!");
58 case DiagnosticsEngine::Note:
59 Diag << Notes[it->second].second;
60 break;
61 case DiagnosticsEngine::Warning:
62 Diag << Warnings[it->second].second;
63 break;
64 case DiagnosticsEngine::Remark:
65 Diag << Remarks[it->second].second;
66 break;
67 case DiagnosticsEngine::Error:
68 case DiagnosticsEngine::Fatal:
69 Diag << Errors[it->second].second;
70 break;
71 }
72 }
Daniel Dunbar05762b12009-11-30 08:41:34 +000073}
Douglas Gregord0e9e3a2011-09-29 00:38:00 +000074