Bug 122156 - [Tools] Add the check to style-checker for reporting use of static_cast on typename objects.
Summary: [Tools] Add the check to style-checker for reporting use of static_cast on ty...
Status: RESOLVED INVALID
Alias: None
Product: WebKit
Classification: Unclassified
Component: Tools / Tests (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-10-01 05:13 PDT by Ravi Kasibhatla
Modified: 2016-04-25 20:15 PDT (History)
10 users (show)

See Also:


Attachments
Patch (12.55 KB, patch)
2013-10-01 06:46 PDT, Ravi Kasibhatla
no flags Details | Formatted Diff | Diff
Patch (12.62 KB, patch)
2013-10-02 23:33 PDT, Ravi Kasibhatla
no flags Details | Formatted Diff | Diff
Patch (12.67 KB, patch)
2013-10-02 23:54 PDT, Ravi Kasibhatla
dbates: review-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ravi Kasibhatla 2013-10-01 05:13:26 PDT
As part of new changes (added by Kling/Gyuyoung et. all), WebKit is migrating to new syntax of calling toFoo() instead of doing static_cast on Foo objects and more and more classes are being changed to add toFoo helper functions. There should be a way to ensure that the added helpers are actually *used* in the code. Currently, there is no enforcement to use the added toFoo functions nor we exert pressure on the dev to add new toFoo() if required in some new module. We should add this check to style checker and try to *enforce* rigidly the use/addition of toFoo().
For now we can keep the change under *disabled* filter and once all the codebase is changed to use toFoo() we should report a style error if the uploaded patch contains static_cast on any object.
Comment 1 Ravi Kasibhatla 2013-10-01 05:51:21 PDT
I have committed the patch which addresses this change in blink under the CL (https://src.chromium.org/viewvc/blink?view=rev&revision=158628).

My logic for checking the error is simple:
- In the patch, I check for any line which contains regex static_cast<Foo*>.
- If line does contain it, I pick the regex part Foo and search for Foo.h in the codebase.
- In Foo.h, I check for toFoo signature and if it is present, I ask the user to use it. If the signature is not present, I throw the message of adding the toFoo() and using it.
- In cases, where Foo.h is not found, I don't report any error (which I plan to look in future for more refining).

I do need to modify the patch for WebKit to accommodate the use of macros (while checking for presence of toFoo() in the header file) like ELEMENT_TYPE_CASTS and CSS_VALUE_TYPE_CASTS which are used to generate toFoo at compilation times.
Comment 2 Ravi Kasibhatla 2013-10-01 05:52:10 PDT
[Merging from Blink]
Comment 3 Ravi Kasibhatla 2013-10-01 06:46:13 PDT
Created attachment 213079 [details]
Patch
Comment 4 Darin Adler 2013-10-01 09:52:12 PDT
Comment on attachment 213079 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=213079&action=review

> Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py:780
> +        self.assertEqual(message, 'static_cast of class objects is not allowed. Use toFoo defined in Foo.h.'

This message is not clear. What are "class objects"?

> Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py:789
> +        self.assertEqual(message, 'static_cast of class objects is not allowed. Add toFoo in Foo.h and use it instead.'

Ditto.
Comment 5 Ravi Kasibhatla 2013-10-02 23:33:31 PDT
Created attachment 213230 [details]
Patch
Comment 6 Ravi Kasibhatla 2013-10-02 23:54:24 PDT
Created attachment 213232 [details]
Patch
Comment 7 Ravi Kasibhatla 2013-10-02 23:55:44 PDT
(In reply to comment #4)
> (From update of attachment 213079 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=213079&action=review
> 
> > Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py:780
> > +        self.assertEqual(message, 'static_cast of class objects is not allowed. Use toFoo defined in Foo.h.'
> 
> This message is not clear. What are "class objects"?
> 
> > Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py:789
> > +        self.assertEqual(message, 'static_cast of class objects is not allowed. Add toFoo in Foo.h and use it instead.'
> 
> Ditto.
Done.
Comment 8 Ravi Kasibhatla 2013-10-07 10:36:12 PDT
(In reply to comment #7)
> (In reply to comment #4)
> > (From update of attachment 213079 [details] [details])
> > View in context: https://bugs.webkit.org/attachment.cgi?id=213079&action=review
> > 
> > > Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py:780
> > > +        self.assertEqual(message, 'static_cast of class objects is not allowed. Use toFoo defined in Foo.h.'
> > 
> > This message is not clear. What are "class objects"?
> > 
> > > Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py:789
> > > +        self.assertEqual(message, 'static_cast of class objects is not allowed. Add toFoo in Foo.h and use it instead.'
> > 
> > Ditto.
> Done.
Thoughts on the patch?
Comment 9 Darin Adler 2013-10-07 16:03:32 PDT
Comment on attachment 213232 [details]
Patch

The rule this patch enforces might be too strict. I can’t tell by reading the code. When you run this check across existing code, do you get any false positives, errors on lines of code that are not incorrect?
Comment 10 Darin Adler 2013-10-07 16:03:53 PDT
Maybe you could upload the list of failures this finds when run on our current code so we can try to spot false positives?
Comment 11 Daniel Bates 2016-04-25 20:12:58 PDT
Comment on attachment 213232 [details]
Patch

Thank you Ravi for the patch. As it turns out we now use downcast<X>() to downcast a type instead of static_cast<X>() or toX(). It would be nice if style checker could suggest using downcast<X>() instead of static_cast<X>(). This is likely non-trivial to implement.
Comment 12 Daniel Bates 2016-04-25 20:15:24 PDT
I am marking this Resolved/Invalid because we prefer using downcast<X>() instead of static_cast<X>() to toX(). If there is interest in teaching the style checker to suggest use of downcast<X>() then I suggest we file a new bug.
Comment 13 Daniel Bates 2016-04-25 20:15:49 PDT
(In reply to comment #12)
> I am marking this Resolved/Invalid because we prefer using downcast<X>()
> instead of static_cast<X>() to toX()

*instead of static_cast<X>() or toX()