[llvm] r181868 - Use only explicit bool conversion operators

Jeffrey Yasskin jyasskin at googlers.com
Wed May 15 09:51:05 PDT 2013


Drive-by thought about the macro: "explicit" itself isn't a C++11
feature, so it's not obvious that LLVM_EXPLICIT would expand to
nothing in C++98. If I were designing the macro, I'd use
LLVM_EXPLICIT_OPERATOR and expand it to "operator" in C++98.

On Wed, May 15, 2013 at 12:37 AM, David Blaikie <dblaikie at gmail.com> wrote:
> Author: dblaikie
> Date: Wed May 15 02:36:59 2013
> New Revision: 181868
>
> URL: http://llvm.org/viewvc/llvm-project?rev=181868&view=rev
> Log:
> Use only explicit bool conversion operators
>
> 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.
>
> Modified:
>     llvm/trunk/include/llvm/ADT/IntervalMap.h
>     llvm/trunk/include/llvm/ADT/OwningPtr.h
>     llvm/trunk/include/llvm/ADT/PointerUnion.h
>     llvm/trunk/include/llvm/Analysis/InlineCost.h
>     llvm/trunk/include/llvm/CodeGen/SlotIndexes.h
>     llvm/trunk/include/llvm/Support/CallSite.h
>     llvm/trunk/include/llvm/Support/YAMLParser.h
>     llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
>     llvm/trunk/lib/CodeGen/RegAllocGreedy.cpp
>     llvm/trunk/lib/Support/SourceMgr.cpp
>     llvm/trunk/lib/Support/Windows/Windows.h
>     llvm/trunk/tools/bugpoint/Miscompilation.cpp
>     llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp
>
> Modified: llvm/trunk/include/llvm/ADT/IntervalMap.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/IntervalMap.h?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/IntervalMap.h (original)
> +++ llvm/trunk/include/llvm/ADT/IntervalMap.h Wed May 15 02:36:59 2013
> @@ -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>
>
> Modified: llvm/trunk/include/llvm/ADT/OwningPtr.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/OwningPtr.h?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/OwningPtr.h (original)
> +++ llvm/trunk/include/llvm/ADT/OwningPtr.h Wed May 15 02:36:59 2013
> @@ -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) {
>
> Modified: llvm/trunk/include/llvm/ADT/PointerUnion.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/PointerUnion.h?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/PointerUnion.h (original)
> +++ llvm/trunk/include/llvm/ADT/PointerUnion.h Wed May 15 02:36:59 2013
> @@ -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>
>
> Modified: llvm/trunk/include/llvm/Analysis/InlineCost.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/InlineCost.h?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Analysis/InlineCost.h (original)
> +++ llvm/trunk/include/llvm/Analysis/InlineCost.h Wed May 15 02:36:59 2013
> @@ -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;
>    }
>
>
> Modified: llvm/trunk/include/llvm/CodeGen/SlotIndexes.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/SlotIndexes.h?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/CodeGen/SlotIndexes.h (original)
> +++ llvm/trunk/include/llvm/CodeGen/SlotIndexes.h Wed May 15 02:36:59 2013
> @@ -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;
>
> Modified: llvm/trunk/include/llvm/Support/CallSite.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/CallSite.h?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Support/CallSite.h (original)
> +++ llvm/trunk/include/llvm/Support/CallSite.h Wed May 15 02:36:59 2013
> @@ -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.
>    ///
>
> Modified: llvm/trunk/include/llvm/Support/YAMLParser.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLParser.h?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Support/YAMLParser.h (original)
> +++ llvm/trunk/include/llvm/Support/YAMLParser.h Wed May 15 02:36:59 2013
> @@ -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;
>
> Modified: llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp (original)
> +++ llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp Wed May 15 02:36:59 2013
> @@ -89,7 +89,7 @@ bool MemoryDependenceAnalysis::runOnFunc
>    AA = &getAnalysis<AliasAnalysis>();
>    TD = getAnalysisIfAvailable<DataLayout>();
>    DT = getAnalysisIfAvailable<DominatorTree>();
> -  if (PredCache == 0)
> +  if (!PredCache)
>      PredCache.reset(new PredIteratorCache());
>    return false;
>  }
>
> Modified: llvm/trunk/lib/CodeGen/RegAllocGreedy.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/RegAllocGreedy.cpp?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/RegAllocGreedy.cpp (original)
> +++ llvm/trunk/lib/CodeGen/RegAllocGreedy.cpp Wed May 15 02:36:59 2013
> @@ -713,7 +713,7 @@ bool RAGreedy::addSplitConstraints(Inter
>      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;
>
> Modified: llvm/trunk/lib/Support/SourceMgr.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SourceMgr.cpp?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Support/SourceMgr.cpp (original)
> +++ llvm/trunk/lib/Support/SourceMgr.cpp Wed May 15 02:36:59 2013
> @@ -65,7 +65,7 @@ unsigned SourceMgr::AddIncludeFile(const
>      MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
>    }
>
> -  if (NewBuf == 0) return ~0U;
> +  if (!NewBuf) return ~0U;
>
>    return AddNewSourceBuffer(NewBuf.take(), IncludeLoc);
>  }
>
> Modified: llvm/trunk/lib/Support/Windows/Windows.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Windows.h?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Support/Windows/Windows.h (original)
> +++ llvm/trunk/lib/Support/Windows/Windows.h Wed May 15 02:36:59 2013
> @@ -75,7 +75,7 @@ public:
>    }
>
>    // True if Handle is valid.
> -  operator bool() const {
> +  LLVM_EXPLICIT operator bool() const {
>      return HandleTraits::IsValid(Handle) ? true : false;
>    }
>
>
> Modified: llvm/trunk/tools/bugpoint/Miscompilation.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/Miscompilation.cpp?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/tools/bugpoint/Miscompilation.cpp (original)
> +++ llvm/trunk/tools/bugpoint/Miscompilation.cpp Wed May 15 02:36:59 2013
> @@ -130,7 +130,7 @@ ReduceMiscompilingPasses::doTest(std::ve
>    //
>    OwningPtr<Module> PrefixOutput(ParseInputFile(BitcodeResult,
>                                                  BD.getContext()));
> -  if (PrefixOutput == 0) {
> +  if (!PrefixOutput) {
>      errs() << BD.getToolName() << ": Error reading bitcode file '"
>             << BitcodeResult << "'!\n";
>      exit(1);
>
> Modified: llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp?rev=181868&r1=181867&r2=181868&view=diff
> ==============================================================================
> --- llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp (original)
> +++ llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp Wed May 15 02:36:59 2013
> @@ -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();
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits



More information about the llvm-commits mailing list