Skip to content

Conversation

@paveljanik
Copy link
Contributor

Additional -Wshadow fixes for gcc 4.8.5.

@paveljanik paveljanik changed the title WIP: Wshadow: gcc 4.8.5 fixes WIP: Wshadow: various gcc fixes Mar 3, 2017
@paveljanik
Copy link
Contributor Author

paveljanik commented Mar 3, 2017

gcc version 5.3.1 20160301 [gcc-5-branch revision 233849] is completely -Wshadow clean.
The same applies to gcc version 6.2.1 20160826 [gcc-6-branch revision 239773].

Copy link
Contributor

@gmaxwell gmaxwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK (lightly reviewed)

@maflcko
Copy link
Member

maflcko commented Mar 4, 2017 via email

@paveljanik
Copy link
Contributor Author

Yes. It is WIP. I'm adding commit by commit for various gcc/clang version I test so people do not see warnings. I still have to test one more clang version I have installed on one system.

Wil squash afterwards of course.

@paveljanik
Copy link
Contributor Author

BTW, this PR is open also for others who see some -Wshadow warnings on their preferred compiler. Having all fixes in one PR should lower the impact of turning the warnings on by default.

@laanwj
Copy link
Member

laanwj commented Mar 4, 2017

Sigh, more Wshadow changes. Is this ever done?

@paveljanik paveljanik force-pushed the 20170303_Wshadow_streams branch from b43439c to 279c29d Compare March 4, 2017 21:57
@paveljanik
Copy link
Contributor Author

... and squashed as both clang version tested are -Wshadow clean.

clang seems to be much better or people use it more when developing for Bitcoin Core ;-)

@paveljanik paveljanik changed the title WIP: Wshadow: various gcc fixes Wshadow: various gcc fixes Mar 4, 2017
@laanwj
Copy link
Member

laanwj commented Mar 5, 2017

clang seems to be much better or people use it more when developing for Bitcoin Core ;-)

Aside: In my case, yes. FreeBSD defaults to clang nowadays, and on Linux I use clang specifically for Bitcoin Core because it compiles faster and uses less memory (=more parallelism with same amount of RAM).

GCC is supposed to produce slightly better code for x86 though, or at least that used to be the case years ago. So for production builds it makes sense to use that (which we do in gitian). I think @theuni was discussing switching all the builds to clang at some point though because it makes cross-compiling easier (also clang support for Win32 is supposed to be fairly good, whereas GCC is giving us a lot of trouble recently.) But it's not possible to say generally that one of them is better.

Copy link
Contributor

@dcousens dcousens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

@paveljanik
Copy link
Contributor Author

paveljanik commented Mar 6, 2017

Added fix for #8574's local variable redeclaration (aka shadowing).

For easier searching, here is the log:

wallet/db.cpp:620:40: warning: declaration shadows a local variable [-Wshadow]
            map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
                                       ^
wallet/db.cpp:610:36: note: previous declaration is here
        map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
                                   ^
1 warning generated.

@gmaxwell
Copy link
Contributor

gmaxwell commented Mar 6, 2017

paveljanik have you compiled with a make clean lately? I'm flooded with streams.h:407:44: warning: declaration of ‘data’ shadows a member of 'this' [-Wshadow]

@paveljanik
Copy link
Contributor Author

@jtimon
Copy link
Contributor

jtimon commented Mar 7, 2017

