Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame^] | 1 | //===- MsgHandler.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_MESSAGE_HANDLER_H |
| 10 | #define MCLD_MESSAGE_HANDLER_H |
| 11 | #ifdef ENABLE_UNITTEST |
| 12 | #include <gtest.h> |
| 13 | #endif |
| 14 | #include <string> |
| 15 | #include <llvm/ADT/StringRef.h> |
| 16 | #include <llvm/ADT/Twine.h> |
| 17 | #include <mcld/Support/Path.h> |
| 18 | #include <mcld/LD/DiagnosticEngine.h> |
| 19 | |
| 20 | namespace mcld |
| 21 | { |
| 22 | |
| 23 | /** \class MsgHandler |
| 24 | * \brief MsgHandler controls the timing to output message. |
| 25 | */ |
| 26 | class MsgHandler |
| 27 | { |
| 28 | public: |
| 29 | MsgHandler(DiagnosticEngine& pEngine); |
| 30 | ~MsgHandler(); |
| 31 | |
| 32 | bool emit(); |
| 33 | |
| 34 | void addString(llvm::StringRef pStr) const; |
| 35 | |
| 36 | void addString(const std::string& pStr) const; |
| 37 | |
| 38 | void addTaggedVal(intptr_t pValue, DiagnosticEngine::ArgumentKind pKind) const; |
| 39 | |
| 40 | private: |
| 41 | void flushCounts() |
| 42 | { m_Engine.state().numArgs = m_NumArgs; } |
| 43 | |
| 44 | private: |
| 45 | DiagnosticEngine& m_Engine; |
| 46 | mutable unsigned int m_NumArgs; |
| 47 | }; |
| 48 | |
| 49 | inline const MsgHandler & |
| 50 | operator<<(const MsgHandler& pHandler, llvm::StringRef pStr) |
| 51 | { |
| 52 | pHandler.addString(pStr); |
| 53 | return pHandler; |
| 54 | } |
| 55 | |
| 56 | inline const MsgHandler & |
| 57 | operator<<(const MsgHandler& pHandler, const std::string& pStr) |
| 58 | { |
| 59 | pHandler.addString(pStr); |
| 60 | return pHandler; |
| 61 | } |
| 62 | |
| 63 | inline const MsgHandler & |
| 64 | operator<<(const MsgHandler& pHandler, const sys::fs::Path& pPath) |
| 65 | { |
| 66 | pHandler.addString(pPath.native()); |
| 67 | return pHandler; |
| 68 | } |
| 69 | |
| 70 | inline const MsgHandler & |
| 71 | operator<<(const MsgHandler& pHandler, const char* pStr) |
| 72 | { |
| 73 | pHandler.addTaggedVal(reinterpret_cast<intptr_t>(pStr), |
| 74 | DiagnosticEngine::ak_c_string); |
| 75 | return pHandler; |
| 76 | } |
| 77 | |
| 78 | inline const MsgHandler & |
| 79 | operator<<(const MsgHandler& pHandler, int pValue) |
| 80 | { |
| 81 | pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_sint); |
| 82 | return pHandler; |
| 83 | } |
| 84 | |
| 85 | inline const MsgHandler & |
| 86 | operator<<(const MsgHandler& pHandler, unsigned int pValue) |
| 87 | { |
| 88 | pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_uint); |
| 89 | return pHandler; |
| 90 | } |
| 91 | |
| 92 | inline const MsgHandler & |
| 93 | operator<<(const MsgHandler& pHandler, long pValue) |
| 94 | { |
| 95 | pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_sint); |
| 96 | return pHandler; |
| 97 | } |
| 98 | |
| 99 | inline const MsgHandler & |
| 100 | operator<<(const MsgHandler& pHandler, unsigned long pValue) |
| 101 | { |
| 102 | pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_uint); |
| 103 | return pHandler; |
| 104 | } |
| 105 | |
| 106 | inline const MsgHandler & |
| 107 | operator<<(const MsgHandler& pHandler, bool pValue) |
| 108 | { |
| 109 | pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_bool); |
| 110 | return pHandler; |
| 111 | } |
| 112 | |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | // Inline member functions |
| 115 | inline MsgHandler |
| 116 | DiagnosticEngine::report(uint16_t pID, DiagnosticEngine::Severity pSeverity) |
| 117 | { |
| 118 | m_State.ID = pID; |
| 119 | m_State.severity = pSeverity; |
| 120 | |
| 121 | MsgHandler result(*this); |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | } // namespace of mcld |
| 126 | |
| 127 | #endif |
| 128 | |