Solved

stdint and __attribute__(format)

Posted on 2012-04-04
9
800 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:JohnSantaFe
  • 4
  • 4
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 37809811
>> 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
 

Author Comment

by:JohnSantaFe
ID: 37812317
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
 
LVL 86

Expert Comment

by:jkr
ID: 37812989
'__attribute__' is absolutely gcc-specific, not part of a standard.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 53

Expert Comment

by:Infinity08
ID: 37828713
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
 

Author Comment

by:JohnSantaFe
ID: 37834414
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 37834541
>> 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
 

Author Comment

by:JohnSantaFe
ID: 37835051
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 37835233
>> 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
 

Author Closing Comment

by:JohnSantaFe
ID: 37943853
Looks like the compiler isn't wanting to cooperate.  Thanks for the help.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about a problem I had getting the GPS LocationListener working.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question