[llvm] r215850 - Return a std::unique_ptr to make the ownership explicit.

David Blaikie dblaikie at gmail.com
Sun Aug 17 16:30:08 PDT 2014


On Sun, Aug 17, 2014 at 2:11 PM, Rafael Espindola
<rafael.espindola at gmail.com> wrote:
> Author: rafael
> Date: Sun Aug 17 16:11:31 2014
> New Revision: 215850
>
> URL: http://llvm.org/viewvc/llvm-project?rev=215850&view=rev
> Log:
> Return a std::unique_ptr to make the ownership explicit.

This'll eventually be something for a clang-tidy warning & we can
cleanup much more nicely, but something I've been trying to do by hand
on an ad-hoc basis:

When making a function return unique_ptr when it used to return raw
pointer, I try to find call sites like this:

  unique_ptr<T> p(func());

and transform them to this:

  unique_ptr<T> p = func();

that way it's clear to the next reader that there's no tricksy raw
pointer ownership going on here.

(the general clang-tidy check of course is: prefer copy initialization
over direct initialization (and the justification: copy initialization
cannot perform explicit conversions/call explicit constructors, so
it's strictly less powerful & thus requires less consideration as a
reader/editor))

>
> Modified:
>     llvm/trunk/include/llvm/ExecutionEngine/ObjectBuffer.h
>
> Modified: llvm/trunk/include/llvm/ExecutionEngine/ObjectBuffer.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/ObjectBuffer.h?rev=215850&r1=215849&r2=215850&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ExecutionEngine/ObjectBuffer.h (original)
> +++ llvm/trunk/include/llvm/ExecutionEngine/ObjectBuffer.h Sun Aug 17 16:11:31 2014
> @@ -38,9 +38,9 @@ public:
>    /// Like MemoryBuffer::getMemBuffer() this function returns a pointer to an
>    /// object that is owned by the caller. However, the caller does not take
>    /// ownership of the underlying memory.
> -  MemoryBuffer *getMemBuffer() const {
> -    return MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
> -                                      Buffer->getBufferIdentifier(), false);
> +  std::unique_ptr<MemoryBuffer> getMemBuffer() const {
> +    return std::unique_ptr<MemoryBuffer>(MemoryBuffer::getMemBuffer(
> +        Buffer->getBuffer(), Buffer->getBufferIdentifier(), false));
>    }
>
>    const char *getBufferStart() const { return Buffer->getBufferStart(); }
>
>
> _______________________________________________
> 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