blob: 0ba2e9c5f4309f582b6b300c383edd7fee980f18 [file] [log] [blame]
Zonr Changaffc1502012-07-16 14:28:23 +08001//===- DiagnosticInfo.cpp -------------------------------------------------===//
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//===----------------------------------------------------------------------===//
Stephen Hines37b74a32014-11-26 18:48:20 -08009#include "mcld/LD/DiagnosticInfos.h"
10
11#include "mcld/LinkerConfig.h"
12#include "mcld/ADT/SizeTraits.h"
13#include "mcld/LD/Diagnostic.h"
14#include "mcld/LD/DiagnosticPrinter.h"
15
Zonr Changaffc1502012-07-16 14:28:23 +080016#include <llvm/ADT/StringRef.h>
17#include <llvm/Support/DataTypes.h>
18
Shih-wei Liao22add6f2012-12-15 17:21:00 -080019#include <algorithm>
20
Stephen Hines37b74a32014-11-26 18:48:20 -080021namespace mcld {
Zonr Changaffc1502012-07-16 14:28:23 +080022
23namespace {
24
Stephen Hines37b74a32014-11-26 18:48:20 -080025struct DiagStaticInfo {
26 public:
Zonr Changaffc1502012-07-16 14:28:23 +080027 uint16_t ID;
28 DiagnosticEngine::Severity Severity;
29 uint16_t DescriptionLen;
30 const char* DescriptionStr;
31
Stephen Hines37b74a32014-11-26 18:48:20 -080032 public:
33 llvm::StringRef getDescription() const {
34 return llvm::StringRef(DescriptionStr, DescriptionLen);
35 }
Zonr Changaffc1502012-07-16 14:28:23 +080036
Stephen Hines37b74a32014-11-26 18:48:20 -080037 bool operator<(const DiagStaticInfo& pRHS) const { return (ID < pRHS.ID); }
Zonr Changaffc1502012-07-16 14:28:23 +080038};
39
Stephen Hines37b74a32014-11-26 18:48:20 -080040} // anonymous namespace
Zonr Changaffc1502012-07-16 14:28:23 +080041
42static const DiagStaticInfo DiagCommonInfo[] = {
Stephen Hines37b74a32014-11-26 18:48:20 -080043#define DIAG(ENUM, CLASS, ADDRDESC, LOCDESC) \
44 { diag::ENUM, CLASS, STR_SIZE(ADDRDESC, uint16_t), ADDRDESC } \
45 ,
46#include "mcld/LD/DiagAttribute.inc" // NOLINT [build/include] [4]
47#include "mcld/LD/DiagCommonKinds.inc" // NOLINT [build/include] [4]
48#include "mcld/LD/DiagReaders.inc" // NOLINT [build/include] [4]
49#include "mcld/LD/DiagSymbolResolutions.inc" // NOLINT [build/include] [4]
50#include "mcld/LD/DiagRelocations.inc" // NOLINT [build/include] [4]
51#include "mcld/LD/DiagLayouts.inc" // NOLINT [build/include] [4]
52#include "mcld/LD/DiagGOTPLT.inc" // NOLINT [build/include] [4]
53#include "mcld/LD/DiagLDScript.inc" // NOLINT [build/include] [4]
Stephen Hinescfcb2242016-03-08 00:18:09 -080054#include "mcld/LD/DiagMips.inc" // NOLINT [build/include] [4]
Zonr Changaffc1502012-07-16 14:28:23 +080055#undef DIAG
Stephen Hines37b74a32014-11-26 18:48:20 -080056 {0, DiagnosticEngine::None, 0, 0}};
Zonr Changaffc1502012-07-16 14:28:23 +080057
58static const unsigned int DiagCommonInfoSize =
Stephen Hines37b74a32014-11-26 18:48:20 -080059 sizeof(DiagCommonInfo) / sizeof(DiagCommonInfo[0]) - 1;
Zonr Changaffc1502012-07-16 14:28:23 +080060
61static const DiagStaticInfo DiagLoCInfo[] = {
Stephen Hines37b74a32014-11-26 18:48:20 -080062#define DIAG(ENUM, CLASS, ADDRDESC, LOCDESC) \
63 { diag::ENUM, CLASS, STR_SIZE(LOCDESC, uint16_t), LOCDESC } \
64 ,
65#include "mcld/LD/DiagAttribute.inc" // NOLINT [build/include] [4]
66#include "mcld/LD/DiagCommonKinds.inc" // NOLINT [build/include] [4]
67#include "mcld/LD/DiagReaders.inc" // NOLINT [build/include] [4]
68#include "mcld/LD/DiagSymbolResolutions.inc" // NOLINT [build/include] [4]
69#include "mcld/LD/DiagRelocations.inc" // NOLINT [build/include] [4]
70#include "mcld/LD/DiagLayouts.inc" // NOLINT [build/include] [4]
71#include "mcld/LD/DiagGOTPLT.inc" // NOLINT [build/include] [4]
72#include "mcld/LD/DiagLDScript.inc" // NOLINT [build/include] [4]
Stephen Hinescfcb2242016-03-08 00:18:09 -080073#include "mcld/LD/DiagMips.inc" // NOLINT [build/include] [4]
Zonr Changaffc1502012-07-16 14:28:23 +080074#undef DIAG
Stephen Hines37b74a32014-11-26 18:48:20 -080075 {0, DiagnosticEngine::None, 0, 0}};
Zonr Changaffc1502012-07-16 14:28:23 +080076
77static const unsigned int DiagLoCInfoSize =
Stephen Hines37b74a32014-11-26 18:48:20 -080078 sizeof(DiagLoCInfo) / sizeof(DiagLoCInfo[0]) - 1;
Zonr Changaffc1502012-07-16 14:28:23 +080079
Stephen Hines37b74a32014-11-26 18:48:20 -080080static const DiagStaticInfo* getDiagInfo(unsigned int pID,
81 bool pInLoC = false) {
82 const DiagStaticInfo* static_info = (pInLoC) ? DiagLoCInfo : DiagCommonInfo;
83 unsigned int info_size = (pInLoC) ? DiagLoCInfoSize : DiagCommonInfoSize;
Zonr Changaffc1502012-07-16 14:28:23 +080084
Stephen Hines37b74a32014-11-26 18:48:20 -080085 DiagStaticInfo key = {
86 static_cast<uint16_t>(pID), DiagnosticEngine::None, 0, 0};
Zonr Changaffc1502012-07-16 14:28:23 +080087
Stephen Hines37b74a32014-11-26 18:48:20 -080088 const DiagStaticInfo* result =
89 std::lower_bound(static_info, static_info + info_size, key);
Zonr Changaffc1502012-07-16 14:28:23 +080090
91 if (result == (static_info + info_size) || result->ID != pID)
92 return NULL;
93
94 return result;
95}
96
97//===----------------------------------------------------------------------===//
98// DiagnosticInfos
Shih-wei Liao22add6f2012-12-15 17:21:00 -080099//===----------------------------------------------------------------------===//
100DiagnosticInfos::DiagnosticInfos(const LinkerConfig& pConfig)
Stephen Hines37b74a32014-11-26 18:48:20 -0800101 : m_Config(pConfig) {
Zonr Changaffc1502012-07-16 14:28:23 +0800102}
103
Stephen Hines37b74a32014-11-26 18:48:20 -0800104DiagnosticInfos::~DiagnosticInfos() {
Zonr Changaffc1502012-07-16 14:28:23 +0800105}
106
Stephen Hines37b74a32014-11-26 18:48:20 -0800107llvm::StringRef DiagnosticInfos::getDescription(unsigned int pID,
108 bool pInLoC) const {
Zonr Changaffc1502012-07-16 14:28:23 +0800109 return getDiagInfo(pID, pInLoC)->getDescription();
110}
111
Stephen Hines37b74a32014-11-26 18:48:20 -0800112bool DiagnosticInfos::process(DiagnosticEngine& pEngine) const {
Zonr Changaffc1502012-07-16 14:28:23 +0800113 Diagnostic info(pEngine);
114
115 unsigned int ID = info.getID();
116
117 // we are not implement LineInfo, so keep pIsLoC false.
118 const DiagStaticInfo* static_info = getDiagInfo(ID);
119
120 DiagnosticEngine::Severity severity = static_info->Severity;
121
122 switch (ID) {
123 case diag::multiple_definitions: {
Stephen Hines87f34652014-02-14 18:00:16 -0800124 if (m_Config.options().isMulDefs()) {
Zonr Changaffc1502012-07-16 14:28:23 +0800125 severity = DiagnosticEngine::Ignore;
126 }
127 break;
128 }
Stephen Hines87f34652014-02-14 18:00:16 -0800129 case diag::undefined_reference:
130 case diag::undefined_reference_text: {
Zonr Changaffc1502012-07-16 14:28:23 +0800131 // we have not implement --unresolved-symbols=method yet. So far, MCLinker
Stephen Hines37b74a32014-11-26 18:48:20 -0800132 // provides the easier --allow-shlib-undefined and --no-undefined (i.e.
133 // -z defs)
134 switch (m_Config.codeGenType()) {
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800135 case LinkerConfig::Object:
136 if (m_Config.options().isNoUndefined())
Zonr Changaffc1502012-07-16 14:28:23 +0800137 severity = DiagnosticEngine::Error;
138 else
139 severity = DiagnosticEngine::Ignore;
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800140 break;
141 case LinkerConfig::DynObj:
Stephen Hines6f757552013-03-04 19:51:03 -0800142 if (m_Config.options().isNoUndefined())
Zonr Changaffc1502012-07-16 14:28:23 +0800143 severity = DiagnosticEngine::Error;
144 else
145 severity = DiagnosticEngine::Ignore;
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800146 break;
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800147 default:
Zonr Changaffc1502012-07-16 14:28:23 +0800148 severity = DiagnosticEngine::Error;
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800149 break;
Zonr Changaffc1502012-07-16 14:28:23 +0800150 }
151 break;
152 }
Stephen Hines0dea6bc2014-07-15 18:33:32 -0700153 case diag::debug_print_gc_sections: {
154 if (!m_Config.options().getPrintGCSections())
155 severity = DiagnosticEngine::Ignore;
156 break;
157 }
Zonr Changaffc1502012-07-16 14:28:23 +0800158 default:
159 break;
Stephen Hines37b74a32014-11-26 18:48:20 -0800160 } // end of switch
Zonr Changaffc1502012-07-16 14:28:23 +0800161
Shih-wei Liaod0fbbb22013-01-03 06:23:31 -0800162 // If --fatal-warnings is turned on, then switch warnings and errors to fatal
163 if (m_Config.options().isFatalWarnings()) {
164 if (severity == DiagnosticEngine::Warning ||
165 severity == DiagnosticEngine::Error) {
166 severity = DiagnosticEngine::Fatal;
167 }
168 }
169
Zonr Changaffc1502012-07-16 14:28:23 +0800170 // finally, report it.
171 pEngine.getPrinter()->handleDiagnostic(severity, info);
172 return true;
173}
174
Stephen Hines37b74a32014-11-26 18:48:20 -0800175} // namespace mcld