summaryrefslogtreecommitdiff
path: root/unittests/CodeGen/MachineInstrBundleIteratorTest.cpp
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2016-08-12 05:05:36 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2016-08-12 05:05:36 +0000
commit21bcd75d9bb09783cb184a33c495e7d15b3429e0 (patch)
tree7bcf8cd34b36201fe476e1e1ac38857750b88d12 /unittests/CodeGen/MachineInstrBundleIteratorTest.cpp
parent7e45eb5f3f2a39a04386088b610423a5563dd6dd (diff)
ADT: Remove all ilist_iterator => pointer casts, NFC
Remove all ilist_iterator to pointer casts. There were two reasons for casts: - Checking for an uninitialized (i.e., null) iterator. I added MachineInstrBundleIterator::isValid() to check for that case. - Comparing an iterator against the underlying pointer value while avoiding converting the pointer value to an iterator. This is occasionally necessary in MachineInstrBundleIterator, since there is an assertion in the constructors that the underlying MachineInstr is not bundled (but we don't care about that if we're just checking for pointer equality). To support the latter case, I rewrote the == and != operators for ilist_iterator and MachineInstrBundleIterator. - The implicit constructors now use enable_if to exclude const-iterator => non-const-iterator conversions from overload resolution (previously it was a compiler error on instantiation, now it's SFINAE). - The == and != operators are now global (friends), and are not templated. - MachineInstrBundleIterator has overloads to compare against both const_pointer and const_reference. This avoids the implicit conversions to MachineInstrBundleIterator that assert, instead just checking the address (and I added unit tests to confirm this). Notably, the only remaining uses of ilist_iterator::getNodePtrUnchecked are in ilist.h, and no code outside of ilist*.h directly relies on this UB end-iterator-to-pointer conversion anymore. It's still needed for ilist_*sentinel_traits, but I'll clean that up soon. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278478 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/CodeGen/MachineInstrBundleIteratorTest.cpp')
-rw-r--r--unittests/CodeGen/MachineInstrBundleIteratorTest.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/unittests/CodeGen/MachineInstrBundleIteratorTest.cpp b/unittests/CodeGen/MachineInstrBundleIteratorTest.cpp
new file mode 100644
index 00000000000..857994a3b29
--- /dev/null
+++ b/unittests/CodeGen/MachineInstrBundleIteratorTest.cpp
@@ -0,0 +1,94 @@
+//===- MachineInstrBundleIteratorTest.cpp ---------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/ilist_node.h"
+#include "llvm/CodeGen/MachineInstrBundleIterator.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+struct MyBundledInstr : public ilist_node<MyBundledInstr> {
+ bool isBundledWithPred() const { return true; }
+ bool isBundledWithSucc() const { return true; }
+};
+typedef MachineInstrBundleIterator<MyBundledInstr> bundled_iterator;
+typedef MachineInstrBundleIterator<const MyBundledInstr> const_bundled_iterator;
+
+#ifdef GTEST_HAS_DEATH_TEST
+#ifndef NDEBUG
+TEST(MachineInstrBundleIteratorTest, CheckForBundles) {
+ MyBundledInstr MBI;
+
+ // Confirm that MBI is always considered bundled.
+ EXPECT_TRUE(MBI.isBundledWithPred());
+ EXPECT_TRUE(MBI.isBundledWithSucc());
+
+ // Confirm that iterators check in their constructor for bundled iterators.
+ EXPECT_DEATH((void)static_cast<bundled_iterator>(MBI),
+ "not legal to initialize");
+ EXPECT_DEATH((void)static_cast<bundled_iterator>(&MBI),
+ "not legal to initialize");
+ EXPECT_DEATH((void)static_cast<const_bundled_iterator>(MBI),
+ "not legal to initialize");
+ EXPECT_DEATH((void)static_cast<const_bundled_iterator>(&MBI),
+ "not legal to initialize");
+}
+#endif
+#endif
+
+TEST(MachineInstrBundleIteratorTest, CompareToBundledMI) {
+ MyBundledInstr MBI;
+ const MyBundledInstr &CMBI = MBI;
+ bundled_iterator I;
+ const_bundled_iterator CI;
+
+ // Confirm that MBI is always considered bundled.
+ EXPECT_TRUE(MBI.isBundledWithPred());
+ EXPECT_TRUE(MBI.isBundledWithSucc());
+
+ // These invocations will crash when !NDEBUG if a conversion is taking place.
+ // These checks confirm that comparison operators don't use any conversion
+ // operators.
+ ASSERT_FALSE(MBI == I);
+ ASSERT_FALSE(&MBI == I);
+ ASSERT_FALSE(CMBI == I);
+ ASSERT_FALSE(&CMBI == I);
+ ASSERT_FALSE(I == MBI);
+ ASSERT_FALSE(I == &MBI);
+ ASSERT_FALSE(I == CMBI);
+ ASSERT_FALSE(I == &CMBI);
+ ASSERT_FALSE(MBI == CI);
+ ASSERT_FALSE(&MBI == CI);
+ ASSERT_FALSE(CMBI == CI);
+ ASSERT_FALSE(&CMBI == CI);
+ ASSERT_FALSE(CI == MBI);
+ ASSERT_FALSE(CI == &MBI);
+ ASSERT_FALSE(CI == CMBI);
+ ASSERT_FALSE(CI == &CMBI);
+ ASSERT_TRUE(MBI != I);
+ ASSERT_TRUE(&MBI != I);
+ ASSERT_TRUE(CMBI != I);
+ ASSERT_TRUE(&CMBI != I);
+ ASSERT_TRUE(I != MBI);
+ ASSERT_TRUE(I != &MBI);
+ ASSERT_TRUE(I != CMBI);
+ ASSERT_TRUE(I != &CMBI);
+ ASSERT_TRUE(MBI != CI);
+ ASSERT_TRUE(&MBI != CI);
+ ASSERT_TRUE(CMBI != CI);
+ ASSERT_TRUE(&CMBI != CI);
+ ASSERT_TRUE(CI != MBI);
+ ASSERT_TRUE(CI != &MBI);
+ ASSERT_TRUE(CI != CMBI);
+ ASSERT_TRUE(CI != &CMBI);
+}
+
+} // end namespace