All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 0/2] Coding style to avoid #ifdef everywhere
@ 2014-02-13  9:30 Masahiro Yamada
  2014-02-13  9:30 ` [U-Boot] [PATCH 1/2] Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee Masahiro Yamada
  2014-02-13  9:30 ` [U-Boot] [PATCH 2/2] Move CONFIG_DISPLAY_CPUINFO to Makefile Masahiro Yamada
  0 siblings, 2 replies; 5+ messages in thread
From: Masahiro Yamada @ 2014-02-13  9:30 UTC (permalink / raw)
  To: u-boot


I agree that many parts of U-Boot code are
sprinkled with #ifdef.

But I am opposed to adding a gimmick to conceal ugly code.
(Rather, we should fix code correctly.)

I guess most of U-Boot developers are working on Linux, too.
But I am afraid U-Boot code is dirty compared with Kernel
especially when it comes to the usage of #ifdef.

I think we all should keep in mind the basic coding style
often used in Linux Kernel.

[1] Unpleasant Coding Style
    ^^^^^^^^^^

In this coding style, #ifdef .. #endif
appear on both callee and caller.

The func definition part is surrounded by #ifdef
like this:

  #ifdef CONFIG_FOO
  int foo_init(struct foo_struct *foo)
  {
        blah blah
        blah blah
  }
  #endif

And the caller is like this:

  int caller_of_foo(void)
  {
  #ifdef
            int error;
  #endif

       blah blah blah

  #ifdef CONFIG_FOO
           error = foo_init(&foo);
           if (error) {
                 printf("Foo error\n");
                 return error;
           }
  #endif

      blah blah blah

  }

If we adopt this style, we must #ifdef
variable declarations as well as the code that uses them.

One funtion is generally called from multiple places.
So we need to add #ifdef to all code which invokes the function.
As a result, we will have code sprinkled with #ifdefs.

[2] Pleasant Coding Style (often used in Linux Kernel.)

In this coding style, #ifdef appear only on the definition and the
prototype.

The definition part is like this:

  #ifdef CONFIG_FOO
  int foo_init(struct foo_struct *foo)
  {
        blah blah
        blah blah
  }
  #endif

(Or, in Makefile
obj-$(CONFIG_FOO) += foo_init.o  )

The prototype in a header file is like this:

  #ifdef CONFIG_FOO
  int foo_init(struct foo_struct *foo);
  #else
  static inline foo_init(struct foo_struct *foo)
  {
          return 0;
  }
  #endif

And the caller is like this:

  int caller_of_foo(void)
  {
        int error;

         blah blah blah

         error = foo_init(&foo);
         if (error) {
               printf("Foo error\n");
               return error;
         }

         blah blah blah

   }

In this style, we don't #ifdef in the caller.

If CONFIG_FOO is not defined, the function is
defined as a static inline function returning a constant value.
We generally expect it will be removed (or quite simplified)
by the compiler optimization.
This is better than weak attribute.
(Weak attribute makes it difficult to track which definition is used.)

Using #ifdef in this style will get over our bad habit:
We are too dependent on garbage collection. (--gc-sections option)
We should compile only what we need in the first place.

This series includes simple examples how we should fix the code.

Comments are welcome.



Masahiro Yamada (2):
  Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee
  Move CONFIG_DISPLAY_CPUINFO to Makefile

 arch/arm/cpu/arm926ejs/omap/Makefile           | 3 ++-
 arch/arm/cpu/arm926ejs/omap/cpuinfo.c          | 4 ++--
 arch/arm/cpu/armv7/omap-common/hwinit-common.c | 3 +++
 arch/arm/cpu/tegra-common/Makefile             | 3 ++-
 arch/arm/cpu/tegra-common/sys_info.c           | 2 --
 arch/arm/include/asm/arch-am33xx/sys_proto.h   | 4 ----
 arch/arm/lib/board.c                           | 4 ----
 board/altera/socfpga/socfpga_cyclone5.c        | 2 ++
 board/freescale/mx53loco/mx53loco.c            | 2 ++
 board/nokia/rx51/rx51.h                        | 2 --
 common/board_f.c                               | 2 --
 include/common.h                               | 7 +++++++
 12 files changed, 20 insertions(+), 18 deletions(-)

-- 
1.8.3.2

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot] [PATCH 1/2] Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee
  2014-02-13  9:30 [U-Boot] [PATCH 0/2] Coding style to avoid #ifdef everywhere Masahiro Yamada
