summaryrefslogtreecommitdiff
path: root/lib/LD/DiagnosticInfos.cpp
blob: 0ba2e9c5f4309f582b6b300c383edd7fee980f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//===- DiagnosticInfo.cpp -------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "mcld/LD/DiagnosticInfos.h"

#include "mcld/LinkerConfig.h"
#include "mcld/ADT/SizeTraits.h"
#include "mcld/LD/Diagnostic.h"
#include "mcld/LD/DiagnosticPrinter.h"

#include <llvm/ADT/StringRef.h>
#include <llvm/Support/DataTypes.h>

#include <algorithm>

namespace mcld {

namespace {

struct DiagStaticInfo {
 public:
  uint16_t ID;
  DiagnosticEngine::Severity Severity;
  uint16_t DescriptionLen;
  const char* DescriptionStr;

 public:
  llvm::StringRef getDescription() const {
    return llvm::StringRef(DescriptionStr, DescriptionLen);
  }

  bool operator<(const DiagStaticInfo& pRHS) const { return (ID < pRHS.ID); }
};

}  // anonymous namespace

static const DiagStaticInfo DiagCommonInfo[] = {
#define DIAG(ENUM, CLASS, ADDRDESC, LOCDESC)                    \
  { diag::ENUM, CLASS, STR_SIZE(ADDRDESC, uint16_t), ADDRDESC } \
  ,
#include "mcld/LD/DiagAttribute.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagCommonKinds.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagReaders.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagSymbolResolutions.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagRelocations.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagLayouts.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagGOTPLT.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagLDScript.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagMips.inc"  // NOLINT [build/include] [4]
#undef DIAG
    {0, DiagnosticEngine::None, 0, 0}};

static const unsigned int DiagCommonInfoSize =
    sizeof(DiagCommonInfo) / sizeof(DiagCommonInfo[0]) - 1;

static const DiagStaticInfo DiagLoCInfo[] = {
#define DIAG(ENUM, CLASS, ADDRDESC, LOCDESC)                  \
  { diag::ENUM, CLASS, STR_SIZE(LOCDESC, uint16_t), LOCDESC } \
  ,
#include "mcld/LD/DiagAttribute.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagCommonKinds.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagReaders.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagSymbolResolutions.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagRelocations.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagLayouts.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagGOTPLT.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagLDScript.inc"  // NOLINT [build/include] [4]
#include "mcld/LD/DiagMips.inc"  // NOLINT [build/include] [4]
#undef DIAG
    {0, DiagnosticEngine::None, 0, 0}};

static const unsigned int DiagLoCInfoSize =
    sizeof(DiagLoCInfo) / sizeof(DiagLoCInfo[0]) - 1;

static const DiagStaticInfo* getDiagInfo(unsigned int pID,
                                         bool pInLoC = false) {
  const DiagStaticInfo* static_info = (pInLoC) ? DiagLoCInfo : DiagCommonInfo;
  unsigned int info_size = (pInLoC) ? DiagLoCInfoSize : DiagCommonInfoSize;

  DiagStaticInfo key = {
      static_cast<uint16_t>(pID), DiagnosticEngine::None, 0, 0};

  const DiagStaticInfo* result =
      std::lower_bound(static_info, static_info + info_size, key);

  if (result == (static_info + info_size) || result->ID != pID)
    return NULL;

  return result;
}

//===----------------------------------------------------------------------===//
//  DiagnosticInfos
//===----------------------------------------------------------------------===//
DiagnosticInfos::DiagnosticInfos(const LinkerConfig& pConfig)
    : m_Config(pConfig) {
}

DiagnosticInfos::~DiagnosticInfos() {
}

llvm::StringRef DiagnosticInfos::getDescription(unsigned int pID,
                                                bool pInLoC) const {
  return getDiagInfo(pID, pInLoC)->getDescription();
}

bool DiagnosticInfos::process(DiagnosticEngine& pEngine) const {
  Diagnostic info(pEngine);

  unsigned int ID = info.getID();

  // we are not implement LineInfo, so keep pIsLoC false.
  const DiagStaticInfo* static_info = getDiagInfo(ID);

  DiagnosticEngine::Severity severity = static_info->Severity;

  switch (ID) {
    case diag::multiple_definitions: {
      if (m_Config.options().isMulDefs()) {
        severity = DiagnosticEngine::Ignore;
      }
      break;
    }
    case diag::undefined_reference:
    case diag::undefined_reference_text: {
      // we have not implement --unresolved-symbols=method yet. So far, MCLinker
      // provides the easier --allow-shlib-undefined and --no-undefined (i.e.
      // -z defs)
      switch (m_Config.codeGenType()) {
        case LinkerConfig::Object:
          if (m_Config.options().isNoUndefined())
            severity = DiagnosticEngine::Error;
          else
            severity = DiagnosticEngine::Ignore;
          break;
        case LinkerConfig::DynObj:
          if (m_Config.options().isNoUndefined())
            severity = DiagnosticEngine::Error;
          else
            severity = DiagnosticEngine::Ignore;
          break;
        default:
          severity = DiagnosticEngine::Error;
          break;
      }
      break;
    }
    case diag::debug_print_gc_sections: {
      if (!m_Config.options().getPrintGCSections())
        severity = DiagnosticEngine::Ignore;
      break;
    }
    default:
      break;
  }  // end of switch

  // If --fatal-warnings is turned on, then switch warnings and errors to fatal
  if (m_Config.options().isFatalWarnings()) {
    if (severity == DiagnosticEngine::Warning ||
        severity == DiagnosticEngine::Error) {
      severity = DiagnosticEngine::Fatal;
    }
  }

  // finally, report it.
  pEngine.getPrinter()->handleDiagnostic(severity, info);
  return true;
}

}  // namespace mcld