llvm-link option and test for recent metadata mapping bug
[oota-llvm.git] / tools / llvm-link / llvm-link.cpp
index c539f75702bed1fae1a0f91e9de81682fa7db397..ada55c93a6e2d22025c4399ef6a70969e12144ea 100644 (file)
@@ -89,6 +89,10 @@ static cl::opt<bool>
 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."),
@@ -259,6 +263,15 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
       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;