Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiler warnings #191

Closed
maxbla opened this issue Aug 4, 2020 · 5 comments
Closed

Fix compiler warnings #191

maxbla opened this issue Aug 4, 2020 · 5 comments

Comments

@maxbla
Copy link

maxbla commented Aug 4, 2020

if you check the box "show verbose output during compilation" in the Arduino IDE, verifying any library that includes RTClib will print warnings.

RTClib.cpp:543:7: warning: 'isPM' may be used uninitialized in this function [-Wmaybe-uninitialized]
       if (isPM) {
       ^
RTClib.cpp:468:28: note: 'isPM' was declared here
   uint8_t hourReformatted, isPM;
                            ^
RTClib.cpp:492:47: warning: 'hourReformatted' may be used uninitialized in this function [-Wmaybe-uninitialized]
         buffer[i + 1] = '0' + hourReformatted % 10;
                                               ^
RTClib.cpp:468:11: note: 'hourReformatted' was declared here
   uint8_t hourReformatted, isPM;

It would be better if compiling didn't print warnings, but as far as I can tell, the uninitalized values aren't being read. The warnings are just being overly careful.

Easy fix: change

  uint8_t hourReformatted, isPM;
  if (apTag) {     // 12 Hour Mode
    if (hh == 0) { // midnight
      isPM = false;
      hourReformatted = 12;
    } else if (hh == 12) { // noon
      isPM = true;
      hourReformatted = 12;
    } else if (hh < 12) { // morning
      isPM = false;
      hourReformatted = hh;
    } else { // 1 o'clock or after
      isPM = true;
      hourReformatted = hh - 12;
    }
  }

to

  uint8_t hourReformatted = hh;
  bool isPM = hh >= 12;
  if (apTag) {     // 12 Hour Mode
    if (hh == 0) { // midnight
      hourReformatted = 12;
    } else if (hh <= 12) { // morning/noon
      hourReformatted = hh;
    } else { // 1 o'clock or after
      hourReformatted = hh - 12;
    }
  }

I can submit a PR making this change, but I'd prefer to wait until my first PR, #190 goes through.

@netdudeuk
Copy link

netdudeuk commented Sep 20, 2020

I get a variation of these warnings when using PlatformIO for a NODEMCU -

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
lib_deps =
RTClib
lib_ignore = TinyWireM

C:\Users\me.platformio\lib\RTClib\RTClib.cpp: In member function 'char* DateTime::toString(cha
C:\Users\me.platformio\lib\RTClib\RTClib.cpp:543:7: warning: 'isPM' may be used uninitialized
if (isPM) {
^
C:\Users\me.platformio\lib\RTClib\RTClib.cpp:492:51: warning: 'hourReformatted' may be used un
buffer[i + 1] = '0' + hourReformatted % 10;
^_

The truncation was done by something in the IDE, not by myself.

However, I don't get the warnings with the same source code when using for a different board (Arduino Nano) -

[env:nanoatmega328new]
platform = atmelavr
board = nanoatmega328
framework = arduino
lib_deps =
RTClib
lib_ignore = TinyWireM

Does the code still get compiled ok for the NODEMCU ?

Would maxbla's fix work in this case ?

Thanks

@edgar-bonet
Copy link
Contributor

Compilers differ in how they analyze the code. One compiler may see a “possible” problem where another one realizes there is no actual problem, and a third one doesn't even notice there is something to worry about.

The code should compile file for either board. maxbla's fix should silence the warnings, with no measurable difference in performance.

@antwal
Copy link

antwal commented Oct 27, 2020

Hi,

same warnings problem on PlatformIO

.pio/libdeps/nanoatmega328/RTClib/RTClib.cpp: In member function 'bool DateTime::operator<(const DateTime&) const':
.pio/libdeps/nanoatmega328/RTClib/RTClib.cpp:672:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   return (yOff + 2000 < right.year() ||
                       ^
.pio/libdeps/nanoatmega328/RTClib/RTClib.cpp:673:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
           (yOff + 2000 == right.year() &&
                        ^
.pio/libdeps/nanoatmega328/RTClib/RTClib.cpp: In member function 'bool DateTime::operator==(const DateTime&) const':
.pio/libdeps/nanoatmega328/RTClib/RTClib.cpp:696:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   return (right.year() == yOff + 2000 && right.month() == m &&
                        ^

@edgar-bonet
Copy link
Contributor

@antwal: The -Wsign-compare warnings have been fixed by pull request #196.

@caternuson
Copy link
Contributor

Should be fixed with latest library release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants