DiagnosticEngine.h revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
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
18namespace mcld {
19
20class Input;
21class LinkerConfig;
22class MsgHandler;
23class DiagnosticPrinter;
24class 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 */
37class DiagnosticEngine
38{
39public:
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
59public:
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
70  const DiagnosticPrinter* getPrinter() const { return m_pPrinter; }
71  DiagnosticPrinter*       getPrinter()       { return m_pPrinter; }
72
73
74  DiagnosticPrinter* takePrinter() {
75    m_OwnPrinter = false;
76    return m_pPrinter;
77  }
78
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
89private:
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:
102    State() : numArgs(0), ID(-1), severity(None), file(NULL) { }
103    ~State() { }
104
105    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
122private:
123  State& state()
124  { return m_State; }
125
126  const State& state() const
127  { return m_State; }
128
129  DiagnosticInfos& infoMap() {
130    assert(NULL != m_pInfoMap && "DiagnosticEngine was not initialized!");
131    return *m_pInfoMap;
132  }
133
134  const DiagnosticInfos& infoMap() const {
135    assert(NULL != m_pInfoMap && "DiagnosticEngine was not initialized!");
136    return *m_pInfoMap;
137  }
138
139private:
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