Skip to content

include/drivers: remove implicit casts from api pointer initialization #21391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2019

Conversation

pabigot
Copy link
Collaborator

@pabigot pabigot commented Dec 13, 2019

C++ disallows implicit cast of void pointers to a non-void pointer type. Presence of implicit casts prevents use of these headers in C++ applications.

Fixes #21290
Fixes #21365 for existing code.

Process: Run the following coccinelle script:

@@
identifier V;
identifier TAG =~ "driver_api";
type T;
expression E;
@@
 T* V =
+(T *)
 E->TAG;

in this command line from $ZEPHYR_BASE:

spatch --sp-file expcast.cocci \
   --include-headers --dir include/ --very-quiet \
 | sed -e '/^\+/s@\*) @*)@' \
 | (cd include/ ; patch -p1)

@zephyrbot
Copy link
Collaborator

All checks passed.

checkpatch (informational only, not a failure)

-:10: WARNING:LONG_LINE: line over 80 characters
#10: FILE: include/audio/codec.h:136:
+	const struct audio_codec_api *api = (const struct audio_codec_api *)dev->driver_api;

-:19: WARNING:LONG_LINE: line over 80 characters
#19: FILE: include/audio/codec.h:152:
+	const struct audio_codec_api *api = (const struct audio_codec_api *)dev->driver_api;

-:28: WARNING:LONG_LINE: line over 80 characters
#28: FILE: include/audio/codec.h:168:
+	const struct audio_codec_api *api = (const struct audio_codec_api *)dev->driver_api;

-:37: WARNING:LONG_LINE: line over 80 characters
#37: FILE: include/audio/codec.h:188:
+	const struct audio_codec_api *api = (const struct audio_codec_api *)dev->driver_api;

-:46: WARNING:LONG_LINE: line over 80 characters
#46: FILE: include/audio/codec.h:206:
+	const struct audio_codec_api *api = (const struct audio_codec_api *)dev->driver_api;

-:90: WARNING:LONG_LINE: line over 80 characters
#90: FILE: include/drivers/can.h:545:
+	const struct can_driver_api *api = (const struct can_driver_api *)dev->driver_api;

-:99: WARNING:LONG_LINE: line over 80 characters
#99: FILE: include/drivers/can.h:566:
+	const struct can_driver_api *api = (const struct can_driver_api *)dev->driver_api;

-:108: WARNING:LONG_LINE: line over 80 characters
#108: FILE: include/drivers/can.h:591:
+	const struct can_driver_api *api = (const struct can_driver_api *)dev->driver_api;

-:121: WARNING:LONG_LINE: line over 80 characters
#121: FILE: include/drivers/eeprom.h:63:
+	const struct eeprom_driver_api *api = (const struct eeprom_driver_api *)dev->driver_api;

-:130: WARNING:LONG_LINE: line over 80 characters
#130: FILE: include/drivers/eeprom.h:84:
+	const struct eeprom_driver_api *api = (const struct eeprom_driver_api *)dev->driver_api;

-:139: WARNING:LONG_LINE: line over 80 characters
#139: FILE: include/drivers/eeprom.h:100:
+	const struct eeprom_driver_api *api = (const struct eeprom_driver_api *)dev->driver_api;

-:152: WARNING:LONG_LINE: line over 80 characters
#152: FILE: include/drivers/flash.h:104:
+	const struct flash_driver_api *api = (const struct flash_driver_api *)dev->driver_api;

-:161: WARNING:LONG_LINE: line over 80 characters
#161: FILE: include/drivers/flash.h:128:
+	const struct flash_driver_api *api = (const struct flash_driver_api *)dev->driver_api;

-:170: WARNING:LONG_LINE: line over 80 characters
#170: FILE: include/drivers/flash.h:159:
+	const struct flash_driver_api *api = (const struct flash_driver_api *)dev->driver_api;

-:179: WARNING:LONG_LINE: line over 80 characters
#179: FILE: include/drivers/flash.h:185:
+	const struct flash_driver_api *api = (const struct flash_driver_api *)dev->driver_api;

