hippy
Posts: 10556
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Determining warnings to tolerate

Sat Jul 24, 2021 1:49 pm

When I use 'make' after 'cmake' to build my project with "-Wall -Werror" compiler options I have a C file which throws up dozens of warnings which get treated as errors. I can resolve that by modifying my 'CMakeLists.txt' to ignore those warnings for that particular file.

The problem is; how to determine what all the warnings are which need to be tolerated ?

The best I have managed to come up with so far is copying screen output and pasting to a text file, running a Python script against that to find what warnings there are, report those with duplicates removed.

I could improve things by piping 'make' output into a 'warning detector', or have that shell-out to 'make' and capture its output, but was wondering if there were some better way, what others do or would do ?

I considered 'lint' or whatever but host warnings might be different to actual build compiler warnings given and I'd have to figure out some way to ensure it checks all the files I am using and only those. Something done when the 'cmake' generated 'make' executes seems the easier option.

User avatar
Paeryn
Posts: 3349
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Determining warnings to tolerate

Sat Jul 24, 2021 2:38 pm

I'm assuming you aren't wanting an automated tool to determine which warning/errors to tolerate, rather an easy way to look at your source code at the point of each. The systems I use are found in Vim and Emacs.

Vim has quickfix, basically you can compile your program from within Vim using :make. Vim will save the output and parse it for errors and then with :cnext and :cprevious you can jump to the location of the next/previous error in the appropriate source file.

Emacs has a similar feature compilation mode, compile with M-x compile and you'll get the output in a compilation buffer. From within that buffer move the cursor to the error you want to check and press C-c C-c to go to the location in the source file. You can jump to the next error with C-x `

See their docs for more info on what they can do. You can even run gdb from within the editors if you want.

Other editors/IDEs may provide similar features but these are the only ones I've used for the last 25+ years.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

hippy
Posts: 10556
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Determining warnings to tolerate

Sat Jul 24, 2021 3:35 pm

Paeryn wrote:
Sat Jul 24, 2021 2:38 pm
I'm assuming you aren't wanting an automated tool to determine which warning/errors to tolerate, rather an easy way to look at your source code at the point of each.
Actually automated. Something which can identify what warnings must be tolerated (ignored) so I can get a clean build rather than having to fix what causes the warnings, becomes treated as errors, leads to a failed build.

For example, my current MicroPython build, abridged ...

Code: Select all

[ 76%] Building C object CMakeFiles/rp2hack.dir/c4.c.obj
/home/pi/pico/micropython/ports/rp2hack/c4.c: In function 'next':
/home/pi/pico/micropython/ports/rp2hack/c4.c:55:10: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   while (tk = *p) {
          ^~
/home/pi/pico/micropython/ports/rp2hack/c4.c:80:32: warning: implicit declaration of function 'memcmp' [-Wimplicit-function-declaration]
         if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; }
                                ^~~~~~
/home/pi/pico/micropython/ports/rp2hack/c4.c:80:39: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
         if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; }
                                       ^~~~~~
[ 95%] Building C object CMakeFiles/rp2hack.dir/home/pi/pico/micropython/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c.obj
In file included from /home/pi/pico/micropython/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c:32:0:
/home/pi/pico/micropython/lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.h: In function 'print_bufctrl16':
/home/pi/pico/micropython/lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.h:139:27: warning: variable 'bufctrl' set but not used [-Wunused-but-set-variable]
   rp2040_buffer_control_t bufctrl;
                           ^~~~~~~
What I am extracting from that is -

Code: Select all

/home/pi/pico/micropython/ports/rp2hack/c4.c
    -Wparentheses
    -Wimplicit-function-declaration
    -Wint-to-pointer-cast
    -Wincompatible-pointer-types
    -Wmaybe-uninitialized
    -Werror
/home/pi/pico/micropython/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c
    -Wunused-but-set-variable
/home/pi/pico/micropython/lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.c
    -Wunused-but-set-variable
Which then gets turned into what needs inserting in my 'CMakeLists.txt' to allow a successfulbuild-

Code: Select all

set_source_files_properties(
    ${MICROPYTHON_PATH}/ports/rp2hack/c4.c
    PROPERTIES COMPILE_OPTIONS "-Wno-error=parentheses;-Wno-error=implicit-function-declaration;-Wno-error=int-to-pointer-cast;-Wno-error=incompatible-pointer-types;-Wno-error=maybe-uninitialized;-Wno-error"
)
set_source_files_properties(
    ${MICROPYTHON_PATH}/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c
    PROPERTIES COMPILE_OPTIONS "-Wno-error=unused-but-set-variable"
)
set_source_files_properties(
    ${MICROPYTHON_PATH}/lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.c
    PROPERTIES COMPILE_OPTIONS "-Wno-error=unused-but-set-variable"
)
Much as I might like to fix the warnings I am not able to; either because I don't know how to, or because they are present in someone else's git cloned library.

pica200
Posts: 281
Joined: Tue Aug 06, 2019 10:27 am

Re: Determining warnings to tolerate

Sat Jul 24, 2021 7:28 pm

My take on warnings is if you can fix them then do so. There are many warnings that look harmless but they are real bugs. If you don't take care of them they will bite you later.

If you use any lib and the warnings are in that lib i would report them.

