[lld] r205332 - s/dyn_cast/cast/ where return value should never be null.

Rui Ueyama ruiu at google.com
Tue Apr 1 11:35:06 PDT 2014


On Tue, Apr 1, 2014 at 11:32 AM, Shankar Easwaran
<shankare at codeaurora.org>wrote:

> On 4/1/2014 1:28 PM, David Blaikie wrote:
>
>> On Tue, Apr 1, 2014 at 11:20 AM, Shankar Easwaran
>> <shankare at codeaurora.org> wrote:
>>
>>> We should assert if dyn_cast fails, which would catch issues in the
>>> Driver.
>>>
>> That's what cast<T> is for - it asserts on failure.
>>
>> dyn_cast cannot assert on failure - it's used for doing type tests "if
>> x = dyn_cast<T1>(a) { do stuff } else if (x = dyn_cast<T2>(b) { ...
>> }", etc.
>>
> Thanks David. I like cast now :) Is this preferred over dyn_cast then ?


cast<T> is a preferred way than dyn_cast<T> for casting if you don't expect
it fails.


>>> On 4/1/2014 1:04 PM, Rui Ueyama wrote:
>>>
>>>> Author: ruiu
>>>> Date: Tue Apr  1 13:04:56 2014
>>>> New Revision: 205332
>>>>
>>>> URL: http://llvm.org/viewvc/llvm-project?rev=205332&view=rev
>>>> Log:
>>>> s/dyn_cast/cast/ where return value should never be null.
>>>>
>>>> cast<X> asserts the type is correct and does not return null on failure.
>>>> So we should use cast<X> rather than dyn_cast<X> at such places where we
>>>> don't expect type conversion could fail.
>>>>
>>>> Modified:
>>>>       lld/trunk/lib/Driver/GnuLdDriver.cpp
>>>>       lld/trunk/lib/Driver/WinLinkDriver.cpp
>>>>       lld/trunk/unittests/DriverTests/DriverTest.h
>>>>
>>>> Modified: lld/trunk/lib/Driver/GnuLdDriver.cpp
>>>> URL:
>>>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/
>>>> GnuLdDriver.cpp?rev=205332&r1=205331&r2=205332&view=diff
>>>>
>>>> ============================================================
>>>> ==================
>>>> --- lld/trunk/lib/Driver/GnuLdDriver.cpp (original)
>>>> +++ lld/trunk/lib/Driver/GnuLdDriver.cpp Tue Apr  1 13:04:56 2014
>>>> @@ -432,13 +432,13 @@ bool GnuLdDriver::parse(int argc, const
>>>>        case OPT_start_group: {
>>>>          std::unique_ptr<InputElement> controlStart(new ELFGroup(*ctx,
>>>> index++));
>>>>          controlNodeStack.push(controlStart.get());
>>>> -
>>>> dyn_cast<ControlNode>(controlNodeStack.top())->processControlEnter();
>>>> +      cast<ControlNode>(controlNodeStack.top())->
>>>> processControlEnter();
>>>>          inputGraph->addInputElement(std::move(controlStart));
>>>>          break;
>>>>        }
>>>>          case OPT_end_group:
>>>> -
>>>> dyn_cast<ControlNode>(controlNodeStack.top())->processControlExit();
>>>> +      cast<ControlNode>(controlNodeStack.top())->processControlExit();
>>>>          controlNodeStack.pop();
>>>>          break;
>>>>    @@ -485,11 +485,12 @@ bool GnuLdDriver::parse(int argc, const
>>>>            }
>>>>          }
>>>>          std::unique_ptr<InputElement> inputFile(inputNode);
>>>> -      if (controlNodeStack.empty())
>>>> +      if (controlNodeStack.empty()) {
>>>>            inputGraph->addInputElement(std::move(inputFile));
>>>> -      else
>>>> -        dyn_cast<ControlNode>(controlNodeStack.top())
>>>> +      } else {
>>>> +        cast<ControlNode>(controlNodeStack.top())
>>>>                ->processInputElement(std::move(inputFile));
>>>> +      }
>>>>          break;
>>>>        }
>>>>
>>>> Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
>>>> URL:
>>>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/
>>>> WinLinkDriver.cpp?rev=205332&r1=205331&r2=205332&view=diff
>>>>
>>>> ============================================================
>>>> ==================
>>>> --- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
>>>> +++ lld/trunk/lib/Driver/WinLinkDriver.cpp Tue Apr  1 13:04:56 2014
>>>> @@ -1222,7 +1222,7 @@ bool WinLinkDriver::parse(int argc, cons
>>>>      // constructed by replacing an extension of the first input file
>>>>      // with ".exe".
>>>>      if (ctx.outputPath().empty()) {
>>>> -    StringRef path = *dyn_cast<FileNode>(&*files[0])->getPath(ctx);
>>>> +    StringRef path = *cast<FileNode>(&*files[0])->getPath(ctx);
>>>>        ctx.setOutputPath(replaceExtension(ctx, path, ".exe"));
>>>>      }
>>>>
>>>> Modified: lld/trunk/unittests/DriverTests/DriverTest.h
>>>> URL:
>>>> http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/
>>>> DriverTests/DriverTest.h?rev=205332&r1=205331&r2=205332&view=diff
>>>>
>>>> ============================================================
>>>> ==================
>>>> --- lld/trunk/unittests/DriverTests/DriverTest.h (original)
>>>> +++ lld/trunk/unittests/DriverTests/DriverTest.h Tue Apr  1 13:04:56
>>>> 2014
>>>> @@ -35,7 +35,7 @@ protected:
>>>>      std::string inputFile(int index) {
>>>>        const InputElement &inputElement =
>>>> linkingContext()->inputGraph()[index];
>>>>        if (inputElement.kind() == InputElement::Kind::File)
>>>> -      return
>>>> *dyn_cast<FileNode>(&inputElement)->getPath(*linkingContext());
>>>> +      return *cast<FileNode>(&inputElement)
>>>> ->getPath(*linkingContext());
>>>>        llvm_unreachable("not handling other types of input files");
>>>>      }
>>>>
>>>>
>>>> _______________________________________________
>>>> llvm-commits mailing list
>>>> llvm-commits at cs.uiuc.edu
>>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>>>>
>>>>
>>>>
>>> --
>>> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
>>> hosted by
>>> the Linux Foundation
>>>
>>>
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>>>
>>
>
> --
> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted
> by the Linux Foundation
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140401/8bb3cdc8/attachment.html>


More information about the llvm-commits mailing list