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