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