DBG002 build with -Os optimization gives compiler warnings

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
User5581
Level 3
Level 3
Hi All,

I went to build my XMC4500 project in DAVE 3.1.10 using -Os optimization, and found that the generated DBG002.c produces several of this compiler warning:

dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

This should probably be considered a defect, as DBG002 may not work correctly if the compiler is set to optimize.

The DBG002 version was 1.0.22

Thanks.
0 Likes
2 Replies
Not applicable
Hi Kurt,

The reason for the error is:
The compiler assumes that a variable shall not be accessed with a pointer of another type.
CmdFromHost->Message is of type uint8_t (a character array/string).

However, the compiler is able to optimize the code better with the assumption especially when optimizing for size.

Changing uint32_t * to char * in the following snip of code will fix the warning.

DBG002_ChannelConfig.DynamicFilterMacroGroupID[0] = *((uint32_t*)(&(CmdFromHost->Message[1])));

In principle, typecasting the entire right-hand-side of the above code should not cause the program to fail.

Best regards,
Sophia
0 Likes
User5581
Level 3
Level 3
Hi Sophia,

Thanks for your kind reply.
You are quite right about the changes to the code that can fix the warning. I can make modifications of DBG002.c to do this.

We have a policy where I am working to eliminate all the compiler warnings in our code base. I was hoping that a future version of DBG002 would address these gcc warnings, so that I may keep unmodified DAVE3 generated code in our project. I have many other DAVE3 apps in this project, and none of them (Except DBG002) exhibit additional compiler warnings when optimization is turned on.

Best Regards, and thank you all for such a good tool - DAVE3.

-Kurt
0 Likes