Use only explicit bool conversion operators
authorDavid Blaikie <dblaikie@gmail.com>
Wed, 15 May 2013 07:36:59 +0000 (07:36 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Wed, 15 May 2013 07:36:59 +0000 (07:36 +0000)
BitVector/SmallBitVector::reference::operator bool remain implicit since
they model more exactly a bool, rather than something else that can be
boolean tested.

The most common (non-buggy) case are where such objects are used as
return expressions in bool-returning functions or as boolean function
arguments. In those cases I've used (& added if necessary) a named
function to provide the equivalent (or sometimes negative, depending on
convenient wording) test.

One behavior change (YAMLParser) was made, though no test case is
included as I'm not sure how to reach that code path. Essentially any
comparison of llvm::yaml::document_iterators would be invalid if neither
iterator was at the end.

This helped uncover a couple of bugs in Clang - test cases provided for
those in a separate commit along with similar changes to `operator bool`
instances in Clang.

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

13 files changed:
include/llvm/ADT/IntervalMap.h
include/llvm/ADT/OwningPtr.h
include/llvm/ADT/PointerUnion.h
include/llvm/Analysis/InlineCost.h
include/llvm/CodeGen/SlotIndexes.h
include/llvm/Support/CallSite.h
include/llvm/Support/YAMLParser.h
lib/Analysis/MemoryDependenceAnalysis.cpp
lib/CodeGen/RegAllocGreedy.cpp
lib/Support/SourceMgr.cpp
lib/Support/Windows/Windows.h
tools/bugpoint/Miscompilation.cpp
unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp

index c4083eed6a993c0d292d25c0fd3e3c3f20605d97..44a61fff85179f09d356e99ff07d1317d5b06951 100644 (file)
@@ -496,7 +496,7 @@ public:
   NodeRef() {}
 
   /// operator bool - Detect a null ref.
-  operator bool() const { return pip.getOpaqueValue(); }
+  LLVM_EXPLICIT operator bool() const { return pip.getOpaqueValue(); }
 
   /// NodeRef - Create a reference to the node p with n elements.
   template <typename NodeT>
index 86f9feee2cb4ce244fdcb552323749004f5e6818..6b9e42eaec0f152e3b59a5a698020bf6f7e45772 100644 (file)
@@ -70,8 +70,9 @@ public:
 
   T *operator->() const { return Ptr; }
   T *get() const { return Ptr; }
-  operator bool() const { return Ptr != 0; }
+  LLVM_EXPLICIT operator bool() const { return Ptr != 0; }
   bool operator!() const { return Ptr == 0; }
+  bool isValid() const { return Ptr != 0; }
 
   void swap(OwningPtr &RHS) {
     T *Tmp = RHS.Ptr;
@@ -132,7 +133,7 @@ public:
   }
 
   T *get() const { return Ptr; }
-  operator bool() const { return Ptr != 0; }
+  LLVM_EXPLICIT operator bool() const { return Ptr != 0; }
   bool operator!() const { return Ptr == 0; }
 
   void swap(OwningArrayPtr &RHS) {
index f42515ac77a74be5ab18cd8dad3ae1d08e68e752..b63ee52cdddace12562164374b39a7fac9feb96d 100644 (file)
@@ -109,7 +109,7 @@ namespace llvm {
       // we recursively strip off low bits if we have a nested PointerUnion.
       return !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());
     }
-    operator bool() const { return !isNull(); }
+    LLVM_EXPLICIT operator bool() const { return !isNull(); }
 
     /// is<T>() return true if the Union currently holds the type matching T.
     template<typename T>
@@ -174,6 +174,11 @@ namespace llvm {
       return V;
     }
   };
+
+  template<typename PT1, typename PT2>
+  bool operator==(PointerUnion<PT1, PT2> lhs, PointerUnion<PT1, PT2> rhs) {
+    return lhs.getOpaqueValue() == rhs.getOpaqueValue();
+  }
   
   // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
   // # low bits available = min(PT1bits,PT2bits)-1.
