RESOLVED [-Wexpansion-to-defined] How to fix/disable it?

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings.

 

Since I've updated Arduino IDE this message appears repeatedly while I compile:

 

this use of "defined" may not be portable [-Wexpansion-to-defined]

 

I have seen that last gcc versions have stricter rules for define(name). Also I have found that with -Wno-expansion-to-defined you can disable that warning but I don't know, within Arduino IDE, I have to type it.

 

Is it possible? How I do it?

 

Thanks in advance for your response.

 

Regards.

This topic has a solution.
Last Edited: Tue. Apr 21, 2020 - 12:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

torri_tz wrote:
this message appears repeatedly while I compile

In your own code? If so, why not fix the issue?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've often wondered about adding compiler options in Arduino. Just googled and hit this:

 

https://forum.arduino.cc/index.php?topic=487940.0

 

(interesting comtribution from our own westfw there - he kind of has a point).  But the main thing is the very last post. That seems to be saying  that you can over-ride with compiler.cpp.flags

 

BTW your OP has got me intrigued. What is the use of #define that leads to "this use of "defined" may not be portable" anyway? presumably the warning exists for a reason so are you doing something that is non-portable and, if so, why?

 

EDIT: looking at platform.txt surely the over-ride is:

# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
compiler.S.extra_flags=
compiler.cpp.extra_flags=
compiler.ar.extra_flags=
compiler.objcopy.eep.extra_flags=
compiler.elf2hex.extra_flags=

 

Last Edited: Tue. Apr 21, 2020 - 09:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
What is the use of #define that leads to "this use of "defined" may not be portable" anyway?

The usual answer is to google the option from the compiler message - in this case,  Wexpansion-to-defined

But doing that doesn't seem to come up with anything for GCC[1]

 

But there is this - for CLang:  https://stackoverflow.com/questions/42074035/how-to-deal-with-clangs-3-9-wexpansion-to-defined-warning

 

which gives this example:

#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))

 

 

EDIT

 

[1] I did find this patch:  http://gcc.1065356.n8.nabble.com/PATCH-v2-cpp-c-Add-Wexpansion-to-defined-td1290639.html

 

But still not seen it in the GCC docs

 

EDIT 2

 

Another Arduino user getting the same message:

 

https://forum.arduino.cc/index.php?topic=654798.0

 

The moderator's suggestion there is to just, "Keep Calm & Carry On"...

 

EDIT 3

 

Ah - found it:

-Wexpansion-to-defined

 

Warn whenever ‘defined’ is encountered in the expansion of a macro (including the case where the macro is expanded by an ‘#if’ directive). Such usage is not portable. This warning is also enabled by -Wpedantic and -Wextra.

 

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

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Apr 21, 2020 - 09:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Then why wouldn't one simply change:

#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))

to be:

 #if (defined(__GNUC__) && !defined(__clang__))
    #define HAS_GNU 1
 #endif

then when HAS_GNU is used you would not get a warning about it being dependent on defined().

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yep - that'd do it.

 

Hence my question/suggestion (to the OP) in #2 ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))
int main (void)
{
	if (HAS_GNU)
		printf("I Have GNU");

    return 0;
}

That looked highly suspicious to me, so I tried the above in Microsoft Visual Studio (easiest because It's currently using it)

And suspicious it was:

1>c:\users\nigel\project\k970\sw19719_win32\source\main.c(146): warning C4013: 'defined' undefined; assuming extern returning int
1>c:\users\nigel\project\k970\sw19719_win32\source\main.c(146): error C2065: '__GNUC__': undeclared identifier
1>c:\users\nigel\project\k970\sw19719_win32\source\main.c(146): error C2065: '__clang__': undeclared identifier

 

Surely a 2-pass preprocessor is necessary to handle the example #define.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well that's a curious result. I thought "defined()" was available in all preprocessors? It's typical if you are just conditionally compiling a block to use:

#ifdef SOMETHING
 ...

but when you are testing 2 or more things it would be:

#if defined(SOMETHING1) && defined(SOMETHING2)
  ...

so I would expect most compilers to be happy with that? The objection here seems to be the use of:

#define THIRD (defined(FIRST) && defined(SECOND))

#if THIRD
  ...

but like I say one could just:

#if (defined(FIRST) && defined(SECOND))
    #define THIRD
#endif

#if THIRD
  ...

I must dig out VS2017 and "play"...

 

EDIT: oh wait a minute I see what you did there. When you wrote:

int main (void)
{
	if (HAS_GNU)
		printf("I Have GNU");

    return 0;
}

I bet you really meant:

int main (void)
{
#if HAS_GNU
		printf("I Have GNU");
#endif

    return 0;
}

Last Edited: Tue. Apr 21, 2020 - 10:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
I bet you really meant:

int main (void)
{
#if HAS_GNU
		printf("I Have GNU");
#endif

    return 0;
}

Yes, I would think so.

 

That builds clean for me.

 

EDIT

 

That was at warning level W3

 

With Wall, I get

warning C4668: 'defined__GNUC__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'

in Visual Studio 2019

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Apr 21, 2020 - 10:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#if HAS_GNU

Now that makes the pre-processor try to resolve all symbols and the defined() will be evaluated. However there is now a stipulation that the HAS_GNU symbol can only be used within the context of the pre-processor. I wonder if that was the intention of the original warning in #1 ?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:
there is now a stipulation that the HAS_GNU symbol can only be used within the context of the pre-processor.

more specifically, that defined() can only be used within the context of the pre-processor.

 

I wonder if that was the intention of the original warning in #1 ?

That does seem to be what the above-mentioned patch is suggesting:

 

that patch wrote:
clang recently added a new warning -Wexpansion-to-defined, which warns when `defined' is used outside a #if expression (including the case of a macro that is then used in a #if expression).

 

http://gcc.1065356.n8.nabble.com/PATCH-v2-cpp-c-Add-Wexpansion-to-defined-td1290639.html

since  "outside a #if expression" must be not "within the context of the pre-processor" ?

 

EDIT

 

Add link

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Apr 21, 2020 - 11:13 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks a lot for your responses.

 

All ""defined" may not be portable"  warnings point to this:

#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
                            (port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
                            (port == 3 && defined(UBRR3H)) 

Which, more or less, fits with awneil's example. 

 

(Sorry for the delay. I'm reading your answers and links as fast I can)

 

Edit

 

Fixed! No more defined warnings.

#if ((PORT == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
     (PORT == 1 && defined(UBRR1H)) || (PORT == 2 && defined(UBRR2H)) || \
     (PORT == 3 && defined(UBRR3H)))
    #define UART_PRESENT
#endif		

Sincerely a lot of thanks and have a good quarantine (whom affects it)

 

Last Edited: Tue. Apr 21, 2020 - 11:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

torri_tz wrote:
Fixed!

Jolly good.

 

Now please mark the solution - see Tip #5 (in my signature, below)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...