1 //===- DiagnosticEngine.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 
11 #include "mcld/LinkerConfig.h"
12 #include "mcld/LD/DiagnosticLineInfo.h"
13 #include "mcld/LD/DiagnosticPrinter.h"
14 #include "mcld/LD/MsgHandler.h"
15 
16 #include <cassert>
17 
18 namespace mcld {
19 
20 //===----------------------------------------------------------------------===//
21 // DiagnosticEngine
22 //===----------------------------------------------------------------------===//
DiagnosticEngine()23 DiagnosticEngine::DiagnosticEngine()
24     : m_pConfig(NULL),
25       m_pLineInfo(NULL),
26       m_pPrinter(NULL),
27       m_pInfoMap(NULL),
28       m_OwnPrinter(false) {
29 }
30 
~DiagnosticEngine()31 DiagnosticEngine::~DiagnosticEngine() {
32   if (m_OwnPrinter && m_pPrinter != NULL)
33     delete m_pPrinter;
34 
35   delete m_pInfoMap;
36 
37   // FIXME: design the destructive relation of LineInfo.
38   delete m_pLineInfo;
39 }
40 
reset(const LinkerConfig & pConfig)41 void DiagnosticEngine::reset(const LinkerConfig& pConfig) {
42   m_pConfig = &pConfig;
43   delete m_pInfoMap;
44   m_pInfoMap = new DiagnosticInfos(*m_pConfig);
45   m_State.reset();
46 }
47 
setLineInfo(DiagnosticLineInfo & pLineInfo)48 void DiagnosticEngine::setLineInfo(DiagnosticLineInfo& pLineInfo) {
49   m_pLineInfo = &pLineInfo;
50 }
51 
setPrinter(DiagnosticPrinter & pPrinter,bool pShouldOwnPrinter)52 void DiagnosticEngine::setPrinter(DiagnosticPrinter& pPrinter,
53                                   bool pShouldOwnPrinter) {
54   if (m_OwnPrinter && m_pPrinter != NULL)
55     delete m_pPrinter;
56   m_pPrinter = &pPrinter;
57   m_OwnPrinter = pShouldOwnPrinter;
58 }
59 
60 // emit - process current diagnostic.
emit()61 bool DiagnosticEngine::emit() {
62   assert(m_pInfoMap != NULL);
63   bool emitted = m_pInfoMap->process(*this);
64   m_State.reset();
65   return emitted;
66 }
67 
report(uint16_t pID,DiagnosticEngine::Severity pSeverity)68 MsgHandler DiagnosticEngine::report(uint16_t pID,
69                                     DiagnosticEngine::Severity pSeverity) {
70   m_State.ID = pID;
71   m_State.severity = pSeverity;
72 
73   MsgHandler result(*this);
74   return result;
75 }
76 
77 }  // namespace mcld
78