ClangBuiltLinux / linux

Linux kernel source tree
Other
241 stars 14 forks source link

Lots of W{,void-}pointer-to-int-cast from ToT clang #887

Closed nathanchance closed 4 years ago

nathanchance commented 4 years ago

There are a lot of new instances of the warning in the kernel, which appears to be due to a semantic difference between GCC and clang. I've commented on the Phabricator post about this to see what they say, I'd like not to send a bunch of patches like I did with -Wmisleading-indentation.

https://reviews.llvm.org/D72231#1878528

nathanchance commented 4 years ago

Well they at least agreed to remove the _Bool conversion warnings but the enum ones are still plentiful (97 unique warnings by my count) so this is going to be fun :(

      4 drivers/ata/ahci_brcm.c:445:18: warning: cast to smaller integer type 'enum brcm_ahci_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/ata/ahci_imx.c:1065:18: warning: cast to smaller integer type 'enum ahci_imx_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/ata/ahci_qoriq.c:277:21: warning: cast to smaller integer type 'enum ahci_qoriq_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/ata/ahci_xgene.c:792:14: warning: cast to smaller integer type 'enum xgene_ahci_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/ata/sata_rcar.c:905:15: warning: cast to smaller integer type 'enum sata_rcar_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/char/ipmi/ipmi_si_platform.c:274:15: warning: cast to smaller integer type 'enum si_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/crypto/exynos-rng.c:280:14: warning: cast to smaller integer type 'enum exynos_prng_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/crypto/inside-secure/safexcel.c:1692:18: warning: cast to smaller integer type 'enum safexcel_eip_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/devfreq/event/exynos-ppmu.c:523:21: warning: cast to smaller integer type 'enum exynos_ppmu_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/dma/mmp_tdma.c:640:10: warning: cast to smaller integer type 'enum mmp_tdma_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/dma/qcom/hidma.c:750:8: warning: cast to smaller integer type 'enum hidma_cap' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/gpu/drm/bridge/adv7511/adv7511_drv.c:1112:19: warning: cast to smaller integer type 'enum adv7511_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/gpu/drm/lima/lima_drv.c:283:13: warning: cast to smaller integer type 'enum lima_gpu_id' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/gpu/drm/mediatek/mtk_drm_drv.c:465:15: warning: cast to smaller integer type 'enum mtk_ddp_comp_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/gpu/drm/pl111/pl111_versatile.c:325:24: warning: cast to smaller integer type 'enum versatile_clcd' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/gpu/drm/tiny/repaper.c:1009:11: warning: cast to smaller integer type 'enum repaper_model' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/ad7418.c:255:16: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/ads7828.c:141:10: warning: cast to smaller integer type 'enum ads7828_chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/adt7475.c:1486:10: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/hwmon/ina2xx.c:445:10: warning: cast to smaller integer type 'enum ina2xx_ids' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/lm63.c:1107:16: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/lm75.c:555:10: warning: cast to smaller integer type 'enum lm75_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/lm85.c:1560:16: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/hwmon/lm90.c:1780:16: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/max6697.c:612:16: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/pmbus/ibm-cffps.c:484:8: warning: cast to smaller integer type 'enum versions' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/pmbus/max20730.c:322:13: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/pmbus/tps53679.c:194:13: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/pmbus/ucd9000.c:524:10: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/pmbus/ucd9200.c:107:10: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/hwmon/tmp513.c:725:14: warning: cast to smaller integer type 'enum tmp51x_ids' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/i2c/busses/i2c-bcm-iproc.c:902:3: warning: cast to smaller integer type 'enum bcm_iproc_i2c_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/i2c/busses/i2c-pxa.c:1227:15: warning: cast to smaller integer type 'enum pxa_i2c_types' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/i2c/busses/i2c-rcar.c:945:18: warning: cast to smaller integer type 'enum rcar_i2c_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/iio/accel/bma180.c:825:10: warning: cast to smaller integer type 'enum chip_ids' from 'const void *' [-Wvoid-pointer-to-int-cast]
      2 drivers/iio/adc/ina2xx-adc.c:974:10: warning: cast to smaller integer type 'enum ina2xx_ids' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/iio/adc/rcar-gyroadc.c:513:16: warning: cast to smaller integer type 'enum rcar_gyroadc_model' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/iio/adc/ti-ads1015.c:947:9: warning: cast to smaller integer type 'enum chip_ids' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/iio/dac/mcp4725.c:402:14: warning: cast to smaller integer type 'enum chip_id' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c:144:15: warning: cast to smaller integer type 'enum inv_devices' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/iio/magnetometer/ak8975.c:878:13: warning: cast to smaller integer type 'enum asahi_compass_chipset' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/input/touchscreen/mms114.c:453:15: warning: cast to smaller integer type 'enum mms_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/media/dvb-core/dvb_frontend.c:2534:7: warning: cast to smaller integer type 'enum fe_sec_mini_cmd' from 'void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/media/dvb-core/dvb_frontend.c:2543:13: warning: cast to smaller integer type 'enum fe_sec_tone_mode' from 'void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/media/dvb-core/dvb_frontend.c:2544:19: warning: cast to smaller integer type 'enum fe_sec_tone_mode' from 'void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/media/dvb-core/dvb_frontend.c:2553:9: warning: cast to smaller integer type 'enum fe_sec_voltage' from 'void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/media/dvb-core/dvb_frontend.c:2554:22: warning: cast to smaller integer type 'enum fe_sec_voltage' from 'void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/media/platform/mtk-mdp/mtk_mdp_core.c:139:15: warning: cast to smaller integer type 'enum mtk_mdp_comp_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/mfd/hi6421-pmic-core.c:63:9: warning: cast to smaller integer type 'enum hi6421_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/mfd/lp87565.c:73:23: warning: cast to smaller integer type 'enum lp87565_device_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/mfd/max14577.c:409:5: warning: cast to smaller integer type 'enum maxim_device_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/mfd/mxs-lradc.c:145:15: warning: cast to smaller integer type 'enum mxs_lradc_id' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/mfd/stmpe-i2c.c:89:13: warning: cast to smaller integer type 'enum stmpe_partnum' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/mfd/tc3589x.c:343:13: warning: cast to smaller integer type 'enum tc3589x_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/mfd/wm831x-i2c.c:39:10: warning: cast to smaller integer type 'enum wm831x_parent' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/mfd/wm831x-spi.c:36:10: warning: cast to smaller integer type 'enum wm831x_parent' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/mfd/wm8994-core.c:639:19: warning: cast to smaller integer type 'enum wm8994_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/mtd/maps/physmap-versatile.c:208:25: warning: cast to smaller integer type 'enum versatile_flashprot' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/mtd/nand/raw/vf610_nfc.c:856:17: warning: cast to smaller integer type 'enum vf610_nfc_variant' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/mux/adgs1408.c:62:12: warning: cast to smaller integer type 'enum adgs1408_chip_id' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/net/can/spi/hi311x.c:874:17: warning: cast to smaller integer type 'enum hi3110_model' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/net/can/spi/mcp251x.c:1073:17: warning: cast to smaller integer type 'enum mcp251x_model' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/net/ethernet/apm/xgene/xgene_enet_main.c:2042:20: warning: cast to smaller integer type 'enum xgene_enet_id' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/net/ethernet/marvell/mvmdio.c:284:9: warning: cast to smaller integer type 'enum orion_mdio_bus_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/net/ethernet/ni/nixge.c:1257:12: warning: cast to smaller integer type 'enum nixge_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/net/ethernet/renesas/ravb_main.c:2021:12: warning: cast to smaller integer type 'enum ravb_chip_id' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/net/phy/mdio-xgene.c:337:13: warning: cast to smaller integer type 'enum xgene_mdio_id' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/net/wireless/ath/ath10k/ahb.c:751:11: warning: cast to smaller integer type 'enum ath10k_hw_rev' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/net/wireless/ath/ath11k/ahb.c:918:15: warning: cast to smaller integer type 'enum ath11k_hw_rev' from 'const void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/pci/controller/pcie-iproc-platform.c:56:15: warning: cast to smaller integer type 'enum iproc_pcie_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/phy/broadcom/phy-bcm-ns-usb3.c:217:17: warning: cast to smaller integer type 'enum bcm_ns_family' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/phy/broadcom/phy-bcm-ns-usb3.c:324:17: warning: cast to smaller integer type 'enum bcm_ns_family' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/phy/broadcom/phy-bcm-sr-usb.c:370:13: warning: cast to smaller integer type 'enum bcm_usb_phy_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/phy/broadcom/phy-brcm-sata.c:767:19: warning: cast to smaller integer type 'enum brcm_sata_phy_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/phy/marvell/phy-pxa-usb.c:300:26: warning: cast to smaller integer type 'enum pxa_usb_phy_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/phy/ti/phy-j721e-wiz.c:782:14: warning: cast to smaller integer type 'enum wiz_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      2 drivers/platform/x86/hp-wmi.c:323:24: warning: cast to smaller integer type 'enum hp_wmi_radio' from 'void *' [-Wvoid-pointer-to-int-cast]
      5 drivers/power/reset/vexpress-poweroff.c:124:10: warning: cast to smaller integer type 'enum vexpress_reset_func' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/power/supply/ltc2941-battery-gauge.c:476:13: warning: cast to smaller integer type 'enum ltc294x_id' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/regulator/lp872x.c:876:5: warning: cast to smaller integer type 'enum lp872x_regulator_id' from 'void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/regulator/ltc3589.c:398:22: warning: cast to smaller integer type 'enum ltc3589_variant' from 'const void *' [-Wvoid-pointer-to-int-cast]
      9 drivers/reset/hisilicon/hi6220_reset.c:107:9: warning: cast to smaller integer type 'enum hi6220_reset_ctrl_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/rtc/rtc-ds1307.c:1605:18: warning: cast to smaller integer type 'enum ds_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/rtc/rtc-jz4740.c:320:15: warning: cast to smaller integer type 'enum jz4740_rtc_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      4 drivers/rtc/rtc-mxc.c:325:20: warning: cast to smaller integer type 'enum imx_rtc_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/rtc/rtc-rs5c372.c:651:19: warning: cast to smaller integer type 'enum rtc_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      6 drivers/rtc/rtc-rv8803.c:545:18: warning: cast to smaller integer type 'enum rv8803_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/scsi/fcoe/fcoe_transport.c:863:27: warning: cast to smaller integer type 'enum fip_mode' from 'void *' [-Wvoid-pointer-to-int-cast]
      8 drivers/soc/renesas/rmobile-sysc.c:206:22: warning: cast to smaller integer type 'enum pd_types' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/spi/spi-pxa2xx.c:1547:10: warning: cast to smaller integer type 'enum pxa_ssp_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/spi/spi-sc18is602.c:266:12: warning: cast to smaller integer type 'enum chips' from 'const void *' [-Wvoid-pointer-to-int-cast]
      7 drivers/thermal/samsung/exynos_tmu.c:897:14: warning: cast to smaller integer type 'enum soc_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
     14 mm/rmap.c:1369:25: warning: cast to smaller integer type 'enum ttu_flags' from 'void *' [-Wvoid-pointer-to-int-cast]
      8 sound/soc/codecs/rt5677.c:5582:19: warning: cast to smaller integer type 'enum rt5677_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 sound/soc/codecs/wm8904.c:2197:21: warning: cast to smaller integer type 'enum wm8904_type' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 sound/soc/jz4740/jz4740-i2s.c:504:3: warning: cast to smaller integer type 'enum jz47xx_i2s_version' from 'const void *' [-Wvoid-pointer-to-int-cast]
      8 sound/soc/rockchip/rockchip_pdm.c:490:18: warning: cast to smaller integer type 'enum rk_pdm_version' from 'const void *' [-Wvoid-pointer-to-int-cast]

I'll get cracking on these soon.

nickdesaulniers commented 4 years ago

Why would you cast a void* to an enum? Is there a commonly used macro that's doing this?

nathanchance commented 4 years ago

No. It looks like most of these warnings are from drivers sticking certain values into a void *data member in certain structs like of_device_id, since any driver supporting device tree can use it so you cannot just have a generic enum for that.

https://elixir.bootlin.com/linux/v5.5.4/source/drivers/ata/ahci_brcm.c#L428

https://elixir.bootlin.com/linux/v5.5.4/source/drivers/ata/ahci_brcm.c#L402

https://elixir.bootlin.com/linux/v5.5.4/source/include/linux/mod_devicetable.h#L264

m-gupta commented 4 years ago

@nathanchance did you manage to look at the warnings? We have started hitting them with our ToT LLVM builds as well in Chrome OS.

nathanchance commented 4 years ago

I think the general consensus is that the kernel is not going to change these sites to work around the warning and LLVM does not want to match GCC exactly so we need a separate diagnostic like -W{void,}-pointer-to-enum-cast, which would be default on, which we can then disable for the kernel.

https://godbolt.org/z/ahf79P

I want to try and tackle this myself as it would be my first major contribution to clang but unfortunately, I am a little time starved right now. If someone else wants to implement that ahead of me, please feel free.

m-gupta commented 4 years ago

Chrome OS bug: https://bugs.chromium.org/p/chromium/issues/detail?id=1058965

nathanchance commented 4 years ago

Alright, that wasn't too hard: https://github.com/ClangBuiltLinux/llvm-project/commit/4fd4438882cc7f78e56e147d52d9a1f63b58ba81

I'll throw it up on Phabricator tomorrow, let me know if there are any general comments towards the approach or nitpicks on the patch itself.

cc @davidbolvansky since you seem active in the Sema space (and you commented on the original patch).

nathanchance commented 4 years ago

LLVM patch submitted: https://reviews.llvm.org/D75758

Tested with the following diff for the kernel: https://gist.github.com/767cccf4d093c1342e1994083518815e

m-gupta commented 4 years ago

Thanks, I was planning to work on it as first thing this morning but @nathanchance beat me to it :).

