blob: b2497f56cbcd28de562107b07c8b6c45f2a7f5e0 [file] [log] [blame]
Eugene Zelenko4f233182018-03-22 00:53:26 +00001//===- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics -----------------===//
Bill Wendling469211a2007-06-27 03:19:45 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Bill Wendling469211a2007-06-27 03:19:45 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This is a concrete diagnostic client, which buffers the diagnostic messages.
10//
11//===----------------------------------------------------------------------===//
12
Daniel Dunbar51adf582009-03-02 06:16:29 +000013#include "clang/Frontend/TextDiagnosticBuffer.h"
Eugene Zelenko4f233182018-03-22 00:53:26 +000014#include "clang/Basic/Diagnostic.h"
15#include "clang/Basic/LLVM.h"
Chris Lattner23be0672008-11-19 06:51:40 +000016#include "llvm/ADT/SmallString.h"
David Blaikie79000202011-09-23 05:57:42 +000017#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko4f233182018-03-22 00:53:26 +000018
Bill Wendling469211a2007-06-27 03:19:45 +000019using namespace clang;
20
Douglas Gregor39c16d42008-10-24 04:54:22 +000021/// HandleDiagnostic - Store the errors, warnings, and notes that are
22/// reported.
David Blaikie9c902b52011-09-25 23:23:43 +000023void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +000024 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +000025 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +000026 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +000027
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000028 SmallString<100> Buf;
Daniel Dunbarecd04442009-11-29 20:58:39 +000029 Info.FormatDiagnostic(Buf);
Bill Wendling469211a2007-06-27 03:19:45 +000030 switch (Level) {
David Blaikie83d382b2011-09-23 05:06:16 +000031 default: llvm_unreachable(
32 "Diagnostic not handled during diagnostic buffering!");
David Blaikie9c902b52011-09-25 23:23:43 +000033 case DiagnosticsEngine::Note:
Hal Finkelcc152192017-12-16 01:40:19 +000034 All.emplace_back(Level, Notes.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000035 Notes.emplace_back(Info.getLocation(), Buf.str());
Douglas Gregore972aa42008-09-11 02:46:36 +000036 break;
David Blaikie9c902b52011-09-25 23:23:43 +000037 case DiagnosticsEngine::Warning:
Hal Finkelcc152192017-12-16 01:40:19 +000038 All.emplace_back(Level, Warnings.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000039 Warnings.emplace_back(Info.getLocation(), Buf.str());
Bill Wendling469211a2007-06-27 03:19:45 +000040 break;
Tobias Grosser86a85672014-05-01 14:06:01 +000041 case DiagnosticsEngine::Remark:
Hal Finkelcc152192017-12-16 01:40:19 +000042 All.emplace_back(Level, Remarks.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000043 Remarks.emplace_back(Info.getLocation(), Buf.str());
Tobias Grosser86a85672014-05-01 14:06:01 +000044 break;
David Blaikie9c902b52011-09-25 23:23:43 +000045 case DiagnosticsEngine::Error:
46 case DiagnosticsEngine::Fatal:
Hal Finkelcc152192017-12-16 01:40:19 +000047 All.emplace_back(Level, Errors.size());
Benjamin Kramer3204b152015-05-29 19:42:19 +000048 Errors.emplace_back(Info.getLocation(), Buf.str());
Bill Wendling469211a2007-06-27 03:19:45 +000049 break;
50 }
51}
Daniel Dunbar05762b12009-11-30 08:41:34 +000052
David Blaikie9c902b52011-09-25 23:23:43 +000053void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
Eugene Zelenko4f233182018-03-22 00:53:26 +000054 for (const auto &I : All) {
55 auto Diag = Diags.Report(Diags.getCustomDiagID(I.first, "%0"));
56 switch (I.first) {
Hal Finkelcc152192017-12-16 01:40:19 +000057 default: llvm_unreachable(
58 "Diagnostic not handled during diagnostic flushing!");
59 case DiagnosticsEngine::Note:
Eugene Zelenko4f233182018-03-22 00:53:26 +000060 Diag << Notes[I.second].second;
Hal Finkelcc152192017-12-16 01:40:19 +000061 break;
62 case DiagnosticsEngine::Warning:
Eugene Zelenko4f233182018-03-22 00:53:26 +000063 Diag << Warnings[I.second].second;
Hal Finkelcc152192017-12-16 01:40:19 +000064 break;
65 case DiagnosticsEngine::Remark:
Eugene Zelenko4f233182018-03-22 00:53:26 +000066 Diag << Remarks[I.second].second;
Hal Finkelcc152192017-12-16 01:40:19 +000067 break;
68 case DiagnosticsEngine::Error:
69 case DiagnosticsEngine::Fatal:
Eugene Zelenko4f233182018-03-22 00:53:26 +000070 Diag << Errors[I.second].second;
Hal Finkelcc152192017-12-16 01:40:19 +000071 break;
72 }
73 }
Daniel Dunbar05762b12009-11-30 08:41:34 +000074}