Skip to contentSkip to conversation

tighten scope of accidentally global variables

PATCH In review Jim Meyering 3 participants 9 messages May 21, 2012

lore
Share & tools
lore
#0 lore raw

cover tighten scope of accidentally global variables

From: Jim Meyering <[email protected]>

I noticed this commit,

virtio-pci: add missing 'static'

which made this change:

Review

Quoted 2 lines
(quoted diff) +1 -1
-const MemoryRegionPortio virtio_portio[] = {
+static const MemoryRegionPortio virtio_portio[] = {

and wondered if there were other variables like that. The following command shows that there are:
[note that there are probably more: this finds only those
for which the variable name appears in only one source file. ]

    $ for i in $(nm -e *.o|sed -n 's/.* [BCDGRS] //p'); do \
        test $(git grep -lw $i|wc -l) = 1 && echo $i;done
    BlockDeviceIoStatus_lookup
    SpiceQueryMouseMode_lookup
    qemu_boot_opts
    qemu_option_rom_opts
    vmstate_info_scsi_requests
    xen_xcg

The *_lookup names are false positives, since the symbols are actually used from two or more .o files. Here are patches for the others:

Jim Meyering (3):
xen: remove unused global, xen_xcg
scsi: declare vmstate_info_scsi_requests to be static
qemu-config: qemu_option_rom_opts, qemu_boot_opts: declare static

 hw/scsi-bus.c    | 2 +-
 hw/xen_backend.c | 1 -
 qemu-config.c    | 4 ++--
 3 files changed, 3 insertions(+), 4 deletions(-)

--
1.7.10.2.552.gaa3bb87

#1 lore raw

PATCH 1/3 xen: remove unused global, xen_xcg

From: Jim Meyering <[email protected]>

---
 hw/xen_backend.c | 1 -
 1 file changed, 1 deletion(-)
hw/xen_backend.c +0 -1
index 66cb144..e44ced0 100644
@@ -47,7 +47,6 @@
 
 /* public */
 XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
-XenGnttab xen_xcg = XC_HANDLER_INITIAL_VALUE;
 struct xs_handle *xenstore = NULL;
 const char *xen_protocol;
 
Signature · 1 line
1.7.10.2.552.gaa3bb87
Acked-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] xen: remove unused global, xen_xcg

Quoted 4 lines · Jim Meyering

From: Jim Meyering <[email protected]>

Quoted 18 lines
 hw/xen_backend.c | 1 -
 1 file changed, 1 deletion(-)
hw/xen_backend.c +0 -1
index 66cb144..e44ced0 100644
@@ -47,7 +47,6 @@
 
 /* public */
 XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
-XenGnttab xen_xcg = XC_HANDLER_INITIAL_VALUE;
 struct xs_handle *xenstore = NULL;
 const char *xen_protocol;
 
Signature · 1 line
1.7.10.2.552.gaa3bb87
#3 lore raw

PATCH 2/3 scsi: declare vmstate_info_scsi_requests to be static

In reply to Jim Meyering

From: Jim Meyering <[email protected]>

---
 hw/scsi-bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi-bus.c +1 -1
index 8ab9bcd..f10f3ec 100644
@@ -1561,7 +1561,7 @@ static int get_scsi_requests(QEMUFile *f, void *pv, size_t size)
     return 0;
 }
 
