2007-06-27 11:19:45 +08:00
|
|
|
//===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-27 11:19:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a concrete diagnostic client, which buffers the diagnostic messages.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-02 14:16:29 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticBuffer.h"
|
2008-11-19 14:51:40 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-09-23 13:57:42 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2007-06-27 11:19:45 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-10-24 12:54:22 +08:00
|
|
|
/// HandleDiagnostic - Store the errors, warnings, and notes that are
|
|
|
|
/// reported.
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2011-09-26 07:23:43 +08:00
|
|
|
void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
|
2011-09-26 09:18:08 +08:00
|
|
|
const Diagnostic &Info) {
|
2010-11-19 04:06:46 +08:00
|
|
|
// Default implementation (Warnings/errors count).
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer::HandleDiagnostic(Level, Info);
|
2010-11-19 04:06:46 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<100> Buf;
|
2009-11-30 04:58:39 +08:00
|
|
|
Info.FormatDiagnostic(Buf);
|
2007-06-27 11:19:45 +08:00
|
|
|
switch (Level) {
|
2011-09-23 13:06:16 +08:00
|
|
|
default: llvm_unreachable(
|
|
|
|
"Diagnostic not handled during diagnostic buffering!");
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::Note:
|
2009-11-30 04:58:39 +08:00
|
|
|
Notes.push_back(std::make_pair(Info.getLocation(), Buf.str()));
|
2008-09-11 10:46:36 +08:00
|
|
|
break;
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::Warning:
|
2009-11-30 04:58:39 +08:00
|
|
|
Warnings.push_back(std::make_pair(Info.getLocation(), Buf.str()));
|
2007-06-27 11:19:45 +08:00
|
|
|
break;
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::Error:
|
|
|
|
case DiagnosticsEngine::Fatal:
|
2009-11-30 04:58:39 +08:00
|
|
|
Errors.push_back(std::make_pair(Info.getLocation(), Buf.str()));
|
2007-06-27 11:19:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-30 16:41:34 +08:00
|
|
|
|
2012-12-08 20:42:30 +08:00
|
|
|
/// \brief Escape diagnostic texts to avoid problems when they are fed into the
|
|
|
|
/// diagnostic formatter a second time.
|
|
|
|
static StringRef escapeDiag(StringRef Str, SmallVectorImpl<char> &Buf) {
|
|
|
|
size_t Pos = Str.find('%');
|
|
|
|
if (Pos == StringRef::npos)
|
|
|
|
return Str;
|
|
|
|
|
|
|
|
// We found a '%'. Replace this and all following '%' with '%%'.
|
|
|
|
Buf.clear();
|
|
|
|
Buf.append(Str.data(), Str.data() + Pos);
|
|
|
|
for (size_t I = Pos, E = Str.size(); I != E; ++I) {
|
|
|
|
if (Str[I] == '%')
|
|
|
|
Buf.push_back('%');
|
|
|
|
Buf.push_back(Str[I]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return StringRef(Buf.data(), Buf.size());
|
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
|
2012-12-08 20:42:30 +08:00
|
|
|
SmallVector<char, 64> Buf;
|
2009-11-30 16:41:34 +08:00
|
|
|
// FIXME: Flush the diagnostics in order.
|
|
|
|
for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it)
|
2011-09-26 07:23:43 +08:00
|
|
|
Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error,
|
2012-12-08 20:42:30 +08:00
|
|
|
escapeDiag(it->second, Buf)));
|
2009-11-30 16:41:34 +08:00
|
|
|
for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
|
2011-09-26 07:23:43 +08:00
|
|
|
Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning,
|
2012-12-08 20:42:30 +08:00
|
|
|
escapeDiag(it->second, Buf)));
|
2009-11-30 16:41:34 +08:00
|
|
|
for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
|
2011-09-26 07:23:43 +08:00
|
|
|
Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note,
|
2012-12-08 20:42:30 +08:00
|
|
|
escapeDiag(it->second, Buf)));
|
2009-11-30 16:41:34 +08:00
|
|
|
}
|
2011-09-29 08:38:00 +08:00
|
|
|
|