hanchenye-llvm-project/clang/lib/Frontend/TextDiagnosticBuffer.cpp

49 lines
1.9 KiB
C++
Raw Normal View History

//===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is a concrete diagnostic client, which buffers the diagnostic messages.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/TextDiagnosticBuffer.h"
#include "llvm/ADT/SmallString.h"
using namespace clang;
/// HandleDiagnostic - Store the errors, warnings, and notes that are
/// reported.
///
This reworks some of the Diagnostic interfaces a bit to change how diagnostics are formed. In particular, a diagnostic with all its strings and ranges is now packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a ton of random stuff. This has the benefit of simplifying the interface, making it more extensible, and allowing us to do more checking for things like access past the end of the various arrays passed in. In addition to introducing DiagnosticInfo, this also substantially changes how Diagnostic::Report works. Instead of being passed in all of the info required to issue a diagnostic, Report now takes only the required info (a location and ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to stuff strings and ranges into the DiagnosticInfo with the << operator. When the dtor runs on the DiagnosticInfo object (which should happen at the end of the statement), the diagnostic is actually emitted with all of the accumulated information. This is a somewhat tricky dance, but it means that the accumulated DiagnosticInfo is allowed to keep pointers to other expression temporaries without those pointers getting invalidated. This is just the minimal change to get this stuff working, but this will allow us to eliminate the zillions of variant "Diag" methods scattered throughout (e.g.) sema. For example, instead of calling: Diag(BuiltinLoc, diag::err_overload_no_match, typeNames, SourceRange(BuiltinLoc, RParenLoc)); We will soon be able to just do: Diag(BuiltinLoc, diag::err_overload_no_match) << typeNames << SourceRange(BuiltinLoc, RParenLoc)); This scales better to support arbitrary types being passed in (not just strings) in a type-safe way. Go operator overloading?! llvm-svn: 59502
2008-11-18 15:04:44 +08:00
void TextDiagnosticBuffer::HandleDiagnostic(Diagnostic::Level Level,
const DiagnosticInfo &Info) {
2009-11-30 04:58:39 +08:00
llvm::SmallString<100> Buf;
Info.FormatDiagnostic(Buf);
switch (Level) {
default: assert(0 && "Diagnostic not handled during diagnostic buffering!");
case Diagnostic::Note:
2009-11-30 04:58:39 +08:00
Notes.push_back(std::make_pair(Info.getLocation(), Buf.str()));
break;
case Diagnostic::Warning:
2009-11-30 04:58:39 +08:00
Warnings.push_back(std::make_pair(Info.getLocation(), Buf.str()));
break;
case Diagnostic::Error:
2009-02-18 01:32:22 +08:00
case Diagnostic::Fatal:
2009-11-30 04:58:39 +08:00
Errors.push_back(std::make_pair(Info.getLocation(), Buf.str()));
break;
}
}
void TextDiagnosticBuffer::FlushDiagnostics(Diagnostic &Diags) const {
// FIXME: Flush the diagnostics in order.
for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it)
Diags.Report(Diags.getCustomDiagID(Diagnostic::Error, it->second.c_str()));
for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
Diags.Report(Diags.getCustomDiagID(Diagnostic::Warning,it->second.c_str()));
for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
Diags.Report(Diags.getCustomDiagID(Diagnostic::Note, it->second.c_str()));
}