[PATCH] of: Define of_match_ptr() with PTR_IF() to avoid unused variable warnings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When CONFIG_OF is disabled and a driver is built in, it is possible for
an of_device_id structure to be unused, which results in a build warning
with W=1 due to -Wunused-const-variable.

For example, in sound/soc/codecs/src4xxx-i2c.c:

  static const struct of_device_id src4xxx_of_match[] = {
      { .compatible = "ti,src4392", },
      { }
  };
  MODULE_DEVICE_TABLE(of, src4xxx_of_match);

  static struct i2c_driver src4xxx_i2c_driver = {
      .driver = {
          .name = "src4xxx",
          .of_match_table = of_match_ptr(src4xxx_of_match),
      },
      .probe = src4xxx_i2c_probe,
      .id_table = src4xxx_i2c_ids,
  };
  module_i2c_driver(src4xxx_i2c_driver);

A configuration with CONFIG_OF=n and CONFIG_SND_SOC_SRC4XXX_I2C=y
produces

  sound/soc/codecs/src4xxx-i2c.c:28:34: warning: unused variable 'src4xxx_of_match' [-Wunused-const-variable]
  static const struct of_device_id src4xxx_of_match[] = {
                                   ^

because of_patch_ptr() expands to NULL when CONFIG_OF=n and
MODULE_DEVICE_TABLE() expands to nothing when MODULE is not set (i.e.,
when the driver is built into the kernel).

This is a similar situation to the power management suspend and resume
functions, which may or may not be used depending on whether or not
CONFIG_PM is set. The solution for a long time was to make the functions
as __maybe_unused but commit c06ef740d401 ("PM: core: Redefine pm_ptr()
macro") adopted a new solution involving IS_ENABLED() and PTR_IF(),
which allows the compiler to see the referenced object at least once in
the file to clear up the unused warning, while simultaneously allowing
the compiler to eliminate unused code in the final object file.

Do the same thing with of_match_ptr() so that unused device IDs do not
cause warnings. This would have prevented several added #ifdef's, such
as the one added by commit 527a7f52529f ("perf/smmuv3: Fix unused
variable warning when CONFIG_OF=n").

Signed-off-by: Nathan Chancellor <nathan@xxxxxxxxxx>
---
 include/linux/of.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/linux/of.h b/include/linux/of.h
index 766d002bddb9..f2a8d411a0f2 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 #include <linux/bitops.h>
 #include <linux/errno.h>
+#include <linux/kconfig.h>
 #include <linux/kobject.h>
 #include <linux/mod_devicetable.h>
 #include <linux/spinlock.h>
@@ -405,8 +406,6 @@ extern int of_update_property(struct device_node *np, struct property *newprop);
 extern int of_attach_node(struct device_node *);
 extern int of_detach_node(struct device_node *);
 
-#define of_match_ptr(_ptr)	(_ptr)
-
 /*
  * struct property *prop;
  * const __be32 *p;
@@ -843,10 +842,11 @@ static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
 	return PHYS_ADDR_MAX;
 }
 
-#define of_match_ptr(_ptr)	NULL
 #define of_match_node(_matches, _node)	NULL
 #endif /* CONFIG_OF */
 
+#define of_match_ptr(_ptr)	PTR_IF(IS_ENABLED(CONFIG_OF), (_ptr))
+
 /* Default string compare functions, Allow arch asm/prom.h to override */
 #if !defined(of_compat_cmp)
 #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))

base-commit: 4fe89d07dcc2804c8b562f6c7896a45643d34b2f
-- 
2.37.3




[Index of Archives]     [Device Tree Compilter]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux PCI Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Yosemite Backpacking]


  Powered by Linux