-:188: WARNING:LONG_LINE: line over 80 characters
#188: FILE: include/drivers/flash.h:272:
+	const struct flash_driver_api *api = (const struct flash_driver_api *)dev->driver_api;

-:201: WARNING:LONG_LINE: line over 80 characters
#201: FILE: include/drivers/gna.h:140:
+	const struct gna_driver_api *api = (const struct gna_driver_api *)dev->driver_api;

-:210: WARNING:LONG_LINE: line over 80 characters
#210: FILE: include/drivers/gna.h:161:
+	const struct gna_driver_api *api = (const struct gna_driver_api *)dev->driver_api;

-:219: WARNING:LONG_LINE: line over 80 characters
#219: FILE: include/drivers/gna.h:181:
+	const struct gna_driver_api *api = (const struct gna_driver_api *)dev->driver_api;

-:228: WARNING:LONG_LINE: line over 80 characters
#228: FILE: include/drivers/gna.h:203:
+	const struct gna_driver_api *api = (const struct gna_driver_api *)dev->driver_api;

-:241: WARNING:LONG_LINE: line over 80 characters
#241: FILE: include/drivers/i2s.h:355:
+	const struct i2s_driver_api *api = (const struct i2s_driver_api *)dev->driver_api;

-:250: WARNING:LONG_LINE: line over 80 characters
#250: FILE: include/drivers/i2s.h:371:
+	const struct i2s_driver_api *api = (const struct i2s_driver_api *)dev->driver_api;

-:259: WARNING:LONG_LINE: line over 80 characters
#259: FILE: include/drivers/i2s.h:410:
+	const struct i2s_driver_api *api = (const struct i2s_driver_api *)dev->driver_api;

-:268: WARNING:LONG_LINE: line over 80 characters
#268: FILE: include/drivers/i2s.h:469:
+	const struct i2s_driver_api *api = (const struct i2s_driver_api *)dev->driver_api;

-:277: WARNING:LONG_LINE: line over 80 characters
#277: FILE: include/drivers/i2s.h:514:
+	const struct i2s_driver_api *api = (const struct i2s_driver_api *)dev->driver_api;

-:290: WARNING:LONG_LINE: line over 80 characters
#290: FILE: include/drivers/ipm.h:137:
+	const struct ipm_driver_api *api = (const struct ipm_driver_api *)ipmdev->driver_api;

-:299: WARNING:LONG_LINE: line over 80 characters
#299: FILE: include/drivers/ipm.h:153:
+	const struct ipm_driver_api *api = (const struct ipm_driver_api *)ipmdev->driver_api;

-:308: WARNING:LONG_LINE: line over 80 characters
#308: FILE: include/drivers/ipm.h:172:
+	const struct ipm_driver_api *api = (const struct ipm_driver_api *)ipmdev->driver_api;

-:317: WARNING:LONG_LINE: line over 80 characters
#317: FILE: include/drivers/ipm.h:192:
+	const struct ipm_driver_api *api = (const struct ipm_driver_api *)ipmdev->driver_api;

-:326: WARNING:LONG_LINE: line over 80 characters
#326: FILE: include/drivers/ipm.h:210:
+	const struct ipm_driver_api *api = (const struct ipm_driver_api *)ipmdev->driver_api;

-:339: WARNING:LONG_LINE: line over 80 characters
#339: FILE: include/drivers/led.h:80:
+	const struct led_driver_api *api = (const struct led_driver_api *)dev->driver_api;

-:348: WARNING:LONG_LINE: line over 80 characters
#348: FILE: include/drivers/led.h:102:
+	const struct led_driver_api *api = (const struct led_driver_api *)dev->driver_api;

-:357: WARNING:LONG_LINE: line over 80 characters
#357: FILE: include/drivers/led.h:120:
+	const struct led_driver_api *api = (const struct led_driver_api *)dev->driver_api;

-:366: WARNING:LONG_LINE: line over 80 characters
#366: FILE: include/drivers/led.h:138:
+	const struct led_driver_api *api = (const struct led_driver_api *)dev->driver_api;

-:379: WARNING:LONG_LINE: line over 80 characters
#379: FILE: include/irq_nextlevel.h:54:
+	const struct irq_next_level_api *api = (const struct irq_next_level_api *)dev->driver_api;