User avatar
Paeryn
Posts: 3349
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Determining warnings to tolerate

Sat Jul 24, 2021 9:40 pm

hippy wrote:
Sat Jul 24, 2021 3:35 pm
For example, my current MicroPython build, abridged ...

Code: Select all

[ 76%] Building C object CMakeFiles/rp2hack.dir/c4.c.obj
/home/pi/pico/micropython/ports/rp2hack/c4.c: In function 'next':
/home/pi/pico/micropython/ports/rp2hack/c4.c:55:10: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   while (tk = *p) {
          ^~
/home/pi/pico/micropython/ports/rp2hack/c4.c:80:32: warning: implicit declaration of function 'memcmp' [-Wimplicit-function-declaration]
         if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; }
                                ^~~~~~
/home/pi/pico/micropython/ports/rp2hack/c4.c:80:39: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
         if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; }
                                       ^~~~~~
Much as I might like to fix the warnings I am not able to; either because I don't know how to, or because they are present in someone else's git cloned library.
Where does the directory rp2hack come from? It isn't in micropython on github. The first two warnings in micropython/ports/rp2hack/c4.c should be easy to fix,
  • The parenthesis warning is to try and make it more obvious that you really want an assignment in the test (rather than a mis-typed equality test).
  • The implicit function warning is because string.h wasn't included (where memcmp() is declared).
And as to the last one
  • The integer-to-pointer cast should be dealt with as an error post-haste and not ignored! Even if it works for you now, such code is just waiting to segfault or worse in the future. <Edit: Casting pointers to integers and back is undefined behaviour if they are of different sizes, best to not do it unless you really have to. At least use either intptr_t or uintptr_t rather than a regular int as that's guaranteed to be able to hold a pointer>
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

hippy
Posts: 10556
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Determining warnings to tolerate

Sun Jul 25, 2021 5:49 pm

pica200 wrote:
Sat Jul 24, 2021 7:28 pm
My take on warnings is if you can fix them then do so. There are many warnings that look harmless but they are real bugs. If you don't take care of them they will bite you later.
I'm in total agreement there; why I add "-Wall -Werror" in my compilation options.
pica200 wrote:
Sat Jul 24, 2021 7:28 pm
If you use any lib and the warnings are in that lib i would report them.
I guess I should.
Paeryn wrote:
Sat Jul 24, 2021 9:40 pm
Where does the directory rp2hack come from? It isn't in micropython on github.
It's my local clone of the official 'rp2' port where I do all my hacking on my build of MicroPython, adding new C Extension Modules and the like. That may include using code from others, such as the C4 C interpreter in the case here.

While I probably will get round to making C4 compile without warnings at some point, I need to deal with the fact that it currently doesn't.

I have done that but it was harder than I expected. Hence the move to automate that process, and my wondering if there were better ways to identify warnings I need to tolerate, wondering what others who have far more C build experience than me would do.

User avatar
Paeryn
Posts: 3349
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Determining warnings to tolerate

Sun Jul 25, 2021 6:48 pm

I personally wouldn't automate deactivating warnings, I'd want to check all the occurrences where the warnings are given to make sure there isn't really an error. If I'm then sure it's safe to disable a warning I will, but that means having to remember to re-enable it the moment any changes are made (to make sure you don't inadvertently introduce an error which the warning would have picked up).

The problem with disabling a warning just so the code will compile is that, if say you got 10 warnings suggesting using parentheses around assignments in a conditional, the first 9 might be valid but the 10th was meant to be an equality. If you just disable the warning because the author does that a lot and it normally works you risk not noticing that one time that it was a typo and is in part of the code that doesn't often get executed (but does for you at the most inconvenient time).
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 879
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: Determining warnings to tolerate

Sun Jul 25, 2021 8:42 pm

FWIW we check the SDK with these:

https://github.com/raspberrypi/pico-sdk ... ts.txt#L55

We do occasionally go thru and fix more warnings, but we have the most egregious ones checked for.

hippy
Posts: 10556
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Determining warnings to tolerate

Sun Jul 25, 2021 10:16 pm

Paeryn wrote:
Sun Jul 25, 2021 6:48 pm
I personally wouldn't automate deactivating warnings, I'd want to check all the occurrences where the warnings are given to make sure there isn't really an error.
I suppose I should say that I really don't care if the code giving warnings doesn't run. At some point I will care and then I will fix things, but for now I need to include my c4.c file, warnings or not, because it's a real PITA to not include that file.

I can and previously got by with commenting out "-Wall -Werror" but I don't like doing that. It is better and I can equally get by with "warnings are errors" for everything except the files which I expect to produce warnings, by taking only those out of that regime.

This question was about how best to achieve doing that, not how warnings should be dealt with.

hippy
Posts: 10556
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Determining warnings to tolerate

Sun Jul 25, 2021 10:20 pm

kilograham wrote:
Sun Jul 25, 2021 8:42 pm
FWIW we check the SDK with these:

https://github.com/raspberrypi/pico-sdk ... ts.txt#L55

We do occasionally go thru and fix more warnings, but we have the most egregious ones checked for.
It's worth noting that I have not seen any warnings given for anything I have used from the Pico SDK. The warnings are in TinyUSB and third-party code.

Return to “General”