2011-04-07 22:31:10 +04:00
|
|
|
//===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/LogDiagnosticPrinter.h"
|
2012-10-24 02:26:28 +04:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2011-04-07 22:37:34 +04:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2011-04-07 22:31:10 +04:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-01-17 06:30:50 +04:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 13:13:33 +04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-04-07 22:31:10 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os,
|
2012-10-24 02:26:28 +04:00
|
|
|
DiagnosticOptions *diags,
|
2011-04-07 22:31:10 +04:00
|
|
|
bool _OwnsOutputStream)
|
2012-10-24 02:26:28 +04:00
|
|
|
: OS(os), LangOpts(0), DiagOpts(diags),
|
2011-04-07 22:31:10 +04:00
|
|
|
OwnsOutputStream(_OwnsOutputStream) {
|
|
|
|
}
|
|
|
|
|
|
|
|
LogDiagnosticPrinter::~LogDiagnosticPrinter() {
|
|
|
|
if (OwnsOutputStream)
|
|
|
|
delete &OS;
|
|
|
|
}
|
|
|
|
|
2011-09-26 03:23:43 +04:00
|
|
|
static StringRef getLevelName(DiagnosticsEngine::Level Level) {
|
2011-04-07 22:37:34 +04:00
|
|
|
switch (Level) {
|
2011-09-26 03:23:43 +04:00
|
|
|
case DiagnosticsEngine::Ignored: return "ignored";
|
|
|
|
case DiagnosticsEngine::Note: return "note";
|
|
|
|
case DiagnosticsEngine::Warning: return "warning";
|
|
|
|
case DiagnosticsEngine::Error: return "error";
|
|
|
|
case DiagnosticsEngine::Fatal: return "fatal error";
|
2011-04-07 22:37:34 +04:00
|
|
|
}
|
2012-01-17 06:30:50 +04:00
|
|
|
llvm_unreachable("Invalid DiagnosticsEngine level!");
|
2011-04-07 22:37:34 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 03:05:07 +04:00
|
|
|
// Escape XML characters inside the raw string.
|
|
|
|
static void emitString(llvm::raw_svector_ostream &OS, const StringRef Raw) {
|
|
|
|
for (StringRef::iterator I = Raw.begin(), E = Raw.end(); I != E; ++I) {
|
|
|
|
char c = *I;
|
|
|
|
switch (c) {
|
|
|
|
default: OS << c; break;
|
|
|
|
case '&': OS << "&"; break;
|
|
|
|
case '<': OS << "<"; break;
|
|
|
|
case '>': OS << ">"; break;
|
|
|
|
case '\'': OS << "'"; break;
|
|
|
|
case '\"': OS << """; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-07 22:37:34 +04:00
|
|
|
void LogDiagnosticPrinter::EndSourceFile() {
|
|
|
|
// We emit all the diagnostics in EndSourceFile. However, we don't emit any
|
|
|
|
// entry if no diagnostics were present.
|
|
|
|
//
|
2011-09-26 03:39:51 +04:00
|
|
|
// Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
|
|
|
|
// will miss any diagnostics which are emitted after and outside the
|
|
|
|
// translation unit processing.
|
2011-04-07 22:37:34 +04:00
|
|
|
if (Entries.empty())
|
|
|
|
return;
|
2011-04-07 22:31:10 +04:00
|
|
|
|
|
|
|
// Write to a temporary string to ensure atomic write of diagnostic object.
|
2012-02-05 06:13:05 +04:00
|
|
|
SmallString<512> Msg;
|
2011-04-07 22:31:10 +04:00
|
|
|
llvm::raw_svector_ostream OS(Msg);
|
|
|
|
|
2011-04-07 22:44:15 +04:00
|
|
|
OS << "<dict>\n";
|
2011-04-07 22:51:54 +04:00
|
|
|
if (!MainFilename.empty()) {
|
|
|
|
OS << " <key>main-file</key>\n"
|
2011-09-29 03:05:07 +04:00
|
|
|
<< " <string>";
|
|
|
|
emitString(OS, MainFilename);
|
|
|
|
OS << "</string>\n";
|
2011-04-07 22:51:54 +04:00
|
|
|
}
|
|
|
|
if (!DwarfDebugFlags.empty()) {
|
|
|
|
OS << " <key>dwarf-debug-flags</key>\n"
|
2011-09-29 03:05:07 +04:00
|
|
|
<< " <string>";
|
|
|
|
emitString(OS, DwarfDebugFlags);
|
|
|
|
OS << "</string>\n";
|
2011-04-07 22:51:54 +04:00
|
|
|
}
|
2011-04-07 22:44:15 +04:00
|
|
|
OS << " <key>diagnostics</key>\n";
|
|
|
|
OS << " <array>\n";
|
2011-04-07 22:37:34 +04:00
|
|
|
for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
|
|
|
|
DiagEntry &DE = Entries[i];
|
|
|
|
|
2011-04-07 22:44:15 +04:00
|
|
|
OS << " <dict>\n";
|
|
|
|
OS << " <key>level</key>\n"
|
2011-09-29 03:05:07 +04:00
|
|
|
<< " <string>";
|
|
|
|
emitString(OS, getLevelName(DE.DiagnosticLevel));
|
|
|
|
OS << "</string>\n";
|
2011-04-07 22:44:15 +04:00
|
|
|
if (!DE.Filename.empty()) {
|
|
|
|
OS << " <key>filename</key>\n"
|
2011-09-29 03:05:07 +04:00
|
|
|
<< " <string>";
|
|
|
|
emitString(OS, DE.Filename);
|
|
|
|
OS << "</string>\n";
|
2011-04-07 22:44:15 +04:00
|
|
|
}
|
|
|
|
if (DE.Line != 0) {
|
|
|
|
OS << " <key>line</key>\n"
|
|
|
|
<< " <integer>" << DE.Line << "</integer>\n";
|
|
|
|
}
|
|
|
|
if (DE.Column != 0) {
|
|
|
|
OS << " <key>column</key>\n"
|
|
|
|
<< " <integer>" << DE.Column << "</integer>\n";
|
|
|
|
}
|
|
|
|
if (!DE.Message.empty()) {
|
|
|
|
OS << " <key>message</key>\n"
|
2011-09-29 03:05:07 +04:00
|
|
|
<< " <string>";
|
|
|
|
emitString(OS, DE.Message);
|
|
|
|
OS << "</string>\n";
|
2011-04-07 22:44:15 +04:00
|
|
|
}
|
|
|
|
OS << " </dict>\n";
|
2011-04-07 22:37:34 +04:00
|
|
|
}
|
2011-04-07 22:44:15 +04:00
|
|
|
OS << " </array>\n";
|
|
|
|
OS << "</dict>\n";
|
2011-04-07 22:31:10 +04:00
|
|
|
|
|
|
|
this->OS << OS.str();
|
|
|
|
}
|
2011-04-07 22:37:34 +04:00
|
|
|
|
2011-09-26 03:23:43 +04:00
|
|
|
void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
|
2011-09-26 05:18:08 +04:00
|
|
|
const Diagnostic &Info) {
|
2011-04-07 22:37:34 +04:00
|
|
|
// Default implementation (Warnings/errors count).
|
2011-09-26 03:39:51 +04:00
|
|
|
DiagnosticConsumer::HandleDiagnostic(Level, Info);
|
2011-04-07 22:37:34 +04:00
|
|
|
|
2011-04-07 22:51:54 +04:00
|
|
|
// Initialize the main file name, if we haven't already fetched it.
|
2011-05-05 06:12:02 +04:00
|
|
|
if (MainFilename.empty() && Info.hasSourceManager()) {
|
2011-04-07 22:51:54 +04:00
|
|
|
const SourceManager &SM = Info.getSourceManager();
|
|
|
|
FileID FID = SM.getMainFileID();
|
|
|
|
if (!FID.isInvalid()) {
|
|
|
|
const FileEntry *FE = SM.getFileEntryForID(FID);
|
|
|
|
if (FE && FE->getName())
|
|
|
|
MainFilename = FE->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-07 22:37:34 +04:00
|
|
|
// Create the diag entry.
|
|
|
|
DiagEntry DE;
|
|
|
|
DE.DiagnosticID = Info.getID();
|
|
|
|
DE.DiagnosticLevel = Level;
|
|
|
|
|
|
|
|
// Format the message.
|
2012-02-05 06:13:05 +04:00
|
|
|
SmallString<100> MessageStr;
|
2011-04-07 22:37:34 +04:00
|
|
|
Info.FormatDiagnostic(MessageStr);
|
|
|
|
DE.Message = MessageStr.str();
|
|
|
|
|
|
|
|
// Set the location information.
|
|
|
|
DE.Filename = "";
|
|
|
|
DE.Line = DE.Column = 0;
|
2011-05-05 06:12:02 +04:00
|
|
|
if (Info.getLocation().isValid() && Info.hasSourceManager()) {
|
2011-04-07 22:37:34 +04:00
|
|
|
const SourceManager &SM = Info.getSourceManager();
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
|
|
|
|
|
|
|
|
if (PLoc.isInvalid()) {
|
|
|
|
// At least print the file name if available:
|
|
|
|
FileID FID = SM.getFileID(Info.getLocation());
|
|
|
|
if (!FID.isInvalid()) {
|
|
|
|
const FileEntry *FE = SM.getFileEntryForID(FID);
|
|
|
|
if (FE && FE->getName())
|
|
|
|
DE.Filename = FE->getName();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DE.Filename = PLoc.getFilename();
|
|
|
|
DE.Line = PLoc.getLine();
|
|
|
|
DE.Column = PLoc.getColumn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the diagnostic entry.
|
|
|
|
Entries.push_back(DE);
|
|
|
|
}
|
2011-09-29 04:38:00 +04:00
|
|
|
|
|
|
|
DiagnosticConsumer *
|
|
|
|
LogDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
|
2012-10-24 02:26:28 +04:00
|
|
|
return new LogDiagnosticPrinter(OS, &*DiagOpts, /*OwnsOutputStream=*/false);
|
2011-09-29 04:38:00 +04:00
|
|
|
}
|
|
|
|
|