Congrats on your first major LLVM contribution!

nathanchance commented 4 years ago

LLVM part done: https://github.com/llvm/llvm-project/commit/2a41b31fcdfcb67ab7038fc2ffb606fd50b83a84

Kernel part submitted: https://lore.kernel.org/lkml/20200308073400.23398-1-natechancellor@gmail.com/

m-gupta commented 4 years ago

@nathanchance Do you know if the patch has been committed to stable branches?

nathanchance commented 4 years ago

@m-gupta Masahiro just picked up the patch today. Not sure if he plans to send a pull request for -rc6 but if not, certainly for -rc7/final; in other words, the patch should be to mainline within a week hopefully. Once it has made it to mainline, it should be shipped to stable within a couple of weeks (depending o

If that timeline doesn't work for CrOS, here is the backported version that I will send when I get the rejected email from Greg so there should be no conflicts when stable gets merged. It applies cleanly via git am to 4.4 through 4.19. The patch as I have sent it above should apply cleanly to 5.4 and newer.

m-gupta commented 4 years ago

Thanks, I was just looking for a local patch to apply to our builds. Will replace by an official commit once available.

nathanchance commented 4 years ago

In -next: https://git.kernel.org/next/linux-next/c/82f2bc2fcc0160d6f82dd1ac64518ae0a4dd183f

nathanchance commented 4 years ago

Merged into mainline: https://git.kernel.org/linus/82f2bc2fcc0160d6f82dd1ac64518ae0a4dd183f

This should be picked up automatically for stable but if Android/CrOS needs this sooner, here's an "official" backport for 4.19 and earlier: https://gist.github.com/366fe819bf1314eccf3e66d690e40575

$ curl -LSs https://gist.githubusercontent.com/nathanchance/366fe819bf1314eccf3e66d690e40575/raw/c0093b60e2871416eb48fe7de3fb343fee5105b4/gistfile1.txt | git am