[cfe-dev] [llvm-dev] Should functions returning bool return true or false on success?

Chandler Carruth via cfe-dev cfe-dev at lists.llvm.org
Wed Sep 19 15:14:26 PDT 2018


On Tue, Sep 18, 2018 at 9:33 AM David Blaikie via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> On Tue, Sep 18, 2018 at 1:08 AM Hans Wennborg via llvm-dev <
> llvm-dev at lists.llvm.org> wrote:
>
>> On Mon, Sep 17, 2018 at 7:57 PM, Nico Weber via cfe-dev
>> <cfe-dev at lists.llvm.org> wrote:
>> > Hi,
>> >
>> > in https://reviews.llvm.org/D52143 there's some uncertainty if LLVM
>> code
>> > prefers
>> >
>> >   if (!Function())
>> >     // Call to function failed, deal with it
>> >
>> > or
>> >
>> >   if (Function())
>> >     // Call to function failed, deal with it
>> >
>> > (Note that this is about functions returning bool, not int.)
>> >
>> > Folks on that review feel that returning true on success is probably
>> what we
>> > want, but it's not documented anywhere and we do have both forms in the
>> > codebase.
>> >
>> > True on success seems more common:
>> > http://llvm-cs.pcc.me.uk/?q=true+on+success
>> > http://llvm-cs.pcc.me.uk/?q=true+on+error
>> >
>> > Does anyone have a pointer to previous on-list discussion on this? If
>> not,
>> > this thread could be the place where we sort this out once and for all
>> :-)
>>
>> I don't remember on-list discussions about this, but I'd be curious to
>> learn about the background.
>>
>> In particular, true-on-error seems pervasive in our various parsers,
>> both in Clang and LLVM. Is this some parser writing convention that's
>> also used outside LLVM, or why do we do this?
>>
>
> Nah, I believe it was an early LLVM convention (thought it was in the
> style guide at some point - but perhaps it was just an undocumented norm
> that was discussed from time to time) based on the idea that functions
> returning integers on failure in C APIs used zero-on-success,
> non-zero-on-failure - and the idea was that bool false-on-success,
> true-on-failure was consistent with that.
>

It was an early LLVM and Clang convention, pushed much more commonly in
Clang's parser for a while.

The theory was more about "returns whether there is an error" -> "returns
true on error".

That said, there were several long email discussions about this many years
ago (no idea how to find them now) and there was general consensus that new
APIs should probably use "true on success" instead, but that code shouldn't
*mix* the two conventions as that is much more confusing.

At some point, I think it would make a lot of sense to just systematically
fix all of the cases we can find of "true on error".

-Chandler


>
> - Dave
>
>
>>
>> Cheers,
>> Hans
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180919/7ad73009/attachment.html>


More information about the cfe-dev mailing list