DiagnosticEngine.h revision 67e37f1be98c926645219cfb47fab9e90d8c725c
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 MCLDInfo;
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 MCLDInfo& pLDInfo);
65
66  void setLineInfo(DiagnosticLineInfo& pLineInfo);
67
68  void setPrinter(DiagnosticPrinter& pPrinter, bool pShouldOwnPrinter = true);
69
70  DiagnosticPrinter* getPrinter()
71  { return m_pPrinter; }
72
73  const DiagnosticPrinter* getPrinter() const
74  { return m_pPrinter; }
75
76  DiagnosticPrinter* takePrinter() {
77    m_OwnPrinter = false;
78    return m_pPrinter;
79  }
80
81  bool ownPrinter() const
82  { return m_OwnPrinter; }
83
84  // -----  emission  ----- //
85  // emit - process the message to printer
86  bool emit();
87
88  // report - issue the message to the printer
89  MsgHandler report(uint16_t pID, Severity pSeverity);
90
91private:
92  friend class MsgHandler;
93  friend class Diagnostic;
94
95  enum {
96    /// MaxArguments - The maximum number of arguments we can hold. We currently
97    /// only support up to 10 arguments (%0-%9).
98    MaxArguments = 10,
99  };
100
101  struct State
102  {
103  public:
104    State() : numArgs(0), ID(-1), severity(None), file(NULL) { }
105    ~State() { }
106
107    void reset() {
108      numArgs = 0;
109      ID = -1;
110      severity = None;
111      file = NULL;
112    }
113
114  public:
115    std::string ArgumentStrs[MaxArguments];
116    intptr_t ArgumentVals[MaxArguments];
117    uint8_t ArgumentKinds[MaxArguments];
118    int8_t numArgs;
119    uint16_t ID;
120    Severity severity;
121    Input* file;
122  };
123
124private:
125  State& state()
126  { return m_State; }
127
128  const State& state() const
129  { return m_State; }
130
131  DiagnosticInfos& infoMap() {
132    assert(NULL != m_pInfoMap && "DiagnosticEngine was not initialized!");
133    return *m_pInfoMap;
134  }
135
136  const DiagnosticInfos& infoMap() const {
137    assert(NULL != m_pInfoMap && "DiagnosticEngine was not initialized!");
138    return *m_pInfoMap;
139  }
140
141private:
142  const MCLDInfo* m_pLDInfo;
143  DiagnosticLineInfo* m_pLineInfo;
144  DiagnosticPrinter* m_pPrinter;
145  DiagnosticInfos* m_pInfoMap;
146  bool m_OwnPrinter;
147
148  State m_State;
149};
150
151} // namespace of mcld
152
153#endif
154
155