MsgHandling.cpp revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
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/TextDiagnosticPrinter.h>
13#include <mcld/LD/MsgHandler.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
23using namespace mcld;
24
25//===----------------------------------------------------------------------===//
26// static variables
27//===----------------------------------------------------------------------===//
28static llvm::ManagedStatic<DiagnosticEngine> g_pEngine;
29
30void
31mcld::InitializeDiagnosticEngine(const mcld::LinkerConfig& pConfig,
32                                 DiagnosticPrinter* pPrinter)
33{
34  g_pEngine->reset(pConfig);
35  if (NULL != pPrinter)
36    g_pEngine->setPrinter(*pPrinter, false);
37  else {
38    DiagnosticPrinter* printer = new TextDiagnosticPrinter(mcld::errs(), pConfig);
39    g_pEngine->setPrinter(*printer, true);
40  }
41}
42
43DiagnosticEngine& mcld::getDiagnosticEngine()
44{
45  return *g_pEngine;
46}
47
48bool mcld::Diagnose()
49{
50  if (g_pEngine->getPrinter()->getNumErrors() > 0) {
51    // If we reached here, we are failing ungracefully. Run the interrupt handlers
52    // to make sure any special cleanups get done, in particular that we remove
53    // files registered with RemoveFileOnSignal.
54    llvm::sys::RunInterruptHandlers();
55    g_pEngine->getPrinter()->finish();
56    return false;
57  }
58  return true;
59}
60
61void mcld::FinalizeDiagnosticEngine()
62{
63  g_pEngine->getPrinter()->finish();
64}
65
66