Skip to contentSkip to conversation

Nested function in drivers/of/of_mdio.c

PATCH In review Jérôme Pouiller 6 participants 7 messages Oct 7, 2009 – Oct 8, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Dear,

I have a problem with commit 8bc487d150b939e69830c39322df4ee486efe381 in file drivers/of/of_mdio.c in function of_phy_find_device.

As you see, this function define match() as a nested function. My
compiler (powerpc-e500-linux-gnu-gcc-3.4.1) raise an error during link
due to this nested definition:
drivers/built-in.o(.text+0x5e2a4): In function `of_phy_find_device': /home/jezz/linux-next/drivers/of/of_mdio.c:107:
undefined reference to `__trampoline_setup'

I am sure I could solve problem by rebuilding my toolchain. Nevertheless, I think nested function definition is not perfectly supported by all compilers. Also, I suggest to place function match() outside of scope of of_phy_find_device as in following patch.

drivers/of/of_mdio.c +4 -5
index bacaa53..c7b2e26 100644
@@ -97,6 +97,10 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
 }
 EXPORT_SYMBOL(of_mdiobus_register);
 
+static int match(struct device *dev, void *phy_np)
+{
+       return dev_archdata_get_node(&dev->archdata) == phy_np;
+}
 /**
  * of_phy_find_device - Give a PHY node, find the phy_device
  * @phy_np: Pointer to the phy's device tree node
@@ -106,11 +110,6 @@ EXPORT_SYMBOL(of_mdiobus_register);
 struct phy_device *of_phy_find_device(struct device_node *phy_np)
 {
        struct device *d;
-       int match(struct device *dev, void *phy_np)
-       {
-               return dev_archdata_get_node(&dev->archdata) == phy_np;
-       }
-
        if (!phy_np)
                return NULL;
 

What do you think about it?

Best regards,

Signature · 1 line
Jérôme Pouiller (jezz AT sysmic DOT org)
#1 lore raw
Quoted 11 lines · Jérôme Pouiller

Dear,

I have a problem with commit 8bc487d150b939e69830c39322df4ee486efe381 in file drivers/of/of_mdio.c in function of_phy_find_device.

As you see, this function define match() as a nested function. My
compiler (powerpc-e500-linux-gnu-gcc-3.4.1) raise an error during link
due to this nested definition:
 drivers/built-in.o(.text+0x5e2a4): In function `of_phy_find_device': /home/jezz/linux-next/drivers/of/of_mdio.c:107:
undefined reference to `__trampoline_setup'

Quoted 4 lines

I am sure I could solve problem by rebuilding my toolchain. Nevertheless, I think nested function definition is not perfectly supported by all compilers. Also, I suggest to place function match() outside of scope of of_phy_find_device as in following patch.

I'm okay with that, but if you're moving code out of the file scope, then please rename the function to of_phy_match() to avoid global namespace conflicts.

g.

Quoted 36 lines
drivers/of/of_mdio.c +4 -5
index bacaa53..c7b2e26 100644
@@ -97,6 +97,10 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  }
  EXPORT_SYMBOL(of_mdiobus_register);
 
+static int match(struct device *dev, void *phy_np)
+{
+       return dev_archdata_get_node(&dev->archdata) == phy_np;
+}
  /**
  * of_phy_find_device - Give a PHY node, find the phy_device
  * @phy_np: Pointer to the phy's device tree node
@@ -106,11 +110,6 @@ EXPORT_SYMBOL(of_mdiobus_register);
  struct phy_device *of_phy_find_device(struct device_node *phy_np)
  {
        struct device *d;
-       int match(struct device *dev, void *phy_np)
-       {
-               return dev_archdata_get_node(&dev->archdata) == phy_np;
-       }
-
        if (!phy_np)
                return NULL;
 

What do you think about it?

Best regards,

--
Jérôme Pouiller (jezz AT sysmic DOT org)

Signature · 2 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
#2 lore raw

Guys, are there other instances of nested C functions in the codebase or was this the first attempt?

Quoted 70 lines · Grant Likely
Quoted 11 lines · Jérôme Pouiller

Dear,

I have a problem with commit 8bc487d150b939e69830c39322df4ee486efe381 in file drivers/of/of_mdio.c in function of_phy_find_device.

As you see, this function define match() as a nested function. My
compiler (powerpc-e500-linux-gnu-gcc-3.4.1) raise an error during link
due to this nested definition:
 drivers/built-in.o(.text+0x5e2a4): In function `of_phy_find_device': /home/jezz/linux-next/drivers/of/of_mdio.c:107:
undefined reference to `__trampoline_setup'

Quoted 4 lines

I am sure I could solve problem by rebuilding my toolchain. Nevertheless, I think nested function definition is not perfectly supported by all compilers. Also, I suggest to place function match() outside of scope of of_phy_find_device as in following patch.

I'm okay with that, but if you're moving code out of the file scope, then please rename the function to of_phy_match() to avoid global namespace conflicts.

g.

Quoted 29 lines
drivers/of/of_mdio.c +4 -5
index bacaa53..c7b2e26 100644
@@ -97,6 +97,10 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  }
  EXPORT_SYMBOL(of_mdiobus_register);
 
+static int match(struct device *dev, void *phy_np)
+{
+       return dev_archdata_get_node(&dev->archdata) == phy_np;
+}
  /**
  * of_phy_find_device - Give a PHY node, find the phy_device
  * @phy_np: Pointer to the phy's device tree node
@@ -106,11 +110,6 @@ EXPORT_SYMBOL(of_mdiobus_register);
  struct phy_device *of_phy_find_device(struct device_node *phy_np)
  {
        struct device *d;
-       int match(struct device *dev, void *phy_np)
-       {
-               return dev_archdata_get_node(&dev->archdata) == phy_np;
-       }
-
        if (!phy_np)
                return NULL;
 
Quoted 7 lines

What do you think about it?

Best regards,

--
Jérôme Pouiller (jezz AT sysmic DOT org)

--
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#5 lore raw

PATCH 1/1 Remove nested function

In reply to Grant Likely

Some toolchains dislike nested function definition, so we define function match outside of of_phy_find_device.

---
 drivers/of/of_mdio.c |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
drivers/of/of_mdio.c +7 -6
index bacaa53..4b22ba5 100644
@@ -97,6 +97,12 @@ int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
 }
 EXPORT_SYMBOL(of_mdiobus_register);
 
+/* Helper function for of_phy_find_device */
+static int of_phy_match(struct device *dev, void *phy_np)
+{
+	return dev_archdata_get_node(&dev->archdata) == phy_np;
+}
+
 /**
  * of_phy_find_device - Give a PHY node, find the phy_device
  * @phy_np: Pointer to the phy's device tree node
@@ -106,15 +112,10 @@ EXPORT_SYMBOL(of_mdiobus_register);
 struct phy_device *of_phy_find_device(struct device_node *phy_np)
 {
 	struct device *d;
-	int match(struct device *dev, void *phy_np)
-	{
-		return dev_archdata_get_node(&dev->archdata) == phy_np;
-	}
-
 	if (!phy_np)
 		return NULL;
 
-	d = bus_find_device(&mdio_bus_type, NULL, phy_np, match);
+	d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
 	return d ? to_phy_device(d) : NULL;
 }
 EXPORT_SYMBOL(of_phy_find_device);
Signature · 6 lines
1.6.0.4

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
#3 lore raw
In reply to [email protected]

I did some grep on codebase. I have not found any other instances of=20 nested functions, but my regexps are not enough to be 100% sure.

Quoted 4 lines · On Wednesday 07 October 2009 18:23:04 [email protected] wrote:

Guys, are there other instances of nested C functions in the codebase or was this the first attempt?
=20
On Wed, Oct 7, 2009 at 9:11 AM, Grant Likely=20

Quoted 1 line · <[email protected]> wrote:
Quoted 1 line

On Wed, Oct 7, 2009 at 9:15 AM, J=E9r=F4me Pouiller <[email protected]>=20

Quoted 68 lines · wrote:
Quoted 58 lines
Quoted 14 lines

Dear,

I have a problem with commit 8bc487d150b939e69830c39322df4ee486efe381 in file drivers/of/of_mdio.c in function of_phy_find_device.

As you see, this function define match() as a nested function. My compiler (powerpc-e500-linux-gnu-gcc-3.4.1) raise an error during link due to this nested definition:
drivers/built-in.o(.text+0x5e2a4): In function
`of_phy_find_device':
/home/jezz/linux-next/drivers/of/of_mdio.c:107: undefined reference to `__trampoline_setup'

Quoted 5 lines

I am sure I could solve problem by rebuilding my toolchain. Nevertheless, I think nested function definition is not perfectly supported by all compilers. Also, I suggest to place function match() outside of scope of of_phy_find_device as in following patch.

I'm okay with that, but if you're moving code out of the file scope, then please rename the function to of_phy_match() to avoid global namespace conflicts.

g.

Quoted 32 lines
drivers/of/of_mdio.c +4 -4
index bacaa53..c7b2e26 100644
@@ -97,6 +97,10 @@ int of_mdiobus_register(struct mii_bus *mdio,
 struct device_node *np) }
 EXPORT_SYMBOL(of_mdiobus_register);
 
+static int match(struct device *dev, void *phy_np)
+{
+       return dev_archdata_get_node(&dev->archdata) =3D=3D phy_np;
+}
 /**
 * of_phy_find_device - Give a PHY node, find the phy_device
 * @phy_np: Pointer to the phy's device tree node
@@ -106,11 +110,6 @@ EXPORT_SYMBOL(of_mdiobus_register);
 struct phy_device *of_phy_find_device(struct device_node *phy_np)
 {
       struct device *d;
-       int match(struct device *dev, void *phy_np)
-       {
-               return dev_archdata_get_node(&dev->archdata) =3D=3D
 phy_np; -       }
-
       if (!phy_np)
               return NULL;

What do you think about it?

Best regards,

Quoted 10 lines
Quoted 2 lines

--
J=E9r=F4me Pouiller (jezz AT sysmic DOT org)

--
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

Quoted 6 lines

=20
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
=20

=2D-=20
J=E9r=F4me Pouiller (jerome AT sysmic DOT org)
Expert Linux Embarqu=E9

#4 lore raw
In reply to Jérôme Pouiller
Quoted 4 lines · Jérôme Pouiller

I did some grep on codebase. I have not found any other instances of nested functions, but my regexps are not enough to be 100% sure.

From Documentation/CodingStyle, written by the Head Penguin himself:

"Heretic people all over the world have claimed that this inconsistency is ... well ... inconsistent, but all right-thinking people know that
(a) K&R are _right_ and (b) K&R are right. Besides, functions are special anyway (you can't nest them in C)."

                ^^^^^^^^^^^^^^^^^^^^^^^^

I interpret it as a clear prohibition of using nested functions in the kernel.

Regards,
Gabriel