1 //===- Diagnostic.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_LD_DIAGNOSTIC_H_
10 #define MCLD_LD_DIAGNOSTIC_H_
11 
12 #include "mcld/LD/DiagnosticEngine.h"
13 
14 #include <cassert>
15 #include <string>
16 
17 namespace mcld {
18 
19 /** \class Diagnostic
20  *  \brief Diagnostic provides current status to DiagnosticPrinters.
21  */
22 class Diagnostic {
23  public:
24   explicit Diagnostic(DiagnosticEngine& pEngine);
25 
26   ~Diagnostic();
27 
getID()28   unsigned int getID() const { return m_Engine.state().ID; }
29 
getNumArgs()30   unsigned int getNumArgs() const { return m_Engine.state().numArgs; }
31 
getArgKind(unsigned int pIdx)32   DiagnosticEngine::ArgumentKind getArgKind(unsigned int pIdx) const {
33     assert(pIdx < getNumArgs() && "Argument index is out of range!");
34     return (DiagnosticEngine::ArgumentKind)m_Engine.state().ArgumentKinds[pIdx];
35   }
36 
getArgStdStr(unsigned int pIdx)37   const std::string& getArgStdStr(unsigned int pIdx) const {
38     assert(getArgKind(pIdx) == DiagnosticEngine::ak_std_string &&
39            "Invalid argument accessor!");
40     return m_Engine.state().ArgumentStrs[pIdx];
41   }
42 
getArgCStr(unsigned int pIdx)43   const char* getArgCStr(unsigned int pIdx) const {
44     assert(getArgKind(pIdx) == DiagnosticEngine::ak_c_string &&
45            "Invalid argument accessor!");
46     return reinterpret_cast<const char*>(m_Engine.state().ArgumentVals[pIdx]);
47   }
48 
getArgSInt(unsigned int pIdx)49   int getArgSInt(unsigned int pIdx) const {
50     assert(getArgKind(pIdx) == DiagnosticEngine::ak_sint &&
51            "Invalid argument accessor!");
52     return static_cast<int>(m_Engine.state().ArgumentVals[pIdx]);
53   }
54 
getArgUInt(unsigned int pIdx)55   unsigned int getArgUInt(unsigned int pIdx) const {
56     assert(getArgKind(pIdx) == DiagnosticEngine::ak_uint &&
57            "Invalid argument accessor!");
58     return (unsigned int)m_Engine.state().ArgumentVals[pIdx];
59   }
60 
getArgULongLong(unsigned pIdx)61   unsigned long long getArgULongLong(unsigned pIdx) const {
62     assert(getArgKind(pIdx) == DiagnosticEngine::ak_ulonglong &&
63            "Invalid argument accessor!");
64     return (unsigned long long)m_Engine.state().ArgumentVals[pIdx];
65   }
66 
getArgBool(unsigned int pIdx)67   bool getArgBool(unsigned int pIdx) const {
68     assert(getArgKind(pIdx) == DiagnosticEngine::ak_bool &&
69            "Invalid argument accessor!");
70     return static_cast<bool>(m_Engine.state().ArgumentVals[pIdx]);
71   }
72 
getRawVals(unsigned int pIdx)73   intptr_t getRawVals(unsigned int pIdx) const {
74     assert(getArgKind(pIdx) != DiagnosticEngine::ak_std_string &&
75            "Invalid argument accessor!");
76     return m_Engine.state().ArgumentVals[pIdx];
77   }
78 
79   // format - format this diagnostic into string, subsituting the formal
80   // arguments. The result is appended at on the pOutStr.
81   void format(std::string& pOutStr) const;
82 
83   // format - format the given formal string, subsituting the formal
84   // arguments. The result is appended at on the pOutStr.
85   void format(const char* pBegin, const char* pEnd, std::string& pOutStr) const;
86 
87  private:
88   const char* findMatch(char pVal, const char* pBegin, const char* pEnd) const;
89 
90  private:
91   DiagnosticEngine& m_Engine;
92 };
93 
94 }  // namespace mcld
95 
96 #endif  // MCLD_LD_DIAGNOSTIC_H_
97