1//===- MsgHandling.cpp ----------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "mcld/LD/DiagnosticEngine.h"
10#include "mcld/LD/DiagnosticLineInfo.h"
11#include "mcld/LD/DiagnosticPrinter.h"
12#include "mcld/LD/MsgHandler.h"
13#include "mcld/LD/TextDiagnosticPrinter.h"
14#include "mcld/Support/MsgHandling.h"
15#include "mcld/Support/raw_ostream.h"
16
17#include <llvm/Support/ManagedStatic.h>
18#include <llvm/Support/raw_ostream.h>
19#include <llvm/Support/Signals.h>
20
21#include <cstdlib>
22
23namespace mcld {
24
25//===----------------------------------------------------------------------===//
26// static variables
27//===----------------------------------------------------------------------===//
28static llvm::ManagedStatic<DiagnosticEngine> g_pEngine;
29
30void InitializeDiagnosticEngine(const LinkerConfig& pConfig,
31                                DiagnosticPrinter* pPrinter) {
32  g_pEngine->reset(pConfig);
33  if (pPrinter != NULL)
34    g_pEngine->setPrinter(*pPrinter, false);
35  else {
36    DiagnosticPrinter* printer =
37        new TextDiagnosticPrinter(errs(), pConfig);
38    g_pEngine->setPrinter(*printer, true);
39  }
40}
41
42DiagnosticEngine& getDiagnosticEngine() {
43  return *g_pEngine;
44}
45
46bool Diagnose() {
47  if (g_pEngine->getPrinter()->getNumErrors() > 0) {
48    // If we reached here, we are failing ungracefully. Run the interrupt
49    // handlers
50    // to make sure any special cleanups get done, in particular that we remove
51    // files registered with RemoveFileOnSignal.
52    llvm::sys::RunInterruptHandlers();
53    g_pEngine->getPrinter()->finish();
54    return false;
55  }
56  return true;
57}
58
59void FinalizeDiagnosticEngine() {
60  g_pEngine->getPrinter()->finish();
61}
62
63}  // namespace mcld
64