Path: blob/main/contrib/llvm-project/clang/lib/Frontend/LogDiagnosticPrinter.cpp
35233 views
//===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/Frontend/LogDiagnosticPrinter.h"9#include "clang/Basic/DiagnosticOptions.h"10#include "clang/Basic/FileManager.h"11#include "clang/Basic/PlistSupport.h"12#include "clang/Basic/SourceManager.h"13#include "llvm/ADT/SmallString.h"14#include "llvm/Support/ErrorHandling.h"15#include "llvm/Support/raw_ostream.h"16using namespace clang;17using namespace markup;1819LogDiagnosticPrinter::LogDiagnosticPrinter(20raw_ostream &os, DiagnosticOptions *diags,21std::unique_ptr<raw_ostream> StreamOwner)22: OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),23DiagOpts(diags) {}2425static StringRef getLevelName(DiagnosticsEngine::Level Level) {26switch (Level) {27case DiagnosticsEngine::Ignored: return "ignored";28case DiagnosticsEngine::Remark: return "remark";29case DiagnosticsEngine::Note: return "note";30case DiagnosticsEngine::Warning: return "warning";31case DiagnosticsEngine::Error: return "error";32case DiagnosticsEngine::Fatal: return "fatal error";33}34llvm_unreachable("Invalid DiagnosticsEngine level!");35}3637void38LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,39const LogDiagnosticPrinter::DiagEntry &DE) {40OS << " <dict>\n";41OS << " <key>level</key>\n"42<< " ";43EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';44if (!DE.Filename.empty()) {45OS << " <key>filename</key>\n"46<< " ";47EmitString(OS, DE.Filename) << '\n';48}49if (DE.Line != 0) {50OS << " <key>line</key>\n"51<< " ";52EmitInteger(OS, DE.Line) << '\n';53}54if (DE.Column != 0) {55OS << " <key>column</key>\n"56<< " ";57EmitInteger(OS, DE.Column) << '\n';58}59if (!DE.Message.empty()) {60OS << " <key>message</key>\n"61<< " ";62EmitString(OS, DE.Message) << '\n';63}64OS << " <key>ID</key>\n"65<< " ";66EmitInteger(OS, DE.DiagnosticID) << '\n';67if (!DE.WarningOption.empty()) {68OS << " <key>WarningOption</key>\n"69<< " ";70EmitString(OS, DE.WarningOption) << '\n';71}72OS << " </dict>\n";73}7475void LogDiagnosticPrinter::EndSourceFile() {76// We emit all the diagnostics in EndSourceFile. However, we don't emit any77// entry if no diagnostics were present.78//79// Note that DiagnosticConsumer has no "end-of-compilation" callback, so we80// will miss any diagnostics which are emitted after and outside the81// translation unit processing.82if (Entries.empty())83return;8485// Write to a temporary string to ensure atomic write of diagnostic object.86SmallString<512> Msg;87llvm::raw_svector_ostream OS(Msg);8889OS << "<dict>\n";90if (!MainFilename.empty()) {91OS << " <key>main-file</key>\n"92<< " ";93EmitString(OS, MainFilename) << '\n';94}95if (!DwarfDebugFlags.empty()) {96OS << " <key>dwarf-debug-flags</key>\n"97<< " ";98EmitString(OS, DwarfDebugFlags) << '\n';99}100OS << " <key>diagnostics</key>\n";101OS << " <array>\n";102for (auto &DE : Entries)103EmitDiagEntry(OS, DE);104OS << " </array>\n";105OS << "</dict>\n";106107this->OS << OS.str();108}109110void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,111const Diagnostic &Info) {112// Default implementation (Warnings/errors count).113DiagnosticConsumer::HandleDiagnostic(Level, Info);114115// Initialize the main file name, if we haven't already fetched it.116if (MainFilename.empty() && Info.hasSourceManager()) {117const SourceManager &SM = Info.getSourceManager();118FileID FID = SM.getMainFileID();119if (FID.isValid()) {120if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID))121MainFilename = std::string(FE->getName());122}123}124125// Create the diag entry.126DiagEntry DE;127DE.DiagnosticID = Info.getID();128DE.DiagnosticLevel = Level;129130DE.WarningOption =131std::string(DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID));132133// Format the message.134SmallString<100> MessageStr;135Info.FormatDiagnostic(MessageStr);136DE.Message = std::string(MessageStr);137138// Set the location information.139DE.Filename = "";140DE.Line = DE.Column = 0;141if (Info.getLocation().isValid() && Info.hasSourceManager()) {142const SourceManager &SM = Info.getSourceManager();143PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());144145if (PLoc.isInvalid()) {146// At least print the file name if available:147FileID FID = SM.getFileID(Info.getLocation());148if (FID.isValid()) {149if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID))150DE.Filename = std::string(FE->getName());151}152} else {153DE.Filename = PLoc.getFilename();154DE.Line = PLoc.getLine();155DE.Column = PLoc.getColumn();156}157}158159// Record the diagnostic entry.160Entries.push_back(DE);161}162163164165