blob: 44bb2bc29bc093985afae8be9ee391b62abd46d1 [file] [log] [blame]
Eugene Zelenko4f233182018-03-22 00:53:26 +00001//===- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics -----------------===//
Bill Wendling469211a2007-06-27 03:19:45 +00002//
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"
Eugene Zelenko4f233182018-03-22 00:53:26 +000015#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/LLVM.h"
Chris Lattner23be0672008-11-19 06:51:40 +000017#include "llvm/ADT/SmallString.h"
David Blaikie79000202011-09-23 05:57:42 +000018#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko4f233182018-03-22 00:53:26 +000019
Bill Wendling469211a2007-06-27 03:19:45 +000020using namespace clang;
21
Douglas Gregor39c16d42008-10-24 04:54:22 +000022/// HandleDiagnostic - Store the errors, warnings, and notes that are
23/// reported.
David Blaikie9c902b52011-09-25 23:23:43 +000024void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +000025 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +000026 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +000027 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +000028
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000029 SmallString<100> Buf;
Daniel Dunbarecd04442009-11-29 20:58:39 +000030 Info.FormatDiagnostic(Buf);
Bill Wendling469211a2007-06-27 03:19:45 +000031 switch (Level) {
David Blaikie83d382b2011-09-23 05:06:16 +000032 default: llvm_unreachable(
33 "Diagnostic not handled during diagnostic buffering!");
David Blaikie9c902b52011-09-25 23:23:43 +000034 case DiagnosticsEngine::Note:
Hal Finkelcc152192017-12-16 01:40:19 +000035 All.emplace_back(Level, Notes.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000036 Notes.emplace_back(Info.getLocation(), Buf.str());
Douglas Gregore972aa42008-09-11 02:46:36 +000037 break;
David Blaikie9c902b52011-09-25 23:23:43 +000038 case DiagnosticsEngine::Warning:
Hal Finkelcc152192017-12-16 01:40:19 +000039 All.emplace_back(Level, Warnings.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000040 Warnings.emplace_back(Info.getLocation(), Buf.str());
Bill Wendling469211a2007-06-27 03:19:45 +000041 break;
Tobias Grosser86a85672014-05-01 14:06:01 +000042 case DiagnosticsEngine::Remark:
Hal Finkelcc152192017-12-16 01:40:19 +000043 All.emplace_back(Level, Remarks.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000044 Remarks.emplace_back(Info.getLocation(), Buf.str());
Tobias Grosser86a85672014-05-01 14:06:01 +000045 break;
David Blaikie9c902b52011-09-25 23:23:43 +000046 case DiagnosticsEngine::Error:
47 case DiagnosticsEngine::Fatal:
Hal Finkelcc152192017-12-16 01:40:19 +000048 All.emplace_back(Level, Errors.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000049 Errors.emplace_back(Info.getLocation(), Buf.str());
Bill Wendling469211a2007-06-27 03:19:45 +000050 break;
51 }
52}
Daniel Dunbar05762b12009-11-30 08:41:34 +000053
David Blaikie9c902b52011-09-25 23:23:43 +000054void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
Eugene Zelenko4f233182018-03-22 00:53:26 +000055 for (const auto &I : All) {
56 auto Diag = Diags.Report(Diags.getCustomDiagID(I.first, "%0"));
57 switch (I.first) {
Hal Finkelcc152192017-12-16 01:40:19 +000058 default: llvm_unreachable(
59 "Diagnostic not handled during diagnostic flushing!");
60 case DiagnosticsEngine::Note:
Eugene Zelenko4f233182018-03-22 00:53:26 +000061 Diag << Notes[I.second].second;
Hal Finkelcc152192017-12-16 01:40:19 +000062 break;
63 case DiagnosticsEngine::Warning:
Eugene Zelenko4f233182018-03-22 00:53:26 +000064 Diag << Warnings[I.second].second;
Hal Finkelcc152192017-12-16 01:40:19 +000065 break;
66 case DiagnosticsEngine::Remark:
Eugene Zelenko4f233182018-03-22 00:53:26 +000067 Diag << Remarks[I.second].second;
Hal Finkelcc152192017-12-16 01:40:19 +000068 break;
69 case DiagnosticsEngine::Error:
70 case DiagnosticsEngine::Fatal:
Eugene Zelenko4f233182018-03-22 00:53:26 +000071 Diag << Errors[I.second].second;
Hal Finkelcc152192017-12-16 01:40:19 +000072 break;
73 }
74 }
Daniel Dunbar05762b12009-11-30 08:41:34 +000075}