144d362409d5469aed47d19e7908d19bd194493aThomas Graf//===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
244d362409d5469aed47d19e7908d19bd194493aThomas Graf//
344d362409d5469aed47d19e7908d19bd194493aThomas Graf//                     The LLVM Compiler Infrastructure
444d362409d5469aed47d19e7908d19bd194493aThomas Graf//
544d362409d5469aed47d19e7908d19bd194493aThomas Graf// This file is distributed under the University of Illinois Open Source
644d362409d5469aed47d19e7908d19bd194493aThomas Graf// License. See LICENSE.TXT for details.
744d362409d5469aed47d19e7908d19bd194493aThomas Graf//
844d362409d5469aed47d19e7908d19bd194493aThomas Graf//===----------------------------------------------------------------------===//
98a3efffa5b3fde252675239914118664d36a2c24Thomas Graf//
1044d362409d5469aed47d19e7908d19bd194493aThomas Graf// This is a concrete diagnostic client, which buffers the diagnostic messages.
1144d362409d5469aed47d19e7908d19bd194493aThomas Graf//
1244d362409d5469aed47d19e7908d19bd194493aThomas Graf//===----------------------------------------------------------------------===//
1344d362409d5469aed47d19e7908d19bd194493aThomas Graf
1444d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "clang/Frontend/TextDiagnosticBuffer.h"
1544d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "llvm/ADT/SmallString.h"
1644d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "llvm/Support/ErrorHandling.h"
1744d362409d5469aed47d19e7908d19bd194493aThomas Grafusing namespace clang;
1844d362409d5469aed47d19e7908d19bd194493aThomas Graf
1944d362409d5469aed47d19e7908d19bd194493aThomas Graf/// HandleDiagnostic - Store the errors, warnings, and notes that are
2044d362409d5469aed47d19e7908d19bd194493aThomas Graf/// reported.
2144d362409d5469aed47d19e7908d19bd194493aThomas Graf///
2244d362409d5469aed47d19e7908d19bd194493aThomas Grafvoid TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
2344d362409d5469aed47d19e7908d19bd194493aThomas Graf                                            const Diagnostic &Info) {
2444d362409d5469aed47d19e7908d19bd194493aThomas Graf  // Default implementation (Warnings/errors count).
2544d362409d5469aed47d19e7908d19bd194493aThomas Graf  DiagnosticConsumer::HandleDiagnostic(Level, Info);
2644d362409d5469aed47d19e7908d19bd194493aThomas Graf
2744d362409d5469aed47d19e7908d19bd194493aThomas Graf  SmallString<100> Buf;
2844d362409d5469aed47d19e7908d19bd194493aThomas Graf  Info.FormatDiagnostic(Buf);
2944d362409d5469aed47d19e7908d19bd194493aThomas Graf  switch (Level) {
3044d362409d5469aed47d19e7908d19bd194493aThomas Graf  default: llvm_unreachable(
3144d362409d5469aed47d19e7908d19bd194493aThomas Graf                         "Diagnostic not handled during diagnostic buffering!");
3244d362409d5469aed47d19e7908d19bd194493aThomas Graf  case DiagnosticsEngine::Note:
3344d362409d5469aed47d19e7908d19bd194493aThomas Graf    Notes.push_back(std::make_pair(Info.getLocation(), Buf.str()));
3444d362409d5469aed47d19e7908d19bd194493aThomas Graf    break;
3544d362409d5469aed47d19e7908d19bd194493aThomas Graf  case DiagnosticsEngine::Warning:
3644d362409d5469aed47d19e7908d19bd194493aThomas Graf    Warnings.push_back(std::make_pair(Info.getLocation(), Buf.str()));
3744d362409d5469aed47d19e7908d19bd194493aThomas Graf    break;
3844d362409d5469aed47d19e7908d19bd194493aThomas Graf  case DiagnosticsEngine::Remark:
3944d362409d5469aed47d19e7908d19bd194493aThomas Graf    Remarks.push_back(std::make_pair(Info.getLocation(), Buf.str()));
4044d362409d5469aed47d19e7908d19bd194493aThomas Graf    break;
4144d362409d5469aed47d19e7908d19bd194493aThomas Graf  case DiagnosticsEngine::Error:
4244d362409d5469aed47d19e7908d19bd194493aThomas Graf  case DiagnosticsEngine::Fatal:
4344d362409d5469aed47d19e7908d19bd194493aThomas Graf    Errors.push_back(std::make_pair(Info.getLocation(), Buf.str()));
4444d362409d5469aed47d19e7908d19bd194493aThomas Graf    break;
4544d362409d5469aed47d19e7908d19bd194493aThomas Graf  }
4644d362409d5469aed47d19e7908d19bd194493aThomas Graf}
4744d362409d5469aed47d19e7908d19bd194493aThomas Graf
4844d362409d5469aed47d19e7908d19bd194493aThomas Grafvoid TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
491155370f520cb64657e25153255cf7dc1424317fThomas Graf  // FIXME: Flush the diagnostics in order.
5044d362409d5469aed47d19e7908d19bd194493aThomas Graf  for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it)
5144d362409d5469aed47d19e7908d19bd194493aThomas Graf    Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
5244d362409d5469aed47d19e7908d19bd194493aThomas Graf        << it->second;
5344d362409d5469aed47d19e7908d19bd194493aThomas Graf  for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
5444d362409d5469aed47d19e7908d19bd194493aThomas Graf    Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning, "%0"))
5544d362409d5469aed47d19e7908d19bd194493aThomas Graf        << it->second;
5644d362409d5469aed47d19e7908d19bd194493aThomas Graf  for (const_iterator it = remark_begin(), ie = remark_end(); it != ie; ++it)
5744d362409d5469aed47d19e7908d19bd194493aThomas Graf    Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Remark, "%0"))
5844d362409d5469aed47d19e7908d19bd194493aThomas Graf        << it->second;
591155370f520cb64657e25153255cf7dc1424317fThomas Graf  for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
6044d362409d5469aed47d19e7908d19bd194493aThomas Graf    Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
6144d362409d5469aed47d19e7908d19bd194493aThomas Graf        << it->second;
6244d362409d5469aed47d19e7908d19bd194493aThomas Graf}
6344d362409d5469aed47d19e7908d19bd194493aThomas Graf
6444d362409d5469aed47d19e7908d19bd194493aThomas Graf