Compare DataLayout by Value, not by pointer.
authorRafael Espindola <rafael.espindola@gmail.com>
Wed, 26 Feb 2014 17:02:08 +0000 (17:02 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Wed, 26 Feb 2014 17:02:08 +0000 (17:02 +0000)
This fixes spurious warnings in llvm-link about the datalayout not matching.

Thanks to Zalman Stern for reporting the bug!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202276 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/DataLayout.h
lib/IR/DataLayout.cpp
lib/Linker/LinkModules.cpp
test/Linker/Inputs/datalayout-a.ll [new file with mode: 0644]
test/Linker/Inputs/datalayout-b.ll [new file with mode: 0644]
test/Linker/datalayout.ll [new file with mode: 0644]

index e26dd026bd4ebed555ef7733bd84fe2d4ee09ac9..cfed302e657dadedb2c95b8fc1b39a5c10e4fd92 100644 (file)
@@ -193,6 +193,9 @@ public:
     return *this;
   }
 
+  bool operator==(const DataLayout &Other) const;
+  bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
+
   ~DataLayout();  // Not virtual, do not subclass this class
 
   /// Parse a data layout string (with fallback to default values).
index 7089b7b85ffc8a52803e52b371e394ef8b1be8b2..162f3d3ac373de11a0866db60517a642d47b284a 100644 (file)
@@ -354,6 +354,16 @@ DataLayout::DataLayout(const Module *M) : LayoutMap(0) {
     reset("");
 }
 
+bool DataLayout::operator==(const DataLayout &Other) const {
+  bool Ret = LittleEndian == Other.LittleEndian &&
+             StackNaturalAlign == Other.StackNaturalAlign &&
+             ManglingMode == Other.ManglingMode &&
+             LegalIntWidths == Other.LegalIntWidths &&
+             Alignments == Other.Alignments && Pointers == Pointers;
+  assert(Ret == (getStringRepresentation() == Other.getStringRepresentation()));
+  return Ret;
+}
+
 void
 DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
                          unsigned pref_align, uint32_t bit_width) {
index f1b8cb761061a96fffa1cccc3ad4d6b649f7b69a..9160e2661b74b436891233343b033f91c7e1b455 100644 (file)
@@ -1208,7 +1208,7 @@ bool ModuleLinker::run() {
     DstM->setTargetTriple(SrcM->getTargetTriple());
 
   if (SrcM->getDataLayout() && DstM->getDataLayout() &&
-      SrcM->getDataLayout() != DstM->getDataLayout()) {
+      *SrcM->getDataLayout() != *DstM->getDataLayout()) {
     if (!SuppressWarnings) {
       errs() << "WARNING: Linking two modules of different data layouts!\n";
     }
diff --git a/test/Linker/Inputs/datalayout-a.ll b/test/Linker/Inputs/datalayout-a.ll
new file mode 100644 (file)
index 0000000..e78478e
--- /dev/null
@@ -0,0 +1 @@
+target datalayout = "e"
diff --git a/test/Linker/Inputs/datalayout-b.ll b/test/Linker/Inputs/datalayout-b.ll
new file mode 100644 (file)
index 0000000..59cdb68
--- /dev/null
@@ -0,0 +1 @@
+target datalayout = "E"
diff --git a/test/Linker/datalayout.ll b/test/Linker/datalayout.ll
new file mode 100644 (file)
index 0000000..3ee96eb
--- /dev/null
@@ -0,0 +1,14 @@
+; RUN: llvm-link %s %S/Inputs/datalayout-a.ll -S -o - 2>%t.a.err | FileCheck %s
+; RUN: cat %t.a.err | not FileCheck %s 2>&1 | FileCheck --check-prefix=WARN-A %s
+
+; RUN: llvm-link %s %S/Inputs/datalayout-b.ll -S -o - 2>%t.b.err | FileCheck %s
+; RUN: cat %t.b.err | FileCheck --check-prefix=WARN-B %s
+
+target datalayout = "e"
+
+; CHECK: target datalayout = "e"
+
+; this is a hack to check that llvm-link printed no warnings.
+; WARN-A: FileCheck error: '-' is empty.
+
+; WARN-B: WARNING: Linking two modules of different data layouts!