Bug 60638 - check-webkit-style should warn about types we normally pass as reference
Summary: check-webkit-style should warn about types we normally pass as reference
Status: REOPENED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Tools / Tests (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-11 10:16 PDT by Eric Seidel (no email)
Modified: 2011-05-13 10:44 PDT (History)
7 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Seidel (no email) 2011-05-11 10:16:25 PDT
check-webkit-style should warn about types we normally pass as reference

If check-webkit-style ever sees a IntSize or IntPoint passed by value, it should warn.  (I'm sure there are a bunch of others).  I can't think of any reason to no pass them by const &.

https://bugs.webkit.org/show_bug.cgi?id=60592#c6
Comment 1 Darin Adler 2011-05-11 10:26:34 PDT
Despite the fact that I think this is probably a good guideline, there are at least two possible benefits to passing a type like IntPoint by value instead of by reference.

First, Anders Carlsson has claimed that in some situations on some platforms it may be more efficient. He can fill in the details at some point when he has the chance.

Second, if you pass a point by reference, you can run into cases where reentrancy can change the value.

Here's an example:

    scrollTo(m_documentSize)

    inside the scrollTo function

    FrameView::scrollTo(const IntPoint& position)
    {
        ... position has the value passed in ...
        ... do some work that changes the document size ...
        ... position is now the new document size ...
    }

This can mean that if you, say, check for a special value such as zero, the value can change later in the same function.
Comment 2 Eric Seidel (no email) 2011-05-11 10:36:35 PDT
I'm happy to close this as invalid then.  Thank you for the background.
Comment 3 Darin Adler 2011-05-11 11:57:33 PDT
Oh, I wasn’t saying this is invalid. I think the rule is probably a good one. I was just responding to your comment that there are *no* reasons to ever prefer, say, IntPoint, to const IntPoint&. We might still decide that for our project these are never compelling reasons to do so, and perhaps should.
Comment 4 Eric Seidel (no email) 2011-05-11 13:24:27 PDT
OK.  Then sounds like we should come up with a list of types which we generally pass this way and add a rule to the style bot. :)
Comment 5 Alexey Proskuryakov 2011-05-11 15:15:56 PDT
If passing an IntPoint by value would save a register on Mac OS X , then maybe we should just do that.
Comment 6 Alexey Proskuryakov 2011-05-11 15:27:35 PDT
I guess my comment didn't make a lot of sense. It's just my feeling that passing by value would be better for performance on many platforms, but I'm not really an expert.
Comment 7 Simon Fraser (smfr) 2011-05-12 16:55:24 PDT
> If passing an IntPoint by value would save a register on Mac OS X
We shouldn't forget 32-bit builds and ARM.
Comment 8 David Levin 2011-05-13 09:02:30 PDT
It looks like people are being guided to using "const IntPoint&"

https://bugs.webkit.org/show_bug.cgi?id=60679#c5

Perhaps the guideline is if it begins with a capital letter (aka a custom defined type), then it is a pass by ref (const is optional) or pointer but not by value.

Then if there are exceptions to this rule we can special case those types. To say that they should be pass by value, but it sounds like IntPoint is not an exception.
Comment 9 Levi Weintraub 2011-05-13 10:44:54 PDT
(In reply to comment #8)
> It looks like people are being guided to using "const IntPoint&"
> 
> https://bugs.webkit.org/show_bug.cgi?id=60679#c5
> 
> Perhaps the guideline is if it begins with a capital letter (aka a custom defined type), then it is a pass by ref (const is optional) or pointer but not by value.
> 
> Then if there are exceptions to this rule we can special case those types. To say that they should be pass by value, but it sounds like IntPoint is not an exception.

I'd just like to see a consensus so we can avoid butting heads on individual bugs when which is best could be argued.