The International Simutrans Forum

 

Author Topic: Fixes for more compiler warnings  (Read 650 times)

0 Members and 1 Guest are viewing this topic.

Offline ceeac

  • *
  • Posts: 45
Fixes for more compiler warnings
« on: March 12, 2019, 10:27:48 AM »
This patch fixes some more compiler warnings, mostly related to unused variables and functions, but also warnings reported by Wclass-memaccess and Wtautological-constant-out-of-range-compare.
The full commit log is available here: https://github.com/ceeac/simutrans/compare/master...ceeac:less-warnings

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5523
  • Languages: EN, NO
Re: Fixes for more compiler warnings
« Reply #1 on: March 12, 2019, 05:05:48 PM »
Other reviewers might find https://stackoverflow.com/questions/45349079/how-to-use-attribute-fallthrough-correctly-in-gcc interesting.

I'm not sure why a decimal constant was changed into a hexidecimal one for motd loading. Neither makes it clear why the value is this strange value.

I don't think putting the return value of fread into len is correct given the way fread is called. The return value will be either 0 or 1, since it returns the number of objects read, not the number of bytes. Swap the second and third argument, and it will be correct.

Why was a lot of UTF-8 related functions just commented out? If we don't need them, there should be no harm in removing them completely. If it turns out that we need them later after all, they can either be found in the file history, or just reimplemented based on UTF-8 specification. They are not that complicated.

In binary_heap_tpl, would it perhaps be better to initialize the member variable in an initializer list?

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4587
  • Languages: EN, DE, AT
Re: Fixes for more compiler warnings
« Reply #2 on: March 12, 2019, 09:11:30 PM »
the change to remove unused work_cost in simhalt.cc looks more like a bug, i.e., this workcost has to be used instead of cost?

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 9512
  • Languages: De,EN,JP
Re: Fixes for more compiler warnings
« Reply #3 on: March 13, 2019, 06:17:32 AM »
I wonder why using decimals instead of hexadecimals gives a warning. That compiler seems to be broken for me ...

The workcost is indeed not booked. Changed in r8718

Also why is everyone so bent on fixing warnings for GCC? There are more compiler out there. MSVC emits more than 100 warnings, although not all useful. A lot about are loosing precisions due to cast; something GCC is awful silent about.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5523
  • Languages: EN, NO
Re: Fixes for more compiler warnings
« Reply #4 on: March 13, 2019, 06:30:18 AM »
Also why is everyone so bent on fixing warnings for GCC?

The right question might be why MSVC users are not bent on fixing warnings. Maybe the warnings are not as visible. Although my sample size is small, I am more alert to warnings that come when I use command-line tools to compile than when compiling inside an IDE.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 9512
  • Languages: De,EN,JP
Re: Fixes for more compiler warnings
« Reply #5 on: March 13, 2019, 08:23:50 AM »
Without being too cynical: Many GCC users seems to have rather extreme standpoints (like OpenSource licenses commata) and thus are also very much bent on achieving zero warning level skills. Ignoring that Oracle, Digital Mars, Windriver, Microsoft, Intel may give different warnings, since those are not standartised.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5523
  • Languages: EN, NO
Re: Fixes for more compiler warnings
« Reply #6 on: March 13, 2019, 03:43:44 PM »
Well, projects were this is noticeable are probably libraries, frameworks or servers where bugs quickly become a serious vulnerability to their users. Without a standardized way of suppressing warnings that the developer has checked out as not a problem, the easiest way to get rid of them is to rewrite the code so that the warnings no longer trigger. If warnings are just allowed to accumulate, people stop paying attention to the new ones, as they are a needle in a stack of needles.

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4587
  • Languages: EN, DE, AT
Re: Fixes for more compiler warnings
« Reply #7 on: March 21, 2019, 04:47:17 PM »
@prissi: there are still unused variables which point to missing accounting of making stuff public.
Code: [Select]
simhalt.cc:2362:22: warning: unused variable 'woowner' [-Wunused-variable]
                                        player_t *const woowner = wo->get_owner();
                                                        ^
simhalt.cc:2366:19: warning: unused variable 'workcost' [-Wunused-variable]
                                        sint64 const workcost = -welt->scale_with_month_length(cost * welt->get_settings().cst_make_public_months);
                                                     ^
simhalt.cc:2353:13: warning: unused variable 'wo' [-Wunused-variable]
                wayobj_t *wo = NULL;

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 9512
  • Languages: De,EN,JP
Re: Fixes for more compiler warnings
« Reply #8 on: March 25, 2019, 02:02:16 PM »
Thank you, removed in r8739

Offline An_dz

  • Web Admin
  • Administrator
  • *
  • Posts: 2892
  • D'oh
    • by An_dz
  • Languages: pt, en, it, (de)
Re: Fixes for more compiler warnings
« Reply #9 on: June 30, 2019, 10:30:37 PM »
Let me revive this topic because I think there are two points worth adding.

As for why 'fixing' warnings, it's because they point to things that can be or lead to undefined behaviour, it compiles because C/C++ is meant to compile even if it might explode. Warnings point that it could explode. While not a standard most compilers agree on nearly all warnings, if one doesn't have a type it's generally just because it's not implemented yet. I've never heard of any compiler that shows a warning for something 'fixed' from a warning of another compiler, though it might have happened in older times.

[...] although not all useful. A lot about are loosing precisions due to cast; something GCC is awful silent about.
That's because we don't pass the flag to check those warnings (-Wconversion). And yes, they are important because the loss of data can lead to errors in other parts of the code. If something was made uint16 it's probably a mistake that out of nowhere it became a sint8.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5523
  • Languages: EN, NO
Re: Fixes for more compiler warnings
« Reply #10 on: July 01, 2019, 05:54:46 AM »
I've never heard of any compiler that shows a warning for something 'fixed' from a warning of another compiler, though it might have happened in older times.
I could have sworn that has happened on Simutrans just a few years back. it is however quite possible that there were actually two issues, or a bigger issue than either compiler realized. One compiler detected one part, but not the other. The other compiler would have found the other, had it not been for the first part, which confuses it without being recognized as an issue. Fixing just the warning from the first compiler will then cause the second compiler to start issuing warnings. It may also be that the fix done for the first compiler is implementation dependent, which can even cause the other compiler to actually issue an all out error. If the developer's/developers' understanding of C++ doesn't let him/them see the full picture, it can certainly feel like a war of compiler warnings. An even more frustrating one if no developer has all compilers involved in the "war".

That's because we don't pass the flag to check those warnings (-Wconversion).
I guess it's not part of -Wall because it may not always be possible to tell the compiler that you know something it doesn't that makes it safe.

Offline An_dz

  • Web Admin
  • Administrator
  • *
  • Posts: 2892
  • D'oh
    • by An_dz
  • Languages: pt, en, it, (de)
Re: Fixes for more compiler warnings
« Reply #11 on: July 01, 2019, 02:23:48 PM »
It may also be that the fix done for the first compiler is implementation dependent, which can even cause the other compiler to actually issue an all out error.
Yes, and on these cases you're going non-standard so it's fine if another implementation complains.

I guess it's not part of -Wall because it may not always be possible to tell the compiler that you know something it doesn't that makes it safe.
Yeah, that's generally the case on many of the warnings compilers issue, sometimes it's intended.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 9512
  • Languages: De,EN,JP
Re: Fixes for more compiler warnings
« Reply #12 on: July 02, 2019, 07:21:11 AM »
If you ever compile on MSVC, you will get hundred or more warning of losing precision and using unsafe string functions (however, the Simutrans project file just silences them, simply we use portable function to be well portable). And the case when fixing one warning triggered an error was with templates and a clash between GCC 2.9x and GCC4 and MSVC7