From 5cb9c82a5da41a093679af1c18f60cb1be27ebf0 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 17 Nov 2014 20:51:01 +0000 Subject: [PATCH] Factor common code it Linker::init. The TypeFinder was not being used in one of the constructors. llvm-svn: 222172 --- llvm/include/llvm/Linker/Linker.h | 1 + llvm/lib/Linker/LinkModules.cpp | 19 +++++++++++++------ llvm/unittests/Linker/CMakeLists.txt | 1 + llvm/unittests/Linker/LinkModulesTest.cpp | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/Linker/Linker.h b/llvm/include/llvm/Linker/Linker.h index d1c02c241d53..c957cc2cd135 100644 --- a/llvm/include/llvm/Linker/Linker.h +++ b/llvm/include/llvm/Linker/Linker.h @@ -45,6 +45,7 @@ class Linker { static bool LinkModules(Module *Dest, Module *Src); private: + void init(Module *M, DiagnosticHandlerFunction DiagnosticHandler); Module *Composite; SmallPtrSet IdentifiedStructTypes; DiagnosticHandlerFunction DiagnosticHandler; diff --git a/llvm/lib/Linker/LinkModules.cpp b/llvm/lib/Linker/LinkModules.cpp index bac187bb3a39..0a9bbf2a7fc1 100644 --- a/llvm/lib/Linker/LinkModules.cpp +++ b/llvm/lib/Linker/LinkModules.cpp @@ -1594,18 +1594,25 @@ bool ModuleLinker::run() { return false; } -Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) - : Composite(M), DiagnosticHandler(DiagnosticHandler) {} +void Linker::init(Module *M, DiagnosticHandlerFunction DiagnosticHandler) { + this->Composite = M; + this->DiagnosticHandler = DiagnosticHandler; -Linker::Linker(Module *M) - : Composite(M), DiagnosticHandler([this](const DiagnosticInfo &DI) { - Composite->getContext().diagnose(DI); - }) { TypeFinder StructTypes; StructTypes.run(*M, true); IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end()); } +Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) { + init(M, DiagnosticHandler); +} + +Linker::Linker(Module *M) { + init(M, [this](const DiagnosticInfo &DI) { + Composite->getContext().diagnose(DI); + }); +} + Linker::~Linker() { } diff --git a/llvm/unittests/Linker/CMakeLists.txt b/llvm/unittests/Linker/CMakeLists.txt index c3dccb6c7bb8..05f45c0a8ce8 100644 --- a/llvm/unittests/Linker/CMakeLists.txt +++ b/llvm/unittests/Linker/CMakeLists.txt @@ -1,4 +1,5 @@ set(LLVM_LINK_COMPONENTS + AsmParser core linker ) diff --git a/llvm/unittests/Linker/LinkModulesTest.cpp b/llvm/unittests/Linker/LinkModulesTest.cpp index a21ee472f347..b15d1804e8b4 100644 --- a/llvm/unittests/Linker/LinkModulesTest.cpp +++ b/llvm/unittests/Linker/LinkModulesTest.cpp @@ -7,12 +7,14 @@ // //===----------------------------------------------------------------------===// +#include "llvm/AsmParser/Parser.h" #include "llvm/Linker/Linker.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Function.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Module.h" +#include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" using namespace llvm; @@ -157,4 +159,22 @@ TEST_F(LinkModuleTest, EmptyModule2) { Linker::LinkModules(InternalM.get(), EmptyM.get()); } +TEST_F(LinkModuleTest, TypeMerge) { + LLVMContext C; + SMDiagnostic Err; + + const char *M1Str = "%t = type {i32}\n" + "@t1 = weak global %t zeroinitializer\n"; + std::unique_ptr M1 = parseAssemblyString(M1Str, Err, C); + + const char *M2Str = "%t = type {i32}\n" + "@t2 = weak global %t zeroinitializer\n"; + std::unique_ptr M2 = parseAssemblyString(M2Str, Err, C); + + Linker::LinkModules(M1.get(), M2.get(), [](const llvm::DiagnosticInfo &){}); + + EXPECT_EQ(M1->getNamedGlobal("t1")->getType(), + M1->getNamedGlobal("t2")->getType()); +} + } // end anonymous namespace