hvaldecantos / gvarharmfulness

Are global variables harmful? An Empirical Study of Global Variables in C Language and their Relation to Bugs.
0 stars 0 forks source link

c macro detected as global variable #4

Closed hvaldecantos closed 8 years ago

hvaldecantos commented 8 years ago

In CPIO project, ATTRIB_PACKED was detected as global variable.

$ ctags -x --c-kinds=v --file-scope=no cpiohdr.h
ATTRIB_PACKED    variable     51 cpiohdr.h        } ATTRIB_PACKED;
ATTRIB_PACKED    variable     73 cpiohdr.h        } ATTRIB_PACKED;
ATTRIB_PACKED    variable    110 cpiohdr.h        } ATTRIB_PACKED;

The macro is defined as:

# ifdef HAVE_ATTRIB_PACKED
#  define ATTRIB_PACKED __attribute__((packed))
# endif

When preprocessing cpiohdr.h ATTRIB_PACKED is replace for __attribute__((packed)) to set alignment of memory space. As in:

struct old_cpio_header
{
  unsigned short c_magic;
  unsigned short c_dev;
  unsigned short c_ino;
  unsigned short c_mode;
  unsigned short c_uid;
  unsigned short c_gid;
  unsigned short c_nlink;
  unsigned short c_rdev;
  unsigned short c_mtimes[2];
  unsigned short c_namesize;
  unsigned short c_filesizes[2];
} ATTRIB_PACKED;

What I understand is that this macro instructs the newly defined struct old_cpio_header (and other structs) to align memory space when declaring variables of this type. Therefore, ATTRIB_PACKED shouldn't be considered a global variable.

__attribute__((packed)) Valid application: As a variable attribute, it applies to simple variables, or individual members of an aggregate, namely a structure, union or class.1 As a type attribute, it applies to all members of all aggregates declared of that type. Effect: Sets the maximum alignment of the selected variable, or variables, to which it applies, to the smallest possible alignment value, namely one byte for a variable and one bit for a bit field.

http://scv.bu.edu/computation/bluegene/IBMdocs/compiler/xlc-8.0/html/proguide/ref/modifiers.htm#modifiers http://stackoverflow.com/questions/841433/gcc-attribute-alignedx-explanation

Workaround One way of ignoring macro definition:

$ ctags -x --c-kinds=v --file-scope=no -I ATTRIB_PACKED cpiohdr.h

As this is not practical, we have to detect first all susceptible macros of being detected as global variables and create a file with the tokens to be ignored.

$ ctags -x --c-kinds=v --file-scope=no -I @macro.token_list cpiohdr.h
hvaldecantos commented 8 years ago

similar case in GZIP project, the following macros were detected as global variables:

MAX_DIST,./lib/match.c,107,2725db10000bae65e311a9329973b11053b86f1a,2
MAX_MATCH2,./lib/match.c,145,2725db10000bae65e311a9329973b11053b86f1a,1
_good_match,./lib/match.c,115,2725db10000bae65e311a9329973b11053b86f1a,1
_max_chain_length,./lib/match.c,104,2725db10000bae65e311a9329973b11053b86f1a,1
_nice_match,./lib/match.c,161,2725db10000bae65e311a9329973b11053b86f1a,1
_prev_length,./lib/match.c,112,2725db10000bae65e311a9329973b11053b86f1a,1
_strstart,./lib/match.c,105,2725db10000bae65e311a9329973b11053b86f1a,1
_window,./lib/match.c,154,2725db10000bae65e311a9329973b11053b86f1a,1

These are macros created to be used in assembly language sections within a .c file. As we are analysing C language we should get rid of them. The before mentioned workaround should work for these cases too.

hvaldecantos commented 8 years ago

Enforcing to C language in ctags command gets the same results.

$ ctags -x --c-kinds=v --file-scope=no --language-force=c

It works at file level.

hvaldecantos commented 8 years ago

Fixed in https://github.com/hvaldecantos/gvar/commit/fe125f521a675ddbaf9e6da5c36743abe7d004c1