utACK bfc6be9

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this shadowing? (and can we make it static?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With gcc4.8.5:

bitcoin@linux-5bz0:~/bitcoin> make CC=gcc
Making all in src
make[1]: Entering directory '/home/bitcoin/bitcoin/src'
make[2]: Entering directory '/home/bitcoin/bitcoin/src'
  CXX      libbitcoin_server_a-torcontrol.o
/usr/include/boost/date_time/time_system_counted.hpp: In instantiation of 'static boost::date_time::counted_time_system<time_rep>::time_rep_type boost::date_time::counted_time_system<time_rep>::add_days(const time_rep_type&, const date_duration_type&) [with time_rep = boost::date_time::counted_time_rep<boost::posix_time::millisec_posix_time_system_config>; boost::date_time::counted_time_system<time_rep>::time_rep_type = boost::date_time::counted_time_rep<boost::posix_time::millisec_posix_time_system_config>; boost::date_time::counted_time_system<time_rep>::date_duration_type = boost::gregorian::date_duration]':
/usr/include/boost/date_time/time.hpp:141:45:   required from 'boost::date_time::base_time<T, time_system>::time_type boost::date_time::base_time<T, time_system>::operator+(const date_duration_type&) const [with T = boost::posix_time::ptime; time_system = boost::date_time::counted_time_system<boost::date_time::counted_time_rep<boost::posix_time::millisec_posix_time_system_config> >; boost::date_time::base_time<T, time_system>::time_type = boost::posix_time::ptime; boost::date_time::base_time<T, time_system>::date_duration_type = boost::gregorian::date_duration]'
/usr/include/boost/date_time/posix_time/date_duration_operators.hpp:33:37:   required from here
torcontrol.cpp:665:20: warning: shadowed declaration is here [-Wshadow]
 struct event_base *base;
                    ^
/usr/include/boost/date_time/time_system_counted.hpp: In instantiation of 'static boost::date_time::counted_time_system<time_rep>::time_rep_type boost::date_time::counted_time_system<time_rep>::add_time_duration(const time_rep_type&, boost::date_time::counted_time_system<time_rep>::time_duration_type) [with time_rep = boost::date_time::counted_time_rep<boost::posix_time::millisec_posix_time_system_config>; boost::date_time::counted_time_system<time_rep>::time_rep_type = boost::date_time::counted_time_rep<boost::posix_time::millisec_posix_time_system_config>; boost::date_time::counted_time_system<time_rep>::time_duration_type = boost::posix_time::time_duration]':
/usr/include/boost/date_time/time.hpp:161:64:   required from 'boost::date_time::base_time<T, time_system>::time_type boost::date_time::base_time<T, time_system>::operator+(const time_duration_type&) const [with T = boost::posix_time::ptime; time_system = boost::date_time::counted_time_system<boost::date_time::counted_time_rep<boost::posix_time::millisec_posix_time_system_config> >; boost::date_time::base_time<T, time_system>::time_type = boost::posix_time::ptime; boost::date_time::base_time<T, time_system>::time_duration_type = boost::posix_time::time_duration]'
/usr/include/boost/date_time/posix_time/conversion.hpp:30:48:   required from here
torcontrol.cpp:665:20: warning: shadowed declaration is here [-Wshadow]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that doesnt really answer my question, thanks gcc...Is that trying to say that boost has some "base" object that we're shadowing? Either way lets make the new gBase static.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I do not see any base there 8)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, so we're at renaming random variables now without even understanding why just to shut up warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are overreacting. The problem is elsewhere, of course. The original author is using base all over the source code and arguments of methods conflicts with the global (now static). I think that renaming the global and making it static is the best solution here. Yes, discovered because of turning on -Wshadow.

@laanwj
Copy link
Member

laanwj commented Mar 8, 2017

To be honest I'd prefer to disable -Wshadow again. Even a few days after enabling it, it seems to me it requires too many disjointed, seemingly random changes, and continuous maintenance after almost every thing that gets merged after new incomprehensible warnings pop up with some gcc or clang version.

@maflcko
Copy link
Member

maflcko commented Mar 8, 2017 via email

@laanwj
Copy link
Member

laanwj commented Mar 8, 2017

We might keep it enabled, after all it could help to detect valid bugs.

It could, sure. In an ideal situation where compilers had a single, sane (or even close) definition of shadowing I'd be all for it.
However I just searched and there are >25 recent pulls already dealing with Wshadow changes: https://github.com/bitcoin/bitcoin/pulls?utf8=%E2%9C%93&q=is%3Apr%20is%3Aclosed%20wshadow
This is becoming a bit too much.
And these kind of changes could also result in bugs when applied carelessly, e.g. forgetting to check binaries for equality on some platform.

Also changes in boost or other dependencies could suddenly trigger these in our code. E.g. on the version of boost on FreeBSD I see various warnings inside boost too.

@laanwj
Copy link
Member

laanwj commented Mar 9, 2017

@gmaxwell noted that there are also sub-options for Wshadow. Maybe we can reduce the false positives/weird cases this way.

  -Wshadow-local which warns if a local variable shadows another local
  variable or parameter,

  -Wshadow-compatible-local which warns if a local variable shadows
  another local variable or parameter whose type is compatible with that
  of the shadowing variable.

@paveljanik
Copy link
Contributor Author

paveljanik commented Mar 9, 2017

We had some false positive/weird case?

The base case is a gcc bug (edit: 4.8.x only; fixed in gcc 5). It correctly identified shadowing but emitted wrong log lines...

@paveljanik paveljanik force-pushed the 20170303_Wshadow_streams branch from 5396531 to 8de5159 Compare March 9, 2017 07:41
@paveljanik
Copy link
Contributor Author

Rebased.

@paveljanik
Copy link
Contributor Author

@laanwj Can you please upload your FreeBSD build log for me with V=1 so I can investigate your warnings?

@paveljanik paveljanik force-pushed the 20170303_Wshadow_streams branch from 8de5159 to 0cebfcf Compare March 15, 2017 14:40
@paveljanik
Copy link
Contributor Author

Added fix for #9497's shadowing issues:

test/checkqueue_tests.cpp:49:23: warning: declaration shadows a field of 'checkqueue_tests::FailingCheck' [-Wshadow]
    FailingCheck(bool fails) : fails(fails){};
                      ^
test/checkqueue_tests.cpp:48:10: note: previous declaration is here
    bool fails;
         ^
test/checkqueue_tests.cpp:414:50: warning: declaration shadows a local variable [-Wshadow]
                    std::unique_lock<std::mutex> l(m);
                                                 ^
test/checkqueue_tests.cpp:411:42: note: previous declaration is here
            std::unique_lock<std::mutex> l(m);
                                         ^
2 warnings generated.

@paveljanik paveljanik force-pushed the 20170303_Wshadow_streams branch from 83fedab to d7f80b6 Compare March 18, 2017 07:00
@laanwj
Copy link
Member

laanwj commented Mar 18, 2017

@paveljanik thanks, going to re-check.

There are two sequential uses, there is no loop in the second part and no nested loop at all.

You are right, it's just that in the interest of reducing friction (efficient checking) and eliminating the chance of accidental bugs introduced in these pulls we should require identical binaries.

@laanwj laanwj merged commit d7f80b6 into bitcoin:master Mar 18, 2017
laanwj added a commit that referenced this pull request Mar 18, 2017
d7f80b6 Rename first iterator to prevent shadowing. (Pavel Janík)
b42ff60 Fix shadowing of local variables. (Pavel Janík)
c4b60b3 Make some global variables less-global (static) (Pavel Janík)
bb2aaee Prevent -Wshadow warnings with gcc versions 4.8.5, 5.3.1 and 6.2.1. (Pavel Janík)

Tree-SHA512: 3aea4e28146c8f2a31351c6e2b0cce88b6f1e567a0ea0e6131624453e7193d0904e30d81b1439d8c69e281cf0e369b895851fb882ae48d5967b5c2e2c227404e
laanwj added a commit to laanwj/bitcoin that referenced this pull request Apr 1, 2017
This warning was enabled by default in bitcoin#8808 but it's a
[continuing](bitcoin#9911 (comment))
[source](bitcoin#10089 (comment)) of
[annoyance](bitcoin#9911 (comment)) for me
and other developers. I'm sick of sounding like a broken record, so disable it again.
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request May 6, 2019
d7f80b6 Rename first iterator to prevent shadowing. (Pavel Janík)
b42ff60 Fix shadowing of local variables. (Pavel Janík)
c4b60b3 Make some global variables less-global (static) (Pavel Janík)
bb2aaee Prevent -Wshadow warnings with gcc versions 4.8.5, 5.3.1 and 6.2.1. (Pavel Janík)

Tree-SHA512: 3aea4e28146c8f2a31351c6e2b0cce88b6f1e567a0ea0e6131624453e7193d0904e30d81b1439d8c69e281cf0e369b895851fb882ae48d5967b5c2e2c227404e
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants