Skip to contentSkip to conversation

Coding style to avoid #ifdef everywhere

PATCH New Masahiro Yamada 2 participants 5 messages Feb 13, 2014 – Feb 19, 2014

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

cover Coding style to avoid #ifdef everywhere

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(-)
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 1/2 Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee

- 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

---

 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(-)
arch/arm/cpu/armv7/omap-common/hwinit-common.c +3 -0
index 85d3754..93796be 100644
@@ -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)
 {
arch/arm/include/asm/arch-am33xx/sys_proto.h +0 -4
index 87b7d36..2e5c356 100644
@@ -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);
arch/arm/lib/board.c +0 -4
index 38b9c7d..c320a35 100644
@@ -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
board/altera/socfpga/socfpga_cyclone5.c +2 -0
index 576066b..a960eb6 100644
@@ -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
board/freescale/mx53loco/mx53loco.c +2 -0
index db0bf17..08dd66f 100644
@@ -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
board/nokia/rx51/rx51.h +0 -2
index 4a230dd..0d2f0a5 100644
@@ -22,8 +22,6 @@ struct emu_hal_params_rx51 {
 	u32 param4;
 };
 
-int print_cpuinfo(void);
-
 /*
  * IEN  - Input Enable
  * IDIS - Input Disable
common/board_f.c +0 -2
index d0ee6f7..02965b0 100644
@@ -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 */
include/common.h +7 -0
index 438cbdb..cb53902 100644
@@ -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);
 
 /**
Signature · 1 line
1.8.3.2
#2 lore raw

PATCH 1/2 Move #ifdef(CONFIG_DISPLAY_CPUINFO) from caller to callee

Quoted 7 lines · Masahiro Yamada

- 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

Quoted 8 lines

- 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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#3 lore raw

PATCH 2/2 Move CONFIG_DISPLAY_CPUINFO to Makefile

In reply to Masahiro Yamada

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

---

 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(-)
arch/arm/cpu/arm926ejs/omap/Makefile +2 -1
index bd0a2fb..add9232 100644
@@ -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
arch/arm/cpu/arm926ejs/omap/cpuinfo.c +2 -2
index 02332ee..587d99a 100644
@@ -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) */
arch/arm/cpu/tegra-common/Makefile +2 -1
index edfc1a8..34d5734 100644
@@ -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
arch/arm/cpu/tegra-common/sys_info.c +0 -2
index dc8a2e4..de20325 100644
@@ -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 */
Signature · 1 line
1.8.3.2
#4 lore raw

PATCH 2/2 Move CONFIG_DISPLAY_CPUINFO to Makefile

Quoted 4 lines · Masahiro Yamada

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>