@ 2014-02-13  9:30 ` Masahiro Yamada
  2014-02-19 21:10   ` [U-Boot] [U-Boot, " Tom Rini
  2014-02-13  9:30 ` [U-Boot] [PATCH 2/2] Move CONFIG_DISPLAY_CPUINFO to Makefile Masahiro Yamada
  1 sibling, 1 reply; 5+ messages in thread
From: Masahiro Yamada @ 2014-02-13  9:30 UTC (permalink / raw)
  To: u-boot

 - When CONFIG_DISPLAY_CPUINFO is not enabled,
   print_cpuinfo() should be defined as an empty function
   in a header, include/common.h

 - Remove #ifdef CONFIG_DISPLAY_CPUINFO .. #endif
   from caller, common/board_f.c and arch/arm/lib/board.c

 - Remove redundant prototypes in arch/arm/lib/board.c,
   arch/arm/include/asm/arch-am33x/sys_proto.h and
   board/nokia/rx51/rx51.h, keeping the one in include/common.h

 - Add #ifdef CONFIG_DISPLAY_CPUINFO to the func definition
   where it is missing

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
---

 arch/arm/cpu/armv7/omap-common/hwinit-common.c | 3 +++
 arch/arm/include/asm/arch-am33xx/sys_proto.h   | 4 ----
 arch/arm/lib/board.c                           | 4 ----
 board/altera/socfpga/socfpga_cyclone5.c        | 2 ++
 board/freescale/mx53loco/mx53loco.c            | 2 ++
 board/nokia/rx51/rx51.h                        | 2 --
 common/board_f.c                               | 2 --
 include/common.h                               | 7 +++++++
 8 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/arch/arm/cpu/armv7/omap-common/hwinit-common.c b/arch/arm/cpu/armv7/omap-common/hwinit-common.c
index 85d3754..93796be 100644
--- a/arch/arm/cpu/armv7/omap-common/hwinit-common.c
+++ b/arch/arm/cpu/armv7/omap-common/hwinit-common.c
@@ -254,6 +254,7 @@ u32 get_device_type(void)
 				      (DEVICE_TYPE_MASK)) >> DEVICE_TYPE_SHIFT;
 }
 
+#if defined(CONFIG_DISPLAY_CPUINFO)
 /*
  * Print CPU information
  */
@@ -264,6 +265,8 @@ int print_cpuinfo(void)
 
 	return 0;
 }
+#endif
+
 #ifndef CONFIG_SYS_DCACHE_OFF
 void enable_caches(void)
 {
diff --git a/arch/arm/include/asm/arch-am33xx/sys_proto.h b/arch/arm/include/asm/arch-am33xx/sys_proto.h
index 87b7d36..2e5c356 100644
--- a/arch/arm/include/asm/arch-am33xx/sys_proto.h
+++ b/arch/arm/include/asm/arch-am33xx/sys_proto.h
@@ -17,10 +17,6 @@
 u32 get_cpu_rev(void);
 u32 get_sysboot_value(void);
 
-#ifdef CONFIG_DISPLAY_CPUINFO
-int print_cpuinfo(void);
-#endif
-
 extern struct ctrl_stat *cstat;
 u32 get_device_type(void);
 void save_omap_boot_params(void);
diff --git a/arch/arm/lib/board.c b/arch/arm/lib/board.c
index 38b9c7d..c320a35 100644
--- a/arch/arm/lib/board.c
+++ b/arch/arm/lib/board.c
@@ -197,8 +197,6 @@ static int arm_pci_init(void)
  */
 typedef int (init_fnc_t) (void);
 
-int print_cpuinfo(void);
-
 void __dram_init_banksize(void)
 {
 	gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
@@ -250,9 +248,7 @@ init_fnc_t *init_sequence[] = {
 	serial_init,		/* serial communications setup */
 	console_init_f,		/* stage 1 init of console */
 	display_banner,		/* say that we are here */
-#if defined(CONFIG_DISPLAY_CPUINFO)
 	print_cpuinfo,		/* display cpu info (and speed) */
-#endif
 #if defined(CONFIG_DISPLAY_BOARDINFO)
 	checkboard,		/* display board info */
 #endif
diff --git a/board/altera/socfpga/socfpga_cyclone5.c b/board/altera/socfpga/socfpga_cyclone5.c
index 576066b..a960eb6 100644
--- a/board/altera/socfpga/socfpga_cyclone5.c
+++ b/board/altera/socfpga/socfpga_cyclone5.c
@@ -12,6 +12,7 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+#if defined(CONFIG_DISPLAY_CPUINFO)
 /*
  * Print CPU information
  */
@@ -20,6 +21,7 @@ int print_cpuinfo(void)
 	puts("CPU   : Altera SOCFPGA Platform\n");
 	return 0;
 }
+#endif
 
 /*
  * Print Board information
diff --git a/board/freescale/mx53loco/mx53loco.c b/board/freescale/mx53loco/mx53loco.c
index db0bf17..08dd66f 100644
--- a/board/freescale/mx53loco/mx53loco.c
+++ b/board/freescale/mx53loco/mx53loco.c
@@ -343,6 +343,7 @@ int board_early_init_f(void)
 	return 0;
 }
 
+#if defined(CONFIG_DISPLAY_CPUINFO)
 int print_cpuinfo(void)
 {
 	u32 cpurev;
@@ -356,6 +357,7 @@ int print_cpuinfo(void)
 	printf("Reset cause: %s\n", get_reset_cause());
 	return 0;
 }
+#endif
 
 /*
  * Do not overwrite the console
diff --git a/board/nokia/rx51/rx51.h b/board/nokia/rx51/rx51.h
index 4a230dd..0d2f0a5 100644
--- a/board/nokia/rx51/rx51.h
+++ b/board/nokia/rx51/rx51.h
@@ -22,8 +22,6 @@ struct emu_hal_params_rx51 {
 	u32 param4;
 };
 
-int print_cpuinfo(void);
-
 /*
  * IEN  - Input Enable
  * IDIS - Input Disable
diff --git a/common/board_f.c b/common/board_f.c
index d0ee6f7..02965b0 100644
--- a/common/board_f.c
+++ b/common/board_f.c
@@ -887,9 +887,7 @@ static init_fnc_t init_sequence_f[] = {
 #ifdef CONFIG_PPC
 	checkcpu,
 #endif
-#if defined(CONFIG_DISPLAY_CPUINFO)
 	print_cpuinfo,		/* display cpu info (and speed) */
-#endif
 #if defined(CONFIG_MPC5xxx)
 	prt_mpc5xxx_clks,
 #endif /* CONFIG_MPC5xxx */
diff --git a/include/common.h b/include/common.h
index 438cbdb..cb53902 100644
--- a/include/common.h
+++ b/include/common.h
@@ -304,7 +304,14 @@ extern ulong monitor_flash_len;
 int mac_read_from_eeprom(void);
 extern u8 __dtb_dt_begin[];	/* embedded device tree blob */
 int set_cpu_clk_info(void);
+#if defined(CONFIG_DISPLAY_CPUINFO)
 int print_cpuinfo(void);
+#else
+static inline int print_cpuinfo(void)
+{
+	return 0;
+}
+#endif
 int update_flash_size(int flash_size);
 
 /**
-- 
1.8.3.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [U-Boot] [PATCH 2/2] Move CONFIG_DISPLAY_CPUINFO to Makefile
  2014-02-13  9:30 [U-Boot] [PATCH 0/2] Coding style to avoid #ifdef everywhere Masahiro Yamada
  2014-02-13  9:30 ` [U-Boot] [PATCH 1/2] Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee Masahiro Yamada
@ 2014-02-13  9:30 ` Masahiro Yamada
  2014-02-19 21:10   ` [U-Boot] [U-Boot,2/2] " Tom Rini
  1 sibling, 1 reply; 5+ messages in thread
From: Masahiro Yamada @ 2014-02-13  9:30 UTC (permalink / raw)
  To: u-boot

If the whole code is surrounded by #ifdef(CONFIG_ ) .. #endif,
it should be moved to Makefile.

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
---

 arch/arm/cpu/arm926ejs/omap/Makefile  | 3 ++-
 arch/arm/cpu/arm926ejs/omap/cpuinfo.c | 4 ++--
 arch/arm/cpu/tegra-common/Makefile    | 3 ++-
 arch/arm/cpu/tegra-common/sys_info.c  | 2 --
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/arm/cpu/arm926ejs/omap/Makefile b/arch/arm/cpu/arm926ejs/omap/Makefile
index bd0a2fb..add9232 100644
--- a/arch/arm/cpu/arm926ejs/omap/Makefile
+++ b/arch/arm/cpu/arm926ejs/omap/Makefile
@@ -5,5 +5,6 @@
 # SPDX-License-Identifier:	GPL-2.0+
 #
 
-obj-y	= timer.o cpuinfo.o
+obj-y	= timer.o
+obj-$(CONFIG_DISPLAY_CPUINFO) += cpuinfo.o
 obj-y	+= reset.o
diff --git a/arch/arm/cpu/arm926ejs/omap/cpuinfo.c b/arch/arm/cpu/arm926ejs/omap/cpuinfo.c
index 02332ee..587d99a 100644
--- a/arch/arm/cpu/arm926ejs/omap/cpuinfo.c
+++ b/arch/arm/cpu/arm926ejs/omap/cpuinfo.c
@@ -13,7 +13,7 @@
 #include <command.h>
 #include <linux/compiler.h>
 
-#if defined(CONFIG_DISPLAY_CPUINFO) && defined(CONFIG_OMAP)
+#if defined(CONFIG_OMAP)
 
 #define omap_readw(x)		*(volatile unsigned short *)(x)
 #define omap_readl(x)		*(volatile unsigned long *)(x)
@@ -239,4 +239,4 @@ int print_cpuinfo (void)
 	return 0;
 }
 
