1affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//===- TextDiagnosticPrinter.cpp ------------------------------------------===// 2affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// 3affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// The MCLinker Project 4affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// 5affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// This file is distributed under the University of Illinois Open Source 6affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// License. See LICENSE.TXT for details. 7affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// 8affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//===----------------------------------------------------------------------===// 9affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/LD/TextDiagnosticPrinter.h> 1022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LinkerConfig.h> 11affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <llvm/Support/Signals.h> 12affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <string> 13affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 14affc150dc44fab1911775a49636d0ce85333b634Zonr Changusing namespace mcld; 15affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 16affc150dc44fab1911775a49636d0ce85333b634Zonr Changstatic const enum llvm::raw_ostream::Colors UnreachableColor = llvm::raw_ostream::RED; 17affc150dc44fab1911775a49636d0ce85333b634Zonr Changstatic const enum llvm::raw_ostream::Colors FatalColor = llvm::raw_ostream::YELLOW; 18affc150dc44fab1911775a49636d0ce85333b634Zonr Changstatic const enum llvm::raw_ostream::Colors ErrorColor = llvm::raw_ostream::RED; 19affc150dc44fab1911775a49636d0ce85333b634Zonr Changstatic const enum llvm::raw_ostream::Colors WarningColor = llvm::raw_ostream::MAGENTA; 20affc150dc44fab1911775a49636d0ce85333b634Zonr Changstatic const enum llvm::raw_ostream::Colors DebugColor = llvm::raw_ostream::CYAN; 21affc150dc44fab1911775a49636d0ce85333b634Zonr Changstatic const enum llvm::raw_ostream::Colors NoteColor = llvm::raw_ostream::GREEN; 22affc150dc44fab1911775a49636d0ce85333b634Zonr Changstatic const enum llvm::raw_ostream::Colors IgnoreColor = llvm::raw_ostream::BLUE; 23affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 24affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//===----------------------------------------------------------------------===// 25affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// TextDiagnosticPrinter 26affc150dc44fab1911775a49636d0ce85333b634Zonr ChangTextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream& pOStream, 2722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao const LinkerConfig& pConfig) 2822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao : m_OStream(pOStream), m_Config(pConfig), m_pInput(NULL) { 29affc150dc44fab1911775a49636d0ce85333b634Zonr Chang} 30affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 31affc150dc44fab1911775a49636d0ce85333b634Zonr ChangTextDiagnosticPrinter::~TextDiagnosticPrinter() 32affc150dc44fab1911775a49636d0ce85333b634Zonr Chang{ 33affc150dc44fab1911775a49636d0ce85333b634Zonr Chang} 34affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 35affc150dc44fab1911775a49636d0ce85333b634Zonr Chang/// HandleDiagnostic - Handle this diagnostic, reporting it to the user or 36affc150dc44fab1911775a49636d0ce85333b634Zonr Chang/// capturing it to a log as needed. 37affc150dc44fab1911775a49636d0ce85333b634Zonr Changvoid 38affc150dc44fab1911775a49636d0ce85333b634Zonr ChangTextDiagnosticPrinter::handleDiagnostic(DiagnosticEngine::Severity pSeverity, 39affc150dc44fab1911775a49636d0ce85333b634Zonr Chang const Diagnostic& pInfo) 40affc150dc44fab1911775a49636d0ce85333b634Zonr Chang{ 41affc150dc44fab1911775a49636d0ce85333b634Zonr Chang DiagnosticPrinter::handleDiagnostic(pSeverity, pInfo); 42affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 43affc150dc44fab1911775a49636d0ce85333b634Zonr Chang std::string out_string; 44affc150dc44fab1911775a49636d0ce85333b634Zonr Chang pInfo.format(out_string); 45affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 46affc150dc44fab1911775a49636d0ce85333b634Zonr Chang switch (pSeverity) { 47affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Unreachable: { 48affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(UnreachableColor, true); 49affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "Unreachable: "; 50affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 51affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << out_string << "\n"; 52affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 53affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 54affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Fatal: { 55affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(FatalColor, true); 56affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "Fatal: "; 57affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 58affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << out_string << "\n"; 59affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 60affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 61affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Error: { 62affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(ErrorColor, true); 63affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "Error: "; 64affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 65affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << out_string << "\n"; 66affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 67affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 68affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Warning: { 69affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(WarningColor, true); 70affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "Warning: "; 71affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 72affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << out_string << "\n"; 73affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 74affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 75affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Debug: { 76affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // show debug message only if verbose >= 0 7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao if (0 <= m_Config.options().verbose()) { 78affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(DebugColor, true); 79affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "Debug: "; 80affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 81affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << out_string << "\n"; 82affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 83affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 84affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 85affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Note: { 86affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // show ignored message only if verbose >= 1 8722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao if (1 <= m_Config.options().verbose()) { 88affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(NoteColor, true); 89affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "Note: "; 90affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 91affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << out_string << "\n"; 92affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 93affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 94affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 95affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Ignore: { 96affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // show ignored message only if verbose >= 2 9722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao if (2 <= m_Config.options().verbose()) { 98affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(IgnoreColor, true); 99affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "Ignore: "; 100affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 101affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << out_string << "\n"; 102affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 103affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 104affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 105affc150dc44fab1911775a49636d0ce85333b634Zonr Chang default: 106affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 107affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 108affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 109affc150dc44fab1911775a49636d0ce85333b634Zonr Chang switch (pSeverity) { 110affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Unreachable: { 111affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "\n\n"; 112affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(llvm::raw_ostream::YELLOW); 113affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "You encounter a bug of MCLinker, please report to:\n" 114affc150dc44fab1911775a49636d0ce85333b634Zonr Chang << " mclinker@googlegroups.com\n"; 115affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 116affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 117affc150dc44fab1911775a49636d0ce85333b634Zonr Chang /** fall through **/ 118affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Fatal: { 119affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // If we reached here, we are failing ungracefully. Run the interrupt handlers 120affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // to make sure any special cleanups get done, in particular that we remove 121affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // files registered with RemoveFileOnSignal. 122affc150dc44fab1911775a49636d0ce85333b634Zonr Chang llvm::sys::RunInterruptHandlers(); 123affc150dc44fab1911775a49636d0ce85333b634Zonr Chang exit(1); 124affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 125affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 126affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Error: { 12722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao int16_t error_limit = m_Config.options().maxErrorNum(); 128affc150dc44fab1911775a49636d0ce85333b634Zonr Chang if ((error_limit != -1) && 129affc150dc44fab1911775a49636d0ce85333b634Zonr Chang (getNumErrors() > static_cast<unsigned>(error_limit))) { 130affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "\n\n"; 131affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(llvm::raw_ostream::YELLOW); 132affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "too many error messages (>" << error_limit << ")...\n"; 133affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 134affc150dc44fab1911775a49636d0ce85333b634Zonr Chang llvm::sys::RunInterruptHandlers(); 135affc150dc44fab1911775a49636d0ce85333b634Zonr Chang exit(1); 136affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 137affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 138affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 139affc150dc44fab1911775a49636d0ce85333b634Zonr Chang case DiagnosticEngine::Warning: { 14022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao int16_t warning_limit = m_Config.options().maxWarnNum(); 141affc150dc44fab1911775a49636d0ce85333b634Zonr Chang if ((warning_limit != -1) && 142affc150dc44fab1911775a49636d0ce85333b634Zonr Chang (getNumWarnings() > static_cast<unsigned>(warning_limit))) { 143affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "\n\n"; 144affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.changeColor(llvm::raw_ostream::YELLOW); 145affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream << "too many warning messages (>" << warning_limit << ")...\n"; 146affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_OStream.resetColor(); 147affc150dc44fab1911775a49636d0ce85333b634Zonr Chang llvm::sys::RunInterruptHandlers(); 148affc150dc44fab1911775a49636d0ce85333b634Zonr Chang exit(1); 149affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 150affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 151affc150dc44fab1911775a49636d0ce85333b634Zonr Chang default: 152affc150dc44fab1911775a49636d0ce85333b634Zonr Chang break; 153affc150dc44fab1911775a49636d0ce85333b634Zonr Chang } 154affc150dc44fab1911775a49636d0ce85333b634Zonr Chang} 155affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 15622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaovoid TextDiagnosticPrinter::beginInput(const Input& pInput, const LinkerConfig& pConfig) 157affc150dc44fab1911775a49636d0ce85333b634Zonr Chang{ 158affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_pInput = &pInput; 159affc150dc44fab1911775a49636d0ce85333b634Zonr Chang} 160affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 161affc150dc44fab1911775a49636d0ce85333b634Zonr Changvoid TextDiagnosticPrinter::endInput() 162affc150dc44fab1911775a49636d0ce85333b634Zonr Chang{ 163affc150dc44fab1911775a49636d0ce85333b634Zonr Chang m_pInput = NULL; 164affc150dc44fab1911775a49636d0ce85333b634Zonr Chang} 165