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_LD_MSGHANDLER_H_
10 #define MCLD_LD_MSGHANDLER_H_
11 #include "mcld/LD/DiagnosticEngine.h"
12 #include "mcld/Support/Path.h"
13 
14 #include <llvm/ADT/StringRef.h>
15 #include <llvm/ADT/Twine.h>
16 
17 #include <string>
18 
19 namespace mcld {
20 
21 /** \class MsgHandler
22  *  \brief MsgHandler controls the timing to output message.
23  */
24 class MsgHandler {
25  public:
26   explicit MsgHandler(DiagnosticEngine& pEngine);
27   ~MsgHandler();
28 
29   bool emit();
30 
31   void addString(llvm::StringRef pStr) const;
32 
33   void addString(const std::string& pStr) const;
34 
35   void addTaggedVal(intptr_t pValue,
36                     DiagnosticEngine::ArgumentKind pKind) const;
37 
38  private:
flushCounts()39   void flushCounts() { m_Engine.state().numArgs = m_NumArgs; }
40 
41  private:
42   DiagnosticEngine& m_Engine;
43   mutable unsigned int m_NumArgs;
44 };
45 
46 inline const MsgHandler& operator<<(const MsgHandler& pHandler,
47                                     llvm::StringRef pStr) {
48   pHandler.addString(pStr);
49   return pHandler;
50 }
51 
52 inline const MsgHandler& operator<<(const MsgHandler& pHandler,
53                                     const std::string& pStr) {
54   pHandler.addString(pStr);
55   return pHandler;
56 }
57 
58 inline const MsgHandler& operator<<(const MsgHandler& pHandler,
59                                     const sys::fs::Path& pPath) {
60   pHandler.addString(pPath.native());
61   return pHandler;
62 }
63 
64 inline const MsgHandler& operator<<(const MsgHandler& pHandler,
65                                     const char* pStr) {
66   pHandler.addTaggedVal(reinterpret_cast<intptr_t>(pStr),
67                         DiagnosticEngine::ak_c_string);
68   return pHandler;
69 }
70 
71 inline const MsgHandler& operator<<(const MsgHandler& pHandler, int pValue) {
72   pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_sint);
73   return pHandler;
74 }
75 
76 inline const MsgHandler& operator<<(const MsgHandler& pHandler,
77                                     unsigned int pValue) {
78   pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_uint);
79   return pHandler;
80 }
81 
82 inline const MsgHandler& operator<<(const MsgHandler& pHandler, long pValue) {
83   pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_sint);
84   return pHandler;
85 }
86 
87 inline const MsgHandler& operator<<(const MsgHandler& pHandler,
88                                     unsigned long pValue) {
89   pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_uint);
90   return pHandler;
91 }
92 
93 inline const MsgHandler& operator<<(const MsgHandler& pHandler,
94                                     unsigned long long pValue) {
95   pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_ulonglong);
96   return pHandler;
97 }
98 
99 inline const MsgHandler& operator<<(const MsgHandler& pHandler, bool pValue) {
100   pHandler.addTaggedVal(pValue, DiagnosticEngine::ak_bool);
101   return pHandler;
102 }
103 
104 }  // namespace mcld
105 
106 #endif  // MCLD_LD_MSGHANDLER_H_
107