-#endif /* #if defined(CONFIG_DISPLAY_CPUINFO) && defined(CONFIG_OMAP) */
+#endif /* #if defined(CONFIG_OMAP) */
diff --git a/arch/arm/cpu/tegra-common/Makefile b/arch/arm/cpu/tegra-common/Makefile
index edfc1a8..34d5734 100644
--- a/arch/arm/cpu/tegra-common/Makefile
+++ b/arch/arm/cpu/tegra-common/Makefile
@@ -8,4 +8,5 @@
 #
 
 obj-y += lowlevel_init.o
-obj-y	+= ap.o board.o sys_info.o clock.o cache.o
+obj-y	+= ap.o board.o clock.o cache.o
+obj-$(CONFIG_DISPLAY_CPUINFO) += sys_info.o
diff --git a/arch/arm/cpu/tegra-common/sys_info.c b/arch/arm/cpu/tegra-common/sys_info.c
index dc8a2e4..de20325 100644
--- a/arch/arm/cpu/tegra-common/sys_info.c
+++ b/arch/arm/cpu/tegra-common/sys_info.c
@@ -8,7 +8,6 @@
 #include <common.h>
 #include <linux/ctype.h>
 
-#ifdef CONFIG_DISPLAY_CPUINFO
 void upstring(char *s)
 {
 	while (*s) {
@@ -30,4 +29,3 @@ int print_cpuinfo(void)
 	/* TBD: Add printf of major/minor rev info, stepping, etc. */
 	return 0;
 }
-#endif	/* CONFIG_DISPLAY_CPUINFO */
-- 
1.8.3.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [U-Boot] [U-Boot, 1/2] Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee
  2014-02-13  9:30 ` [U-Boot] [PATCH 1/2] Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee Masahiro Yamada
@ 2014-02-19 21:10   ` Tom Rini
  0 siblings, 0 replies; 5+ messages in thread
From: Tom Rini @ 2014-02-19 21:10 UTC (permalink / raw)
  To: u-boot

On Thu, Feb 13, 2014 at 06:30:26PM +0900, Masahiro Yamada wrote:

> - When CONFIG_DISPLAY_CPUINFO is not enabled,
>    print_cpuinfo() should be defined as an empty function
>    in a header, include/common.h
> 
>  - Remove #ifdef CONFIG_DISPLAY_CPUINFO .. #endif
>    from caller, common/board_f.c and arch/arm/lib/board.c
> 
>  - Remove redundant prototypes in arch/arm/lib/board.c,
>    arch/arm/include/asm/arch-am33x/sys_proto.h and
>    board/nokia/rx51/rx51.h, keeping the one in include/common.h
> 
>  - Add #ifdef CONFIG_DISPLAY_CPUINFO to the func definition
>    where it is missing
> 
> Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140219/71e5370b/attachment.pgp>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot] [U-Boot,2/2] Move CONFIG_DISPLAY_CPUINFO to Makefile
  2014-02-13  9:30 ` [U-Boot] [PATCH 2/2] Move CONFIG_DISPLAY_CPUINFO to Makefile Masahiro Yamada
@ 2014-02-19 21:10   ` Tom Rini
  0 siblings, 0 replies; 5+ messages in thread
From: Tom Rini @ 2014-02-19 21:10 UTC (permalink / raw)
  To: u-boot

On Thu, Feb 13, 2014 at 06:30:27PM +0900, Masahiro Yamada wrote:

> If the whole code is surrounded by #ifdef(CONFIG_ ) .. #endif,
> it should be moved to Makefile.
> 
> Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140219/a7bb224f/attachment.pgp>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-02-19 21:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-13  9:30 [U-Boot] [PATCH 0/2] Coding style to avoid #ifdef everywhere Masahiro Yamada
2014-02-13  9:30 ` [U-Boot] [PATCH 1/2] Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee Masahiro Yamada
2014-02-19 21:10   ` [U-Boot] [U-Boot, " Tom Rini
2014-02-13  9:30 ` [U-Boot] [PATCH 2/2] Move CONFIG_DISPLAY_CPUINFO to Makefile Masahiro Yamada
2014-02-19 21:10   ` [U-Boot] [U-Boot,2/2] " Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.