Home | History | Annotate | Download | only in LD
      1 //===- DiagnosticEngine.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_ENGINE_H
     10 #define MCLD_DIAGNOSTIC_ENGINE_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 #include <string>
     15 #include <llvm/Support/DataTypes.h>
     16 #include <mcld/LD/DiagnosticInfos.h>
     17 
     18 namespace mcld {
     19 
     20 class Input;
     21 class LinkerConfig;
     22 class MsgHandler;
     23 class DiagnosticPrinter;
     24 class DiagnosticLineInfo;
     25 
     26 /** \class DiagnosticEngine
     27  *  \brief DiagnosticEngine is used to report problems and issues.
     28  *
     29  *  DiagnosticEngine is used to report problems and issues. It creates the
     30  *  Diagnostics and passes them to the DiagnosticPrinter for reporting to the
     31  *  user.
     32  *
     33  *  DiagnosticEngine is a complex class, it is responsible for
     34  *  - remember the argument string for MsgHandler
     35  *  - choice the severity of a message by options
     36  */
     37 class DiagnosticEngine
     38 {
     39 public:
     40   enum Severity {
     41     Unreachable,
     42     Fatal,
     43     Error,
     44     Warning,
     45     Debug,
     46     Note,
     47     Ignore,
     48     None
     49   };
     50 
     51   enum ArgumentKind {
     52     ak_std_string,  // std::string
     53     ak_c_string,    // const char *
     54     ak_sint,        // int
     55     ak_uint,        // unsigned int
     56     ak_ulonglong,   // unsigned long long
     57     ak_bool         // bool
     58   };
     59 
     60 public:
     61   DiagnosticEngine();
     62 
     63   ~DiagnosticEngine();
     64 
     65   void reset(const LinkerConfig& pConfig);
     66 
     67   void setLineInfo(DiagnosticLineInfo& pLineInfo);
     68 
     69   void setPrinter(DiagnosticPrinter& pPrinter, bool pShouldOwnPrinter = true);
     70 
     71   const DiagnosticPrinter* getPrinter() const { return m_pPrinter; }
     72   DiagnosticPrinter*       getPrinter()       { return m_pPrinter; }
     73 
     74 
     75   DiagnosticPrinter* takePrinter() {
     76     m_OwnPrinter = false;
     77     return m_pPrinter;
     78   }
     79 
     80   bool ownPrinter() const
     81   { return m_OwnPrinter; }
     82 
     83   // -----  emission  ----- //
     84   // emit - process the message to printer
     85   bool emit();
     86 
     87   // report - issue the message to the printer
     88   MsgHandler report(uint16_t pID, Severity pSeverity);
     89 
     90 private:
     91   friend class MsgHandler;
     92   friend class Diagnostic;
     93 
     94   enum {
     95     /// MaxArguments - The maximum number of arguments we can hold. We currently
     96     /// only support up to 10 arguments (%0-%9).
     97     MaxArguments = 10
     98   };
     99 
    100   struct State
    101   {
    102   public:
    103     State() : numArgs(0), ID(-1), severity(None), file(NULL) { }
    104     ~State() { }
    105 
    106     void reset() {
    107       numArgs = 0;
    108       ID = -1;
    109       severity = None;
    110       file = NULL;
    111     }
    112 
    113   public:
    114     std::string ArgumentStrs[MaxArguments];
    115     intptr_t ArgumentVals[MaxArguments];
    116     uint8_t ArgumentKinds[MaxArguments];
    117     int8_t numArgs;
    118     uint16_t ID;
    119     Severity severity;
    120     Input* file;
    121   };
    122 
    123 private:
    124   State& state()
    125   { return m_State; }
    126 
    127   const State& state() const
    128   { return m_State; }
    129 
    130   DiagnosticInfos& infoMap() {
    131     assert(NULL != m_pInfoMap && "DiagnosticEngine was not initialized!");
    132     return *m_pInfoMap;
    133   }
    134 
    135   const DiagnosticInfos& infoMap() const {
    136     assert(NULL != m_pInfoMap && "DiagnosticEngine was not initialized!");
    137     return *m_pInfoMap;
    138   }
    139 
    140 private:
    141   const LinkerConfig* m_pConfig;
    142   DiagnosticLineInfo* m_pLineInfo;
    143   DiagnosticPrinter* m_pPrinter;
    144   DiagnosticInfos* m_pInfoMap;
    145   bool m_OwnPrinter;
    146 
    147   State m_State;
    148 };
    149 
    150 } // namespace of mcld
    151 
    152 #endif
    153 
    154