@@ -251,7 +256,7 @@ namespace llvm {
     /// isNull - Return true if the pointer held in the union is null,
     /// regardless of which type it is.
     bool isNull() const { return Val.isNull(); }
-    operator bool() const { return !isNull(); }
+    LLVM_EXPLICIT operator bool() const { return !isNull(); }
     
     /// is<T>() return true if the Union currently holds the type matching T.
     template<typename T>
@@ -359,7 +364,7 @@ namespace llvm {
     /// isNull - Return true if the pointer held in the union is null,
     /// regardless of which type it is.
     bool isNull() const { return Val.isNull(); }
-    operator bool() const { return !isNull(); }
+    LLVM_EXPLICIT operator bool() const { return !isNull(); }
     
     /// is<T>() return true if the Union currently holds the type matching T.
     template<typename T>
index bc7924e10fdcb6a38b519a8431379d0bf48cbb0e..28baa9eb94c0d69b5f75aa6ab540237c9f8a87fb 100644 (file)
@@ -77,7 +77,7 @@ public:
   }
 
   /// \brief Test whether the inline cost is low enough for inlining.
-  operator bool() const {
+  LLVM_EXPLICIT operator bool() const {
     return Cost < Threshold;
   }
 
index 26d0433f3e87ccc9dca5b7975baa3e750498d3a4..676cdaf7fbf1e61d973fa8625200dc58eb34e20e 100644 (file)
@@ -162,7 +162,7 @@ namespace llvm {
     }
 
     /// Return true for a valid index.
-    operator bool() const { return isValid(); }
+    LLVM_EXPLICIT operator bool() const { return isValid(); }
 
     /// Print this index to the given raw_ostream.
     void print(raw_ostream &os) const;
index 92107ac025263eded1e8da02617c3f328b2d85a4..d80d9d8ad15a067b24ab9847fed7449d6214f7d8 100644 (file)
@@ -78,7 +78,7 @@ public:
 
   InstrTy *getInstruction() const { return I.getPointer(); }
   InstrTy *operator->() const { return I.getPointer(); }
-  operator bool() const { return I.getPointer(); }
+  LLVM_EXPLICIT operator bool() const { return I.getPointer(); }
 
   /// getCalledValue - Return the pointer to function that is being called.
   ///
index 6e4f57f6ab4a89bd3b1e625feebbfbabead5d5ab..338bb4b6f2b60eaae57da5aebc86c0aa4f4599c8 100644 (file)
@@ -516,7 +516,7 @@ public:
     if (isAtEnd() || Other.isAtEnd())
       return isAtEnd() && Other.isAtEnd();
 
-    return *Doc == *Other.Doc;
+    return Doc == Other.Doc;
   }
   bool operator !=(const document_iterator &Other) {
     return !(*this == Other);
@@ -543,7 +543,7 @@ public:
 
 private:
   bool isAtEnd() const {
-    return Doc == 0 || *Doc == 0;
+    return !Doc || !*Doc;
   }
 
   OwningPtr<Document> *Doc;
index c0009cb9899f7f50ab737de16b374ffb6cb3f37e..674ce3aea71fab9bf527a0d296e1794341740ba1 100644 (file)
@@ -89,7 +89,7 @@ bool MemoryDependenceAnalysis::runOnFunction(Function &) {
   AA = &getAnalysis<AliasAnalysis>();
   TD = getAnalysisIfAvailable<DataLayout>();
   DT = getAnalysisIfAvailable<DominatorTree>();
-  if (PredCache == 0)
+  if (!PredCache)
     PredCache.reset(new PredIteratorCache());
   return false;
 }
index 9eed1fc62accc2381c7c41b4ff6150c9bc287690..49748289dacdf5d8b20afb2abe94afbce01a13b8 100644 (file)
@@ -713,7 +713,7 @@ bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf,
     Intf.moveToBlock(BC.Number);
     BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
     BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
-    BC.ChangesValue = BI.FirstDef;
+    BC.ChangesValue = BI.FirstDef.isValid();
 
     if (!Intf.hasInterference())
       continue;
index fac3cad5cc2569ec221e2ed0e38754285303c33d..4f650b42cce77c1448aff344134d88d3ceb52399 100644 (file)
@@ -65,7 +65,7 @@ unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
     MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
   }
 
-  if (NewBuf == 0) return ~0U;
+  if (!NewBuf) return ~0U;
 
   return AddNewSourceBuffer(NewBuf.take(), IncludeLoc);
 }
index 5c1da0d617aa776132943a8042571c346eb8569e..2ddd15efa19de1c63f87a6da7a8415b73562d4e6 100644 (file)
@@ -75,7 +75,7 @@ public:
   }
 
   // True if Handle is valid.
-  operator bool() const {
+  LLVM_EXPLICIT operator bool() const {
     return HandleTraits::IsValid(Handle) ? true : false;
   }
 
index c676a05cb6ceb9ca4070768be01d6e5614131ff4..11ac0de104e8b7a1631b49be529fc78a205ae121 100644 (file)
@@ -130,7 +130,7 @@ ReduceMiscompilingPasses::doTest(std::vector<std::string> &Prefix,
   //
   OwningPtr<Module> PrefixOutput(ParseInputFile(BitcodeResult,
                                                 BD.getContext()));
-  if (PrefixOutput == 0) {
+  if (!PrefixOutput) {
     errs() << BD.getToolName() << ": Error reading bitcode file '"
            << BitcodeResult << "'!\n";
     exit(1);
index 0061e30e7a541cfa0cc5c58f37020fd93a3a4d55..abe8be450ae91e0d1a01aa7039d7c2bf13d8577c 100644 (file)
@@ -98,7 +98,7 @@ protected:
 
   void compileAndRun(int ExpectedRC = OriginalRC) {
     // This function shouldn't be called until after SetUp.
-    ASSERT_TRUE(0 != TheJIT);
+    ASSERT_TRUE(TheJIT.isValid());
     ASSERT_TRUE(0 != Main);
 
     TheJIT->finalizeObject();