-const VMStateInfo vmstate_info_scsi_requests = {
+static const VMStateInfo vmstate_info_scsi_requests = {
     .name = "scsi-requests",
     .get  = get_scsi_requests,
     .put  = put_scsi_requests,
Signature · 1 line
1.7.10.2.552.gaa3bb87
#4 lore raw

PATCH 3/3 qemu-config: qemu_option_rom_opts, qemu_boot_opts: declare static

In reply to Jim Meyering

From: Jim Meyering <[email protected]>

---
 qemu-config.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
qemu-config.c +2 -2
index be84a03..c03e52b 100644
@@ -528,7 +528,7 @@ QemuOptsList qemu_spice_opts = {
     },
 };
 
-QemuOptsList qemu_option_rom_opts = {
+static QemuOptsList qemu_option_rom_opts = {
     .name = "option-rom",
     .implied_opt_name = "romfile",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
@@ -587,7 +587,7 @@ static QemuOptsList qemu_machine_opts = {
     },
 };
 
-QemuOptsList qemu_boot_opts = {
+static QemuOptsList qemu_boot_opts = {
     .name = "boot-opts",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
     .desc = {
Signature · 1 line
1.7.10.2.552.gaa3bb87
#5 lore raw
In reply to Jim Meyering
Quoted 8 lines · Jim Meyering

From: Jim Meyering <[email protected]>

I noticed this commit,

   virtio-pci: add missing 'static'

which made this change:

Quoted 7 lines

   > -const MemoryRegionPortio virtio_portio[] = { > +static const MemoryRegionPortio virtio_portio[] = {

and wondered if there were other variables like that. The following command shows that there are:
[note that there are probably more: this finds only those for which the variable name appears in only one source file. ]

Also, only for files at the top level. What about functions? ;-)

Quoted 25 lines
   $ for i in $(nm -e *.o|sed -n 's/.* [BCDGRS] //p'); do \
       test $(git grep -lw $i|wc -l) = 1 && echo $i;done
   BlockDeviceIoStatus_lookup
   SpiceQueryMouseMode_lookup
   qemu_boot_opts
   qemu_option_rom_opts
   vmstate_info_scsi_requests
   xen_xcg

The *_lookup names are false positives, since the symbols are actually used from two or more .o files.  Here are patches for the others:

Jim Meyering (3):
 xen: remove unused global, xen_xcg
 scsi: declare vmstate_info_scsi_requests to be static
 qemu-config: qemu_option_rom_opts, qemu_boot_opts: declare static

 hw/scsi-bus.c    | 2 +-
 hw/xen_backend.c | 1 -
 qemu-config.c    | 4 ++--

 3 files changed, 3 insertions(+), 4 deletions(-)

--
1.7.10.2.552.gaa3bb87

#6 lore raw
Quoted 12 lines · Blue Swirl
Quoted 11 lines · Jim Meyering

From: Jim Meyering <[email protected]>

I noticed this commit,

   virtio-pci: add missing 'static'

which made this change:

   > -const MemoryRegionPortio virtio_portio[] = { > +static const MemoryRegionPortio virtio_portio[] = {

Quoted 6 lines
Quoted 4 lines

and wondered if there were other variables like that. The following command shows that there are:
[note that there are probably more: this finds only those for which the variable name appears in only one source file. ]

Also, only for files at the top level.

Thanks. There were so many .o files, I assumed that all were at the top. Searching all .o files, I found many more:

  $ for i in $(nm -e $(find . -name '*.o')|sed -n 's/.* [BCDGRS] //p'|sort -u);\
    do test $(git grep -lw $i|wc -l) = 1 && echo $i;done
  BlockDeviceIoStatus_lookup
  DEFAULT_ATR
  SpiceQueryMouseMode_lookup
  __jit_debug_descriptor
  alpha_num_opcodes
  alpha_num_operands
  alpha_opcodes
  alpha_operands
  arg_table
  backend_enum_table
  bdrv_sheepdog
  bfd_mips_num_builtin_opcodes
  bfd_mips_num_opcodes
  bonito_state
  check_cpuid
  cris_cond15s
  cris_cores
  device_configs
  display_remote
  enforce_cpuid
  floatformat_arm_ext_big
  floatformat_arm_ext_littlebyte_bigword
  floatformat_i387_ext
  floatformat_i960_ext
  floatformat_ia64_quad_big
  floatformat_ia64_quad_little
  floatformat_ia64_spill_big
  floatformat_ia64_spill_little
  floatformat_ieee_double_big
  floatformat_ieee_double_little
  floatformat_ieee_double_littlebyte_bigword
  floatformat_ieee_single_big
  floatformat_ieee_single_little
  floatformat_m68881_ext
  floatformat_m88110_ext
  floatformat_m88110_harris_ext
  fsl_register_prefix
  fw_boot_order
  last_mapping_addr
  last_mapping_sym
  last_type
  leon3_generic_machine
  m68k_numaliases
  m68k_numopcodes
  m68k_opcode_aliases
  m68k_opcodes
  memory_region_transaction_depth
  mips_builtin_opcodes
  mips_fulong2e_machine
  mips_opcodes
  no_reboot
  num_powerpc_operands
  para_features
  powerpc_macros
  powerpc_num_macros
  powerpc_num_opcodes
  powerpc_operands
  pvr_register_prefix
  qemu_boot_opts
  qemu_global_mutex
  qemu_option_rom_opts
  register_prefix
  rtas_next
  s390_virtio_bus_info
  sh_table
  special_register_prefix
  test_image
  timers_state
  v9fs_synth_root
  virtcon_hds
  vmstate_bmdma_status
  vmstate_info_scsi_requests
  vmstate_rxtx_stats
  xen_platform_ioport
  xen_xcg
Quoted 1 line

What about functions? ;-)

I planned to check them separately, as I do in gnulib's sc_tight_scope syntax-check rule:

http://git.sv.gnu.org/cgit/gnulib.git/tree/top/maint.mk#n1443

#7 lore raw
Quoted 18 lines · Jim Meyering
Quoted 17 lines · Blue Swirl
Quoted 15 lines · Jim Meyering

From: Jim Meyering <[email protected]>

I noticed this commit,

   virtio-pci: add missing 'static'

which made this change:

   > -const MemoryRegionPortio virtio_portio[] = { > +static const MemoryRegionPortio virtio_portio[] = {

and wondered if there were other variables like that. The following command shows that there are:
[note that there are probably more: this finds only those for which the variable name appears in only one source file. ]

Quoted 7 lines
Quoted 1 line

Also, only for files at the top level.

Thanks.  There were so many .o files, I assumed that all were at the top. Searching all .o files, I found many more:

 $ for i in $(nm -e $(find . -name '*.o')|sed -n 's/.* [BCDGRS] //p'|sort -u);\
   do test $(git grep -lw $i|wc -l) = 1 && echo $i;done

How about just checking the executables (*-softmmu/qemu-system-xyz and linux-user/qemu-xyz)?

Quoted 76 lines

 BlockDeviceIoStatus_lookup
 DEFAULT_ATR
 SpiceQueryMouseMode_lookup
 __jit_debug_descriptor
 alpha_num_opcodes
 alpha_num_operands
 alpha_opcodes
 alpha_operands
 arg_table
 backend_enum_table
 bdrv_sheepdog
 bfd_mips_num_builtin_opcodes
 bfd_mips_num_opcodes
 bonito_state
 check_cpuid
 cris_cond15s
 cris_cores
 device_configs
 display_remote
 enforce_cpuid
 floatformat_arm_ext_big
 floatformat_arm_ext_littlebyte_bigword
 floatformat_i387_ext
 floatformat_i960_ext
 floatformat_ia64_quad_big
 floatformat_ia64_quad_little
 floatformat_ia64_spill_big
 floatformat_ia64_spill_little
 floatformat_ieee_double_big
 floatformat_ieee_double_little
 floatformat_ieee_double_littlebyte_bigword
 floatformat_ieee_single_big
 floatformat_ieee_single_little
 floatformat_m68881_ext
 floatformat_m88110_ext
 floatformat_m88110_harris_ext
 fsl_register_prefix
 fw_boot_order
 last_mapping_addr
 last_mapping_sym
 last_type
 leon3_generic_machine
 m68k_numaliases
 m68k_numopcodes
 m68k_opcode_aliases
 m68k_opcodes
 memory_region_transaction_depth
 mips_builtin_opcodes
 mips_fulong2e_machine
 mips_opcodes
 no_reboot
 num_powerpc_operands
 para_features
 powerpc_macros
 powerpc_num_macros
 powerpc_num_opcodes
 powerpc_operands
 pvr_register_prefix
 qemu_boot_opts
 qemu_global_mutex
 qemu_option_rom_opts
 register_prefix
 rtas_next
 s390_virtio_bus_info
 sh_table
 special_register_prefix
 test_image
 timers_state
 v9fs_synth_root
 virtcon_hds
 vmstate_bmdma_status
 vmstate_info_scsi_requests
 vmstate_rxtx_stats
 xen_platform_ioport
 xen_xcg

Quoted 6 lines
Quoted 1 line

What about functions? ;-)

I planned to check them separately, as I do in gnulib's sc_tight_scope syntax-check rule:

 http://git.sv.gnu.org/cgit/gnulib.git/tree/top/maint.mk#n1443

Interesting. We could do similar source level checks for patches in checkpatch.pl (which should be a commit hook if it never generated any false positives) and add the object file tests for example to the 'make check' test setup.

#8 lore raw
Quoted 21 lines · Blue Swirl
Quoted 20 lines · Jim Meyering
Quoted 18 lines · Blue Swirl
Quoted 8 lines · Jim Meyering

From: Jim Meyering <[email protected]>

I noticed this commit,

   virtio-pci: add missing 'static'

which made this change:

Quoted 7 lines

   > -const MemoryRegionPortio virtio_portio[] = { > +static const MemoryRegionPortio virtio_portio[] = {

and wondered if there were other variables like that. The following command shows that there are:
[note that there are probably more: this finds only those for which the variable name appears in only one source file. ]

Also, only for files at the top level.

Quoted 8 lines
Quoted 5 lines

Thanks.  There were so many .o files, I assumed that all were at the top. Searching all .o files, I found many more:

 $ for i in $(nm -e $(find . -name '*.o')|sed -n 's/.* [BCDGRS] //p'|sort -u);\
   do test $(git grep -lw $i|wc -l) = 1 && echo $i;done

How about just checking the executables (*-softmmu/qemu-system-xyz and linux-user/qemu-xyz)?

If I understood what you suggest, that would seem to miss only "test_image":
(README-scope-vars is the result of the one above using find and *.o)

$ for i in $(nm -e *-softmmu/qemu-system-* linux-user/qemu-*|sed -n 's/.* [BCDGRS] //p'|sort -u); do test $(git grep -lw $i|wc -l) = 1 && echo $i;done > k
nm: linux-user/qemu-types.h: File format not recognized
[Exit 1]
$ diff -u k README-scope-vars
--- k   2012-05-21 20:38:15.912149444 +0200
+++ README-scope-vars   2012-05-21 20:12:13.270023293 +0200
@@ -53,6 +62,7 @@ rtas_next
 s390_virtio_bus_info
 sh_table
 special_register_prefix
+test_image
 timers_state
 v9fs_synth_root
 virtcon_hds