llvm.org GIT mirror llvm / 72478e5
Update the error handling of lib/Linker. Instead of passing a std::string&, use the new diagnostic infrastructure. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220608 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
12 changed file(s) with 133 addition(s) and 141 deletion(s). Raw diff Collapse all Expand all
4646 enum DiagnosticKind {
4747 DK_InlineAsm,
4848 DK_StackSize,
49 DK_Linker,
4950 DK_DebugMetadataVersion,
5051 DK_SampleProfile,
5152 DK_OptimizationRemark,
6363 ~LTOCodeGenerator();
6464
6565 // Merge given module, return true on success.
66 bool addModule(struct LTOModule*, std::string &errMsg);
66 bool addModule(struct LTOModule *);
6767
6868 void setTargetOptions(TargetOptions options);
6969 void setDebugInfo(lto_debug_model);
1010 #define LLVM_LINKER_LINKER_H
1111
1212 #include "llvm/ADT/SmallPtrSet.h"
13 #include
1413
1514 namespace llvm {
1615
17 class Comdat;
18 class GlobalValue;
1916 class Module;
20 class StringRef;
2117 class StructType;
2218
2319 /// This class provides the core functionality of linking in LLVM. It keeps a
3127 PreserveSource = 1 // Preserve the source module.
3228 };
3329
34 Linker(Module *M, bool SuppressWarnings=false);
30 Linker(Module *M);
3531 ~Linker();
3632
3733 Module *getModule() const { return Composite; }
4238 /// If \p ErrorMsg is not null, information about any error is written
4339 /// to it.
4440 /// Returns true on error.
45 bool linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg);
46 bool linkInModule(Module *Src, std::string *ErrorMsg) {
47 return linkInModule(Src, Linker::DestroySource, ErrorMsg);
41 bool linkInModule(Module *Src, unsigned Mode);
42 bool linkInModule(Module *Src) {
43 return linkInModule(Src, Linker::DestroySource);
4844 }
4945
50 static bool LinkModules(Module *Dest, Module *Src, unsigned Mode,
51 std::string *ErrorMsg);
46 static bool LinkModules(Module *Dest, Module *Src, unsigned Mode);
5247
5348 private:
5449 Module *Composite;
5550 SmallPtrSet IdentifiedStructTypes;
56
57 bool SuppressWarnings;
5851 };
5952
6053 } // End llvm namespace
112112 initializeCFGSimplifyPassPass(R);
113113 }
114114
115 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
116 bool ret = IRLinker.linkInModule(&mod->getModule(), &errMsg);
115 bool LTOCodeGenerator::addModule(LTOModule *mod) {
116 bool ret = IRLinker.linkInModule(&mod->getModule());
117117
118118 const std::vector &undefs = mod->getAsmUndefinedRefs();
119119 for (int i = 0, e = undefs.size(); i != e; ++i)
1616 #include "llvm/ADT/SetVector.h"
1717 #include "llvm/ADT/SmallString.h"
1818 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/IR/LLVMContext.h"
1922 #include "llvm/IR/Module.h"
2023 #include "llvm/IR/TypeFinder.h"
2124 #include "llvm/Support/CommandLine.h"
375378 Value *materializeValueFor(Value *V) override;
376379 };
377380
381 namespace {
382 class LinkDiagnosticInfo : public DiagnosticInfo {
383 const Twine &Msg;
384
385 public:
386 LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg);
387 void print(DiagnosticPrinter &DP) const override;
388 };
389 LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
390 const Twine &Msg)
391 : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
392 void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
393 }
394
378395 /// ModuleLinker - This is an implementation class for the LinkModules
379396 /// function, which is the entrypoint for this file.
380397 class ModuleLinker {
405422 // Vector of functions to lazily link in.
406423 std::vector LazilyLinkFunctions;
407424
408 bool SuppressWarnings;
409
410425 public:
411 std::string ErrorMsg;
412
413 ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM, unsigned mode,
414 bool SuppressWarnings=false)
426 ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM, unsigned mode)
415427 : DstM(dstM), SrcM(srcM), TypeMap(Set),
416 ValMaterializer(TypeMap, DstM, LazilyLinkFunctions), Mode(mode),
417 SuppressWarnings(SuppressWarnings) {}
428 ValMaterializer(TypeMap, DstM, LazilyLinkFunctions), Mode(mode) {}
418429
419430 bool run();
420431
421432 private:
422 bool shouldLinkFromSource(const GlobalValue &Dest, const GlobalValue &Src);
423
424 /// emitError - Helper method for setting a message and returning an error
425 /// code.
433 bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
434 const GlobalValue &Src);
435
436 /// Helper method for setting a message and returning an error code.
426437 bool emitError(const Twine &Message) {
427 ErrorMsg = Message.str();
438 DstM->getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
428439 return true;
440 }
441
442 void emitWarning(const Twine &Message) {
443 DstM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
429444 }
430445
431446 bool getComdatLeader(Module *M, StringRef ComdatName,
671686 LinkFromSrc);
672687 }
673688
674 bool ModuleLinker::shouldLinkFromSource(const GlobalValue &Dest,
689 bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
690 const GlobalValue &Dest,
675691 const GlobalValue &Src) {
676692 bool SrcIsDeclaration = Src.isDeclarationForLinker();
677693 bool DestIsDeclaration = Dest.isDeclarationForLinker();
682698 if (SrcIsDeclaration) {
683699 // If Src is external or if both Src & Dest are external.. Just link the
684700 // external globals, we aren't adding anything.
685 if (Src.hasDLLImportStorageClass())
701 if (Src.hasDLLImportStorageClass()) {
686702 // If one of GVs is marked as DLLImport, result should be dllimport'ed.
687 return DestIsDeclaration;
703 LinkFromSrc = DestIsDeclaration;
704 return false;
705 }
688706 // If the Dest is weak, use the source linkage.
689 return Dest.hasExternalWeakLinkage();
690 }
691
692 if (DestIsDeclaration)
707 LinkFromSrc = Dest.hasExternalWeakLinkage();
708 return false;
709 }
710
711 if (DestIsDeclaration) {
693712 // If Dest is external but Src is not:
694 return true;
713 LinkFromSrc = true;
714 return false;
715 }
695716
696717 if (Src.hasCommonLinkage()) {
697 if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage())
698 return true;
699
700 if (!Dest.hasCommonLinkage())
718 if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
719 LinkFromSrc = true;
701720 return false;
721 }
722
723 if (!Dest.hasCommonLinkage()) {
724 LinkFromSrc = false;
725 return false;
726 }
702727
703728 uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
704729 uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
705 return SrcSize > DestSize;
730 LinkFromSrc = SrcSize > DestSize;
731 return false;
706732 }
707733
708734 if (Src.isWeakForLinker()) {
709735 assert(!Dest.hasExternalWeakLinkage());
710736 assert(!Dest.hasAvailableExternallyLinkage());
711737
712 if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage())
713 return true;
714
738 if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
739 LinkFromSrc = true;
740 return false;
741 }
742
743 LinkFromSrc = false;
715744 return false;
716745 }
717746
718747 if (Dest.isWeakForLinker()) {
719748 assert(Src.hasExternalLinkage());
720 return true;
749 LinkFromSrc = true;
750 return false;
721751 }
722752
723753 assert(!Src.hasExternalWeakLinkage());
741771 assert(!Src->hasLocalLinkage() &&
742772 "If Src has internal linkage, Dest shouldn't be set!");
743773
744 assert(ErrorMsg.empty());
745 LinkFromSrc = shouldLinkFromSource(*Dest, *Src);
746 if (!ErrorMsg.empty())
774 if (shouldLinkFromSource(LinkFromSrc, *Dest, *Src))
747775 return true;
748776
749777 if (LinkFromSrc)
14691497 case Module::Warning: {
14701498 // Emit a warning if the values differ.
14711499 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1472 if (!SuppressWarnings) {
1473 errs() << "WARNING: linking module flags '" << ID->getString()
1474 << "': IDs have conflicting values";
1475 }
1500 emitWarning("linking module flags '" + ID->getString() +
1501 "': IDs have conflicting values");
14761502 }
14771503 continue;
14781504 }
15391565
15401566 if (SrcM->getDataLayout() && DstM->getDataLayout() &&
15411567 *SrcM->getDataLayout() != *DstM->getDataLayout()) {
1542 if (!SuppressWarnings) {
1543 errs() << "WARNING: Linking two modules of different data layouts: '"
1544 << SrcM->getModuleIdentifier() << "' is '"
1545 << SrcM->getDataLayoutStr() << "' whereas '"
1546 << DstM->getModuleIdentifier() << "' is '"
1547 << DstM->getDataLayoutStr() << "'\n";
1548 }
1568 emitWarning("Linking two modules of different data layouts: '" +
1569 SrcM->getModuleIdentifier() + "' is '" +
1570 SrcM->getDataLayoutStr() + "' whereas '" +
1571 DstM->getModuleIdentifier() + "' is '" +
1572 DstM->getDataLayoutStr() + "'\n");
15491573 }
15501574 if (!SrcM->getTargetTriple().empty() &&
15511575 DstM->getTargetTriple() != SrcM->getTargetTriple()) {
1552 if (!SuppressWarnings) {
1553 errs() << "WARNING: Linking two modules of different target triples: "
1554 << SrcM->getModuleIdentifier() << "' is '"
1555 << SrcM->getTargetTriple() << "' whereas '"
1556 << DstM->getModuleIdentifier() << "' is '"
1557 << DstM->getTargetTriple() << "'\n";
1558 }
1576 emitWarning("Linking two modules of different target triples: " +
1577 SrcM->getModuleIdentifier() + "' is '" +
1578 SrcM->getTargetTriple() + "' whereas '" +
1579 DstM->getModuleIdentifier() + "' is '" +
1580 DstM->getTargetTriple() + "'\n");
15591581 }
15601582
15611583 // Append the module inline asm string.
16251647
16261648 // Materialize if needed.
16271649 if (SF->isMaterializable()) {
1628 if (std::error_code EC = SF->materialize()) {
1629 ErrorMsg = EC.message();
1630 return true;
1631 }
1650 if (std::error_code EC = SF->materialize())
1651 return emitError(EC.message());
16321652 }
16331653
16341654 // Skip if no body (function is external).
16781698
16791699 // Materialize if needed.
16801700 if (SF->isMaterializable()) {
1681 if (std::error_code EC = SF->materialize()) {
1682 ErrorMsg = EC.message();
1683 return true;
1684 }
1701 if (std::error_code EC = SF->materialize())
1702 return emitError(EC.message());
16851703 }
16861704
16871705 // Skip if no body (function is external).
17101728 return false;
17111729 }
17121730
1713 Linker::Linker(Module *M, bool SuppressWarnings)
1714 : Composite(M), SuppressWarnings(SuppressWarnings) {
1731 Linker::Linker(Module *M) : Composite(M) {
17151732 TypeFinder StructTypes;
17161733 StructTypes.run(*M, true);
17171734 IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end());
17251742 Composite = nullptr;
17261743 }
17271744
1728 bool Linker::linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg) {
1729 ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, Mode,
1730 SuppressWarnings);
1731 if (TheLinker.run()) {
1732 if (ErrorMsg)
1733 *ErrorMsg = TheLinker.ErrorMsg;
1734 return true;
1735 }
1736 return false;
1745 bool Linker::linkInModule(Module *Src, unsigned Mode) {
1746 ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, Mode);
1747 return TheLinker.run();
17371748 }
17381749
17391750 //===----------------------------------------------------------------------===//
17451756 /// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
17461757 /// the problem. Upon failure, the Dest module could be in a modified state,
17471758 /// and shouldn't be relied on to be consistent.
1748 bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode,
1749 std::string *ErrorMsg) {
1759 bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode) {
17501760 Linker L(Dest);
1751 return L.linkInModule(Src, Mode, ErrorMsg);
1761 return L.linkInModule(Src, Mode);
17521762 }
17531763
17541764 //===----------------------------------------------------------------------===//
17571767
17581768 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
17591769 LLVMLinkerMode Mode, char **OutMessages) {
1760 std::string Messages;
1761 LLVMBool Result = Linker::LinkModules(unwrap(Dest), unwrap(Src),
1762 Mode, OutMessages? &Messages : nullptr);
1763 if (OutMessages)
1764 *OutMessages = strdup(Messages.c_str());
1770 LLVMBool Result = Linker::LinkModules(unwrap(Dest), unwrap(Src), Mode);
17651771 return Result;
17661772 }
125125 if (!M.get()) return true;
126126
127127 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
128 std::string ErrorMessage;
129 if (Linker::LinkModules(Program, M.get(), Linker::DestroySource,
130 &ErrorMessage)) {
131 errs() << ToolName << ": error linking in '" << Filenames[i] << "': "
132 << ErrorMessage << '\n';
128 if (Linker::LinkModules(Program, M.get(), Linker::DestroySource))
133129 return true;
134 }
135130 }
136131
137132 outs() << "*** All input ok\n";
217217 bool DeleteInputs, std::string &Error,
218218 bool &Broken) {
219219 // Link the two portions of the program back to together.
220 std::string ErrorMsg;
221220 if (!DeleteInputs) {
222221 M1 = CloneModule(M1);
223222 M2 = CloneModule(M2);
224223 }
225 if (Linker::LinkModules(M1, M2, Linker::DestroySource, &ErrorMsg)) {
226 errs() << BD.getToolName() << ": Error linking modules together:"
227 << ErrorMsg << '\n';
224 if (Linker::LinkModules(M1, M2, Linker::DestroySource))
228225 exit(1);
229 }
230226 delete M2; // We are done with this module.
231227
232228 // Execute the program.
395391 F->getFunctionType()));
396392 }
397393
398 std::string ErrorMsg;
399 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted,
400 Linker::DestroySource, &ErrorMsg)){
401 errs() << BD.getToolName() << ": Error linking modules together:"
402 << ErrorMsg << '\n';
394 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted,
395 Linker::DestroySource))
403396 exit(1);
404 }
405397
406398 MiscompiledFunctions.clear();
407399 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
429421 // extraction both didn't break the program, and didn't mask the problem.
430422 // Replace the current program with the loop extracted version, and try to
431423 // extract another loop.
432 std::string ErrorMsg;
433 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted,
434 Linker::DestroySource, &ErrorMsg)){
435 errs() << BD.getToolName() << ": Error linking modules together:"
436 << ErrorMsg << '\n';
424 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted,
425 Linker::DestroySource))
437426 exit(1);
438 }
427
439428 delete ToOptimizeLoopExtracted;
440429
441430 // All of the Function*'s in the MiscompiledFunctions list are in the old
611600 MisCompFunctions.push_back(std::make_pair(I->getName(),
612601 I->getFunctionType()));
613602
614 std::string ErrorMsg;
615 if (Linker::LinkModules(ProgClone, Extracted.get(), Linker::DestroySource,
616 &ErrorMsg)) {
617 errs() << BD.getToolName() << ": Error linking modules together:"
618 << ErrorMsg << '\n';
603 if (Linker::LinkModules(ProgClone, Extracted.get(), Linker::DestroySource))
619604 exit(1);
620 }
621605
622606 // Set the new program and delete the old one.
623607 BD.setNewProgram(ProgClone);
776776 M->setTargetTriple(DefaultTriple);
777777 }
778778
779 std::string ErrMsg;
780 if (L.linkInModule(M.get(), &ErrMsg))
781 message(LDPL_FATAL, "Failed to link module: %s", ErrMsg.c_str());
779 if (L.linkInModule(M.get()))
780 message(LDPL_FATAL, "Failed to link module");
782781 }
783782
784783 for (const auto &Name : Internalize) {
1313
1414 #include "llvm/Linker/Linker.h"
1515 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/IR/DiagnosticPrinter.h"
1618 #include "llvm/IR/LLVMContext.h"
1719 #include "llvm/IR/Module.h"
1820 #include "llvm/IR/Verifier.h"
6870 return Result;
6971 }
7072
73 static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
74 unsigned Severity = DI.getSeverity();
75 switch (Severity) {
76 case DS_Error:
77 errs() << "ERROR: ";
78 case DS_Warning:
79 if (SuppressWarnings)
80 return;
81 errs() << "WARNING: ";
82 break;
83 case DS_Remark:
84 case DS_Note:
85 llvm_unreachable("Only expecting warnings and errors");
86 }
87
88 DiagnosticPrinterRawOStream DP(errs());
89 DI.print(DP);
90 }
91
7192 int main(int argc, char **argv) {
7293 // Print a stack trace if we signal out.
7394 sys::PrintStackTraceOnErrorSignal();
7899 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
79100
80101 auto Composite = make_unique("llvm-link", Context);
81 Linker L(Composite.get(), SuppressWarnings);
102 Linker L(Composite.get());
82103
83 std::string ErrorMessage;
104 Context.setDiagnosticHandler(diagnosticHandler);
84105 for (unsigned i = 0; i < InputFilenames.size(); ++i) {
85106 std::unique_ptr M = loadFile(argv[0], InputFilenames[i], Context);
86107 if (!M.get()) {
90111
91112 if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
92113
93 if (L.linkInModule(M.get(), &ErrorMessage)) {
94 errs() << argv[0] << ": link error in '" << InputFilenames[i]
95 << "': " << ErrorMessage << "\n";
114 if (L.linkInModule(M.get()))
96115 return 1;
97 }
98116 }
99117
100118 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
142142 return 1;
143143 }
144144
145
146 if (!CodeGen.addModule(Module.get(), error)) {
147 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
148 << "': " << error << "\n";
149 return 1;
150 }
145 if (!CodeGen.addModule(Module.get()))
146 return 1;
151147
152148 unsigned NumSyms = Module->getSymbolCount();
153149 for (unsigned I = 0; I < NumSyms; ++I) {
204204 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
205205
206206 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
207 return !unwrap(cg)->addModule(unwrap(mod), sLastErrorString);
207 return !unwrap(cg)->addModule(unwrap(mod));
208208 }
209209
210210 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
8787 Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
8888
8989 Module *LinkedModule = new Module("MyModuleLinked", Ctx);
90 Linker::LinkModules(LinkedModule, M.get(), Linker::PreserveSource, nullptr);
90 Linker::LinkModules(LinkedModule, M.get(), Linker::PreserveSource);
9191
9292 // Delete the original module.
9393 M.reset();
142142 GV->setInitializer(ConstantStruct::get(STy, F));
143143
144144 Module *EmptyM = new Module("EmptyModule1", Ctx);
145 Linker::LinkModules(EmptyM, InternalM, Linker::PreserveSource, nullptr);
145 Linker::LinkModules(EmptyM, InternalM, Linker::PreserveSource);
146146
147147 delete EmptyM;
148148 EmptyM = new Module("EmptyModule2", Ctx);
149 Linker::LinkModules(InternalM, EmptyM, Linker::PreserveSource, nullptr);
149 Linker::LinkModules(InternalM, EmptyM, Linker::PreserveSource);
150150
151151 delete EmptyM;
152152 delete InternalM;