2011-04-08 02:31:10 +08: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 06:26:28 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2011-04-08 02:37:34 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2014-07-06 12:26:52 +08:00
|
|
|
#include "clang/Basic/PlistSupport.h"
|
2011-04-08 02:37:34 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2011-04-08 02:31:10 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-01-17 10:30:50 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-04-08 02:31:10 +08:00
|
|
|
using namespace clang;
|
2014-07-06 12:26:52 +08:00
|
|
|
using namespace markup;
|
2011-04-08 02:31:10 +08:00
|
|
|
|
2014-09-16 01:30:56 +08:00
|
|
|
LogDiagnosticPrinter::LogDiagnosticPrinter(
|
|
|
|
raw_ostream &os, DiagnosticOptions *diags,
|
|
|
|
std::unique_ptr<raw_ostream> StreamOwner)
|
|
|
|
: OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
|
|
|
|
DiagOpts(diags) {}
|
2011-04-08 02:31:10 +08:00
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
static StringRef getLevelName(DiagnosticsEngine::Level Level) {
|
2011-04-08 02:37:34 +08:00
|
|
|
switch (Level) {
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::Ignored: return "ignored";
|
2014-02-28 17:11:08 +08:00
|
|
|
case DiagnosticsEngine::Remark: return "remark";
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::Note: return "note";
|
|
|
|
case DiagnosticsEngine::Warning: return "warning";
|
|
|
|
case DiagnosticsEngine::Error: return "error";
|
|
|
|
case DiagnosticsEngine::Fatal: return "fatal error";
|
2011-04-08 02:37:34 +08:00
|
|
|
}
|
2012-01-17 10:30:50 +08:00
|
|
|
llvm_unreachable("Invalid DiagnosticsEngine level!");
|
2011-04-08 02:37:34 +08:00
|
|
|
}
|
|
|
|
|
2014-07-06 12:26:52 +08:00
|
|
|
void
|
|
|
|
LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
|
|
|
|
const LogDiagnosticPrinter::DiagEntry &DE) {
|
|
|
|
OS << " <dict>\n";
|
|
|
|
OS << " <key>level</key>\n"
|
|
|
|
<< " ";
|
|
|
|
EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
|
|
|
|
if (!DE.Filename.empty()) {
|
|
|
|
OS << " <key>filename</key>\n"
|
|
|
|
<< " ";
|
|
|
|
EmitString(OS, DE.Filename) << '\n';
|
2011-09-29 07:05:07 +08:00
|
|
|
}
|
2014-07-06 12:26:52 +08:00
|
|
|
if (DE.Line != 0) {
|
|
|
|
OS << " <key>line</key>\n"
|
|
|
|
<< " ";
|
|
|
|
EmitInteger(OS, DE.Line) << '\n';
|
|
|
|
}
|
|
|
|
if (DE.Column != 0) {
|
|
|
|
OS << " <key>column</key>\n"
|
|
|
|
<< " ";
|
|
|
|
EmitInteger(OS, DE.Column) << '\n';
|
|
|
|
}
|
|
|
|
if (!DE.Message.empty()) {
|
|
|
|
OS << " <key>message</key>\n"
|
|
|
|
<< " ";
|
|
|
|
EmitString(OS, DE.Message) << '\n';
|
|
|
|
}
|
2014-11-15 05:23:56 +08:00
|
|
|
OS << " <key>ID</key>\n"
|
|
|
|
<< " ";
|
|
|
|
EmitInteger(OS, DE.DiagnosticID) << '\n';
|
|
|
|
if (!DE.WarningOption.empty()) {
|
|
|
|
OS << " <key>WarningOption</key>\n"
|
|
|
|
<< " ";
|
|
|
|
EmitString(OS, DE.WarningOption) << '\n';
|
|
|
|
}
|
2014-07-06 12:26:52 +08:00
|
|
|
OS << " </dict>\n";
|
2011-09-29 07:05:07 +08:00
|
|
|
}
|
|
|
|
|
2011-04-08 02:37:34 +08: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 07:39:51 +08: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-08 02:37:34 +08:00
|
|
|
if (Entries.empty())
|
|
|
|
return;
|
2011-04-08 02:31:10 +08:00
|
|
|
|
|
|
|
// Write to a temporary string to ensure atomic write of diagnostic object.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<512> Msg;
|
2011-04-08 02:31:10 +08:00
|
|
|
llvm::raw_svector_ostream OS(Msg);
|
|
|
|
|
2011-04-08 02:44:15 +08:00
|
|
|
OS << "<dict>\n";
|
2011-04-08 02:51:54 +08:00
|
|
|
if (!MainFilename.empty()) {
|
|
|
|
OS << " <key>main-file</key>\n"
|
2014-07-06 12:26:52 +08:00
|
|
|
<< " ";
|
|
|
|
EmitString(OS, MainFilename) << '\n';
|
2011-04-08 02:51:54 +08:00
|
|
|
}
|
|
|
|
if (!DwarfDebugFlags.empty()) {
|
|
|
|
OS << " <key>dwarf-debug-flags</key>\n"
|
2014-07-06 12:26:52 +08:00
|
|
|
<< " ";
|
|
|
|
EmitString(OS, DwarfDebugFlags) << '\n';
|
2011-04-08 02:51:54 +08:00
|
|
|
}
|
2011-04-08 02:44:15 +08:00
|
|
|
OS << " <key>diagnostics</key>\n";
|
|
|
|
OS << " <array>\n";
|
2014-07-06 12:26:52 +08:00
|
|
|
for (auto &DE : Entries)
|
|
|
|
EmitDiagEntry(OS, DE);
|
2011-04-08 02:44:15 +08:00
|
|
|
OS << " </array>\n";
|
|
|
|
OS << "</dict>\n";
|
2011-04-08 02:31:10 +08:00
|
|
|
|
|
|
|
this->OS << OS.str();
|
|
|
|
}
|
2011-04-08 02:37:34 +08:00
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
|
2011-09-26 09:18:08 +08:00
|
|
|
const Diagnostic &Info) {
|
2011-04-08 02:37:34 +08:00
|
|
|
// Default implementation (Warnings/errors count).
|
2011-09-26 07:39:51 +08:00
|
|
|
DiagnosticConsumer::HandleDiagnostic(Level, Info);
|
2011-04-08 02:37:34 +08:00
|
|
|
|
2011-04-08 02:51:54 +08:00
|
|
|
// Initialize the main file name, if we haven't already fetched it.
|
2011-05-05 10:12:02 +08:00
|
|
|
if (MainFilename.empty() && Info.hasSourceManager()) {
|
2011-04-08 02:51:54 +08:00
|
|
|
const SourceManager &SM = Info.getSourceManager();
|
|
|
|
FileID FID = SM.getMainFileID();
|
2015-10-03 18:46:20 +08:00
|
|
|
if (FID.isValid()) {
|
2011-04-08 02:51:54 +08:00
|
|
|
const FileEntry *FE = SM.getFileEntryForID(FID);
|
2014-02-28 01:23:33 +08:00
|
|
|
if (FE && FE->isValid())
|
2011-04-08 02:51:54 +08:00
|
|
|
MainFilename = FE->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-08 02:37:34 +08:00
|
|
|
// Create the diag entry.
|
|
|
|
DiagEntry DE;
|
|
|
|
DE.DiagnosticID = Info.getID();
|
|
|
|
DE.DiagnosticLevel = Level;
|
|
|
|
|
2014-11-15 05:23:56 +08:00
|
|
|
DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
|
|
|
|
|
2011-04-08 02:37:34 +08:00
|
|
|
// Format the message.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<100> MessageStr;
|
2011-04-08 02:37:34 +08:00
|
|
|
Info.FormatDiagnostic(MessageStr);
|
|
|
|
DE.Message = MessageStr.str();
|
|
|
|
|
|
|
|
// Set the location information.
|
|
|
|
DE.Filename = "";
|
|
|
|
DE.Line = DE.Column = 0;
|
2011-05-05 10:12:02 +08:00
|
|
|
if (Info.getLocation().isValid() && Info.hasSourceManager()) {
|
2011-04-08 02:37:34 +08: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());
|
2015-10-03 18:46:20 +08:00
|
|
|
if (FID.isValid()) {
|
2011-04-08 02:37:34 +08:00
|
|
|
const FileEntry *FE = SM.getFileEntryForID(FID);
|
2014-02-28 01:23:33 +08:00
|
|
|
if (FE && FE->isValid())
|
2011-04-08 02:37:34 +08:00
|
|
|
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 08:38:00 +08:00
|
|
|
|