Warning: variable ‘PT_YIELD_FLAG’ set but not used

When writing software, I prefer to enable all warnings and errors and also treat warnings as errors. This way I know I’ve covered my bases in terms of compiler detected errors and warnings, and when resolved, my compiler output window is clean.

Compiling software that uses Protothreads, you sometimes get: “warning: variable ‘PT_YIELD_FLAG’ set but not used”.

If you have a look at what is happening in the background with Protothreads, you will see this:

#define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)

#define PT_YIELD(pt)				\
	do {					\
		PT_YIELD_FLAG = 0;		\
		LC_SET((pt)->lc);		\
		if(PT_YIELD_FLAG == 0) {	\
			return PT_YIELDED;	\
		}				\
	} while(0)

#define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
                   PT_INIT(pt); return PT_ENDED; }

A variable “PT_YIELD_FLAG” is declared and assigned a value of one for every call to “PT_BEGIN” . With “PT_END”, the variable is assigned a value of zero. Other than that, “PT_YIELD_FLAG” is never used unless you call “PT_YIELD”. So if you do not use or have the need for “PT_YIELD” in your software, you will get this warning.

To eliminate this warning you can either suppress it from your compiler settings (With GCC you can use the -W option) or handle it in software by declaring “PT_YIELD_FLAG” as unused with the “unused” attribute. I prefer to handle it in software.

#define PT_BEGIN(pt) { char __attribute__ ((unused)) PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)

This attribute will tell the compiler that the variable “PT_YIELD_FLAG” is possibly unused and will not generate a compiler warning when not in use.

For more info on the GCC -W option and compiler attributes:

https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html#Warning-

and

https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Common-Type-Attributes.html#Common-Type-Attributes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s