1 //===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a concrete diagnostic client, which buffers the diagnostic messages.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/TextDiagnosticBuffer.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/ErrorHandling.h"
17 using namespace clang;
19 /// HandleDiagnostic - Store the errors, warnings, and notes that are
22 void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
23 const Diagnostic &Info) {
24 // Default implementation (Warnings/errors count).
25 DiagnosticConsumer::HandleDiagnostic(Level, Info);
28 Info.FormatDiagnostic(Buf);
30 default: llvm_unreachable(
31 "Diagnostic not handled during diagnostic buffering!");
32 case DiagnosticsEngine::Note:
33 Notes.push_back(std::make_pair(Info.getLocation(), Buf.str()));
35 case DiagnosticsEngine::Warning:
36 Warnings.push_back(std::make_pair(Info.getLocation(), Buf.str()));
38 case DiagnosticsEngine::Error:
39 case DiagnosticsEngine::Fatal:
40 Errors.push_back(std::make_pair(Info.getLocation(), Buf.str()));
45 /// \brief Escape diagnostic texts to avoid problems when they are fed into the
46 /// diagnostic formatter a second time.
47 static StringRef escapeDiag(StringRef Str, SmallVectorImpl<char> &Buf) {
48 size_t Pos = Str.find('%');
49 if (Pos == StringRef::npos)
52 // We found a '%'. Replace this and all following '%' with '%%'.
54 Buf.append(Str.data(), Str.data() + Pos);
55 for (size_t I = Pos, E = Str.size(); I != E; ++I) {
58 Buf.push_back(Str[I]);
61 return StringRef(Buf.data(), Buf.size());
64 void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
65 SmallVector<char, 64> Buf;
66 // FIXME: Flush the diagnostics in order.
67 for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it)
68 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error,
69 escapeDiag(it->second, Buf)));
70 for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
71 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning,
72 escapeDiag(it->second, Buf)));
73 for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
74 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note,
75 escapeDiag(it->second, Buf)));