schar and uchar macros in usbdrv.h

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
blargg
Rank 3
Rank 3
Posts: 102
Joined: Thu Nov 14, 2013 10:01 pm

schar and uchar macros in usbdrv.h

Post by blargg » Thu Nov 14, 2013 10:34 pm

Perhaps a minor issue, but I'd rather mention it than keep quiet. I am thrilled with V-USB so small things like this stand out more. I am also aware that there's a feedback channel, but I'd rather discuss this with other users rather than merely have it be a request.

The schar and uchar macros can conflict with user code that uses those names for other things in a local scope. e.g.

Code: Select all

#include "usbdrv.h"

void f( void )
{
    int schar = 123; // error: two or more data types in declaration specifiers
}


If they were typedefs, this would be fine:

Code: Select all

typedef signed char schar;

void f( void )
{
    int schar = 123; // no error
}


I realize that the macro might serve a second purpose: to avoid a duplicate typedef in case someone else has also defined schar:

Code: Select all

// other.h
#define schar signed char

// usbdrv.h
#ifndef schar // no problem, already defined so this is skipped
#define schar signed char
#endif


This serves the same purpose but doesn't conflict with user code using the name locally:

Code: Select all

#ifndef schar
#define schar schar
typedef signed char schar;
#endif

void f( void )
{
    int schar = 123; // no error
}


Ideally a library's header file only defines things needed by the user, and doesn't introduce things like schar and uchar into the global space at all, especially as macros, them being lowercase. Things for the implementer's convenience ideally have the library's prefix in their names.

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Re: schar and uchar macros in usbdrv.h

Post by christian » Thu Nov 14, 2013 11:22 pm

You are, of course, right. V-USB has been developed for tiny projects originally, and it's easy to rename variables or avoid particular names in tiny projects. V-USB has grown from that, as you can see, and it would be time for a major cleanup.

BTW: I usually don't have the time to read the forum, just discovered your postings because I was doing some administrative work here. If you have a feature request, please send it to our support, at least in copy.

Post Reply