-:388: WARNING:LONG_LINE: line over 80 characters
#388: FILE: include/irq_nextlevel.h:71:
+	const struct irq_next_level_api *api = (const struct irq_next_level_api *)dev->driver_api;

-:397: WARNING:LONG_LINE: line over 80 characters
#397: FILE: include/irq_nextlevel.h:88:
+	const struct irq_next_level_api *api = (const struct irq_next_level_api *)dev->driver_api;

-:406: WARNING:LONG_LINE: line over 80 characters
#406: FILE: include/irq_nextlevel.h:109:
+	const struct irq_next_level_api *api = (const struct irq_next_level_api *)dev->driver_api;

-:415: WARNING:LONG_LINE: line over 80 characters
#415: FILE: include/irq_nextlevel.h:128:
+	const struct irq_next_level_api *api = (const struct irq_next_level_api *)dev->driver_api;

-:428: WARNING:LONG_LINE: line over 80 characters
#428: FILE: include/ptp_clock.h:42:
+	const struct ptp_clock_driver_api *api = (const struct ptp_clock_driver_api *)dev->driver_api;

-:437: WARNING:LONG_LINE: line over 80 characters
#437: FILE: include/ptp_clock.h:60:
+	const struct ptp_clock_driver_api *api = (const struct ptp_clock_driver_api *)dev->driver_api;

-:446: WARNING:LONG_LINE: line over 80 characters
#446: FILE: include/ptp_clock.h:75:
+	const struct ptp_clock_driver_api *api = (const struct ptp_clock_driver_api *)dev->driver_api;

-:455: WARNING:LONG_LINE: line over 80 characters
#455: FILE: include/ptp_clock.h:90:
+	const struct ptp_clock_driver_api *api = (const struct ptp_clock_driver_api *)dev->driver_api;

-:468: WARNING:LONG_LINE: line over 80 characters
#468: FILE: include/shared_irq.h:62:
+	const struct shared_irq_driver_api *api = (const struct shared_irq_driver_api *)dev->driver_api;

-:477: WARNING:LONG_LINE: line over 80 characters
#477: FILE: include/shared_irq.h:74:
+	const struct shared_irq_driver_api *api = (const struct shared_irq_driver_api *)dev->driver_api;

-:486: WARNING:LONG_LINE: line over 80 characters
#486: FILE: include/shared_irq.h:86:
+	const struct shared_irq_driver_api *api = (const struct shared_irq_driver_api *)dev->driver_api;

- total: 0 errors, 46 warnings, 392 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

Your patch has style problems, please review.

NOTE: Ignored message types: AVOID_EXTERNS BRACES CONFIG_EXPERIMENTAL CONST_STRUCT DATE_TIME FILE_PATH_CHANGES MINMAX NETWORKING_BLOCK_COMMENT_STYLE PRINTK_WITHOUT_KERN_LEVEL SPLIT_STRING VOLATILE

NOTE: If any of the errors are false positives, please report
      them to the maintainers.

Tip: The bot edits this comment instead of posting a new one, so you can check the comment's history to see earlier messages.

Copy link
Member

@alexanderwachter alexanderwachter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for the CAN API.
Thanks @pabigot

Copy link
Member

@stephanosio stephanosio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this patch, as long as we are going to be consistently enforcing explicit void pointer to non-void pointer casting, which is a C++ requirement, in all C headers.

As for the commit title, cocinelle: is rather confusing because the commit is changing implicit casts in the driver headers, not adding a Coccinelle script to detect implicit casts (at first, I thought this patch was adding a Cocinelle script in scripts/coccinelle).

@stephanosio
Copy link
Member

@pabigot Speaking of which, is there any specific reason you are not adding the Coccinelle script in the commit message to scripts/coccinelle?

@pabigot
Copy link
Collaborator Author

pabigot commented Dec 14, 2019

@stephanosio I had been using coccinelle: as a prefix for any tree-wide commit that was a result of invoking cocinelle. The intent is to make clear that the transformation in the commit was automatic, so while there may be errors they aren't as likely as with manual conversions.

This was based on what I believe was existing practice in Linux back when coccinelle was introduced, though I can't find recent evidence for its continued use. (The commit for the script itself is separate, and tagged with scripts/coccinelle:.)

It seemed a useful distinction, but I'm not chained to it.

I didn't add this to scripts because it should not be applied to C source files (where the transformation is unnecessary); its output has to be processed before application in order to conform to the coding standards; it has to be invoked with special arguments to work; and it's not worth the hassle of adding the coccicheck mode support. If we're careful about requiring this idiom for new driver headers we won't need it again, but it's recorded in the commit message so it can be resurrected if necessary.

Copy link
Member

@nashif nashif left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the commit title, cocinelle: is rather confusing because the commit is changing implicit casts in the driver headers, not adding a Coccinelle script to detect implicit casts (at first, I thought this patch was adding a Cocinelle script in scripts/coccinelle).

agree here, you are not making any changes to anything related to coccinelle, so this does not belong in the commit message.
I never put "sed: " in the commit message when I do mass substitutions :)

C++ disallows implicit cast of void pointers to a non-void pointer
type.  Presence of implicit casts prevents use of these headers in C++
applications.

Process: Run the following coccinelle script:

@@
identifier V;
identifier TAG =~ "driver_api";
type T;
expression E;
@@
 T* V =
+(T *)
 E->TAG;

in this command line from $ZEPHYR_BASE:

spatch --sp-file expcast.cocci \
   --include-headers --dir include/ --very-quiet \
 | sed -e '/^\+/s@\*) @*)@' \
 | (cd include/ ; patch -p1)

Signed-off-by: Peter A. Bigot <[email protected]>
@pabigot pabigot changed the title coccinelle: remove implicit casts from driver headers include/drivers: remove implicit casts from api pointer initialization Dec 14, 2019
@pabigot
Copy link
Collaborator Author

pabigot commented Dec 14, 2019

commit message subject updated.

@nashif nashif merged commit 2e358f1 into zephyrproject-rtos:master Dec 15, 2019
@tbursztyka
Copy link
Collaborator

@nashif so we don't give a damn about checkpatch's warning anymore then? 80 chars limit is not enforced as it used to or?

@jfischer-no
Copy link
Collaborator

It should be reverted as it obviously violates coding style.

@alexanderwachter
Copy link
Member

From the Coding-Guideline:
"Statements longer than 80 columns will be broken into sensible chunks, unless exceeding 80 columns significantly increases readability and does not hide information"

Exceeding the 80 cars limit is therefor not strictly forbidden. Sometimes it makes sense to not break the line. For example, I'm not breaking the line if it only has 82 characters.

@jfischer-no
Copy link
Collaborator

NO, it does and it can be broken into sensible chunks. And it still coding style:

https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings

"Coding style is all about readability and maintainability using commonly available tools.

The limit on the length of lines is 80 columns and this is a strongly preferred limit.

Statements longer than 80 columns will be broken into sensible chunks, unless exceeding 80 columns significantly increases readability and does not hide information. Descendants are always substantially shorter than the parent and are placed substantially to the right. The same applies to function headers with a long argument list. However, never break user-visible strings such as printk messages, because that breaks the ability to grep for them."

@pabigot
Copy link
Collaborator Author

pabigot commented Dec 16, 2019

For the record: I didn't split the lines because this was an automated change, and per #21392 there was no way to fix the line length in an automated fashion.

I'll add a PR that ensures these lines are broken.

@nashif
Copy link
Member

nashif commented Dec 17, 2019

@nashif so we don't give a damn about checkpatch's warning anymore then? 80 chars limit is not enforced as it used to or?

Well, to be honest I did not notice the checkpatch warning and the long lines did not bother me while looking at the change in github. If this really matters then warnings of checkpatch should be switched to errors and this should block the change. Obviously this is not the first time such warnings are being ignored (by authors and reviewers), so either we remove the warning altogether or we enforce the rule with an error. I vote for the latter.

@pabigot pabigot deleted the pabigot/20191213b branch December 19, 2019 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: C++
Projects
None yet
8 participants