SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
cl::init(false));
+static cl::opt<bool>
+ PreserveModules("preserve-modules",
+ cl::desc("Preserve linked modules for testing"));
+
static cl::opt<bool> PreserveBitcodeUseListOrder(
"preserve-bc-uselistorder",
cl::desc("Preserve use-list order when writing LLVM bitcode."),
errs() << '\n';
}
-/// Load a function index if requested by the -functionindex option.
-static ErrorOr<std::unique_ptr<FunctionInfoIndex>>
-loadIndex(LLVMContext &Context, const Module *ExportingModule = nullptr) {
- assert(!FunctionIndex.empty());
- ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
- MemoryBuffer::getFileOrSTDIN(FunctionIndex);
- std::error_code EC = FileOrErr.getError();
- if (EC)
- return EC;
- MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
- ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
- object::FunctionIndexObjectFile::create(BufferRef, Context,
- ExportingModule);
- EC = ObjOrErr.getError();
- if (EC)
- return EC;
-
- object::FunctionIndexObjectFile &Obj = **ObjOrErr;
- return Obj.takeIndex();
+static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, void *C) {
+ diagnosticHandler(DI);
}
/// Import any functions requested via the -import option.
std::unique_ptr<FunctionInfoIndex> Index;
if (!FunctionIndex.empty()) {
ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr =
- loadIndex(Context);
+ llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler);
std::error_code EC = IndexOrErr.getError();
if (EC) {
errs() << EC.message() << '\n';
}
// Link in the specified function.
- if (L.linkInModule(M.get(), false, Index.get(), F))
+ DenseSet<const GlobalValue *> FunctionsToImport;
+ FunctionsToImport.insert(F);
+ if (L.linkInModule(std::move(M), Linker::Flags::None, Index.get(),
+ &FunctionsToImport))
return false;
}
return true;
std::unique_ptr<FunctionInfoIndex> Index;
if (!FunctionIndex.empty()) {
ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr =
- loadIndex(Context, &*M);
+ llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler);
std::error_code EC = IndexOrErr.getError();
if (EC) {
errs() << EC.message() << '\n';
if (Verbose)
errs() << "Linking in '" << File << "'\n";
- if (L.linkInModule(M.get(), ApplicableFlags, Index.get()))
+ if (L.linkInModule(std::move(M), ApplicableFlags, Index.get()))
return false;
// All linker flags apply to linking of subsequent files.
ApplicableFlags = Flags;
+
+ // If requested for testing, preserve modules by releasing them from
+ // the unique_ptr before the are freed. This can help catch any
+ // cross-module references from e.g. unneeded metadata references
+ // that aren't properly set to null but instead mapped to the source
+ // module version. The bitcode writer will assert if it finds any such
+ // cross-module references.
+ if (PreserveModules)
+ M.release();
}
return true;
PrettyStackTraceProgram X(argc, argv);
LLVMContext &Context = getGlobalContext();
+ Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
+
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
auto Composite = make_unique<Module>("llvm-link", Context);
- Linker L(Composite.get(), diagnosticHandler);
+ Linker L(*Composite);
unsigned Flags = Linker::Flags::None;
if (Internalize)