stdint and __attribute__(format)

I'm using a custom function to supplement printf based on a debug level that is defined in the source.  (see code below).  When I turned on the warning -Wmissing-format-attribute in gcc it flagged the function with:
warning: function might be possible candidate for 'printf' format attribute

With a little reading I found a solution to be adding __attribute__(format)  to the declaration so the compiler checks that calls to the function meet the formatting rules of printf.  More precisely the declaration is now:

extern int32_t printfl(const uint8_t print_level, const char *print_me, ...)__attribute__((format(printf, 2, 3)));

That worked, but now for all the calls to the function I'm getting a warning about argument types that don't match. For example:
warning: format '%i' expects type 'int', but argument 3 has type 'int32_t'

Of course int and int32_t are the same thing and I'm certain stdint.h is include in all these locations.

How can I get the __attribute__(format) capability to use the stdint types?

Thanks.


/* Specialized printf function that allows for printing of messages
 * based on a global print level.
 */
int32_t printfl(const uint8_t print_level, const char *print_me, ...)
{
	va_list ap;
	int32_t ret = 0;

	if ( g_comm_config.print_level == 0){
		return 0;
	}

	if ( print_level <= g_comm_config.print_level  )
	{
		va_start(ap, print_me);
		ret = vfprintf(stdout, print_me, ap);
		va_end(ap);
		fflush(stdout);
	}

	return ret;
}

Open in new window

JohnSantaFeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> How can I get the __attribute__(format) capability to use the stdint types?

It does for me.

What version of gcc are you using ?
What compiler flags are you passing ?
Can you post a minimal compilable sample that has the behavior you see ?
0
JohnSantaFeAuthor Commented:
I'm using gcc version 4.3.3.  It's a version for the sparc architecture with the RTEMS operating: system:  sparc-rtems-gcc

Leaving out all the -W warning flags here's what the compile line looks like for the first object:
sparc-rtems-gcc -O0 -mcpu=v8 -g -I./include/  <snip -W's> -c -o bin/CH_Manager.o src/CH_Manager.c

The application includes the RTEMS operating system code so trying to post a full example would be hard.

Is __attribute__ a flag for the compiler and thus this is a gcc issue, or does that come from some library?

Thanks.
0
jkrCommented:
'__attribute__' is absolutely gcc-specific, not part of a standard.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Infinity08Commented:
Sorry for the delay.

>> Leaving out all the -W warning flags

I was actually wanting to see the warning flags specifically, since what you observe is a warning message that you didn't expect.

The cross-compilation bit might be part of the equation though. Are you sure that for the target architecture, int and int32_t are the same ?
0
JohnSantaFeAuthor Commented:
Also, sorry for the delay.

Here is the entire compiler line:
make all
sparc-rtems-gcc -O0 -mcpu=v8 -g -I./include/ -Wall -Wsign-compare -Waggregate-return -Wbad-function-cast -Wcast-align -Wfloat-equal -Wformat-nonliteral -Winline -Wmissing-declarations -Wmissing-format-attribute -Wmissing-prototypes -Wnested-externs -Wno-deprecated-declarations -Wno-format-y2k -Wpacked -Wpointer-arith -Wredundant-decls -Wshadow -Wstrict-prototypes  -Wundef -Wunreachable-code  -c -o bin/CH_Manager.o src/CH_Manager.c

I'm absolutely certain that the system is 32 bit and should be treating int and int32_t.  

However, I did find another strange clue.  The warning is only being generated when  the variable is a uint32_t or int32_t
e.g.
warning: format '%i' expects type 'int', but argument 3 has type 'int32_t'
or
warning: format '%08x' expects type 'unsigned int', but argument 3 has type 'uint32_t'

When the variable is a uint8_t or a uint16_t the warning is not generated.....hmmm
0
Infinity08Commented:
>> When the variable is a uint8_t or a uint16_t the warning is not generated.....hmmm

That further points in the direction that int32_t has a larger width than int.

The warning indicates that not all possible values of an int32_t can be held by an int.

It is always possible that the warning was generated in error.

But for my peace of mind, could you double-check what int32_t is defined as for the target platform ?
Could you also check whether it might not have been re-defined to something other than int by accident ?
0
JohnSantaFeAuthor Commented:
So I right click in int32_t and go to its declaration, it takes me to stdint.h.  Here's the typdefs:
/* 7.18.1.1  Exact-width integer types */
typedef signed char int8_t;
typedef unsigned char   uint8_t;
typedef short  int16_t;
typedef unsigned short  uint16_t;
typedef int  int32_t;
typedef unsigned   uint32_t;
typedef long long  int64_t;
typedef unsigned long long   uint64_t;

Another piece of evidence is I definitely get the warning every time int32_t and uint32_t are used, and, I also get the error when two other types are used:

warning: format '%i' expects type 'int', but argument 3 has type 'mqd_t'
warning: format '%i' expects type 'int', but argument 3 has type 'socket_t'

mqd_t is a message queue descriptor that is typed in mqueue.h as a uint32_t and socket_t is typed as an int32_t in my own code.
0
Infinity08Commented:
>> So I right click in int32_t and go to its declaration, it takes me to stdint.h.  Here's the typdefs:

Ok. Then I guess gcc is wrong heh :)

To get rid of the warnings, you could do an explicit cast to int.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnSantaFeAuthor Commented:
Looks like the compiler isn't wanting to cooperate.  Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.