Solved

stdint and __attribute__(format)

Posted on 2012-04-04
9
784 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
'__attribute__' is absolutely gcc-specific, not part of a standard.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:JohnSantaFe
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
Looks like the compiler isn't wanting to cooperate.  Thanks for the help.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now