• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- DiagnosticEngine.h -------------------------------------------------===//
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 #ifndef MCLD_DIAGNOSTIC_ENGINE_H
10 #define MCLD_DIAGNOSTIC_ENGINE_H
11 #ifdef ENABLE_UNITTEST
12 #include <gtest.h>
13 #endif
14 #include <string>
15 #include <llvm/Support/DataTypes.h>
16 #include <mcld/LD/DiagnosticInfos.h>
17 
18 namespace mcld {
19 
20 class Input;
21 class LinkerConfig;
22 class MsgHandler;
23 class DiagnosticPrinter;
24 class DiagnosticLineInfo;
25 
26 /** \class DiagnosticEngine
27  *  \brief DiagnosticEngine is used to report problems and issues.
28  *
29  *  DiagnosticEngine is used to report problems and issues. It creates the
30  *  Diagnostics and passes them to the DiagnosticPrinter for reporting to the
31  *  user.
32  *
33  *  DiagnosticEngine is a complex class, it is responsible for
34  *  - remember the argument string for MsgHandler
35  *  - choice the severity of a message by options
36  */
37 class DiagnosticEngine
38 {
39 public:
40   enum Severity {
41     Unreachable,
42     Fatal,
43     Error,
44     Warning,
45     Debug,
46     Note,
47     Ignore,
48     None
49   };
50 
51   enum ArgumentKind {
52     ak_std_string,  // std::string
53     ak_c_string,    // const char *
54     ak_sint,        // int
55     ak_uint,        // unsigned int
56     ak_bool         // bool
57   };
58 
59 public:
60   DiagnosticEngine();
61 
62   ~DiagnosticEngine();
63 
64   void reset(const LinkerConfig& pConfig);
65 
66   void setLineInfo(DiagnosticLineInfo& pLineInfo);
67 
68   void setPrinter(DiagnosticPrinter& pPrinter, bool pShouldOwnPrinter = true);
69 
getPrinter()70   const DiagnosticPrinter* getPrinter() const { return m_pPrinter; }
getPrinter()71   DiagnosticPrinter*       getPrinter()       { return m_pPrinter; }
72 
73 
takePrinter()74   DiagnosticPrinter* takePrinter() {
75     m_OwnPrinter = false;
76     return m_pPrinter;
77   }
78 
ownPrinter()79   bool ownPrinter() const
80   { return m_OwnPrinter; }
81 
82   // -----  emission  ----- //
83   // emit - process the message to printer
84   bool emit();
85 
86   // report - issue the message to the printer
87   MsgHandler report(uint16_t pID, Severity pSeverity);
88 
89 private:
90   friend class MsgHandler;
91   friend class Diagnostic;
92 
93   enum {
94     /// MaxArguments - The maximum number of arguments we can hold. We currently
95     /// only support up to 10 arguments (%0-%9).
96     MaxArguments = 10
97   };
98 
99   struct State
100   {
101   public:
StateState102     State() : numArgs(0), ID(-1), severity(None), file(NULL) { }
~StateState103     ~State() { }
104 
resetState105     void reset() {
106       numArgs = 0;
107       ID = -1;
108       severity = None;
109       file = NULL;
110     }
111 
112   public:
113     std::string ArgumentStrs[MaxArguments];
114     intptr_t ArgumentVals[MaxArguments];
115     uint8_t ArgumentKinds[MaxArguments];
116     int8_t numArgs;
117     uint16_t ID;
118     Severity severity;
119     Input* file;
120   };
121 
122 private:
state()123   State& state()
124   { return m_State; }
125 
state()126   const State& state() const
127   { return m_State; }
128 
infoMap()129   DiagnosticInfos& infoMap() {
130     assert(NULL != m_pInfoMap && "DiagnosticEngine was not initialized!");
131     return *m_pInfoMap;
132   }
133 
infoMap()134   const DiagnosticInfos& infoMap() const {
135     assert(NULL != m_pInfoMap && "DiagnosticEngine was not initialized!");
136     return *m_pInfoMap;
137   }
138 
139 private:
140   const LinkerConfig* m_pConfig;
141   DiagnosticLineInfo* m_pLineInfo;
142   DiagnosticPrinter* m_pPrinter;
143   DiagnosticInfos* m_pInfoMap;
144   bool m_OwnPrinter;
145 
146   State m_State;
147 };
148 
149 } // namespace of mcld
150 
151 #endif
152 
153