Diagnostic.h revision affc150dc44fab1911775a49636d0ce85333b634
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_DIAGNOSTIC_H 10#define MCLD_DIAGNOSTIC_H 11#ifdef ENABLE_UNITTEST 12#include <gtest.h> 13#endif 14 15#include <string> 16#include <cassert> 17#include <mcld/LD/DiagnosticEngine.h> 18 19namespace mcld 20{ 21 22/** \class Diagnostic 23 * \brief Diagnostic provides current status to DiagnosticPrinters. 24 */ 25class Diagnostic 26{ 27public: 28 Diagnostic(DiagnosticEngine& pEngine); 29 30 ~Diagnostic(); 31 32 unsigned int getID() const 33 { return m_Engine.state().ID; } 34 35 unsigned int getNumArgs() const 36 { return m_Engine.state().numArgs; } 37 38 DiagnosticEngine::ArgumentKind getArgKind(unsigned int pIdx) const { 39 assert(pIdx < getNumArgs() && "Argument index is out of range!"); 40 return (DiagnosticEngine::ArgumentKind)m_Engine.state().ArgumentKinds[pIdx]; 41 } 42 43 const std::string &getArgStdStr(unsigned int pIdx) const { 44 assert(getArgKind(pIdx) == DiagnosticEngine::ak_std_string && 45 "Invalid argument accessor!"); 46 return m_Engine.state().ArgumentStrs[pIdx]; 47 } 48 49 const char* getArgCStr(unsigned int pIdx) const { 50 assert(getArgKind(pIdx) == DiagnosticEngine::ak_c_string && 51 "Invalid argument accessor!"); 52 return reinterpret_cast<const char*>(m_Engine.state().ArgumentVals[pIdx]); 53 } 54 55 int getArgSInt(unsigned int pIdx) const { 56 assert(getArgKind(pIdx) == DiagnosticEngine::ak_sint && 57 "Invalid argument accessor!"); 58 return (int)m_Engine.state().ArgumentVals[pIdx]; 59 } 60 61 unsigned int getArgUInt(unsigned int pIdx) const { 62 assert(getArgKind(pIdx) == DiagnosticEngine::ak_uint && 63 "Invalid argument accessor!"); 64 return (unsigned int)m_Engine.state().ArgumentVals[pIdx]; 65 } 66 67 bool getArgBool(unsigned int pIdx) const { 68 assert(getArgKind(pIdx) == DiagnosticEngine::ak_bool && 69 "Invalid argument accessor!"); 70 return (bool)m_Engine.state().ArgumentVals[pIdx]; 71 } 72 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 87private: 88 const char* findMatch(char pVal, const char* pBegin, const char* pEnd ) const; 89 90private: 91 DiagnosticEngine& m_Engine; 92}; 93 94} // namespace of mcld 95 96#endif 97 98