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 shadowing warning #12960

Closed
wants to merge 1 commit into from
Closed

Fix shadowing warning #12960

wants to merge 1 commit into from

Conversation

Rombur
Copy link
Member

@Rombur Rombur commented Nov 16, 2021

With #12940, we correctly import Kokkos flags including -Wshadow. We have a lot of these warnings and this PR fixes some of them. The majority of the shadowing happens in constructors with code like

foo::foo(int bar)
: 
bar(bar)
{}

but I also found a few places where we declare and recompute the same variable several times. Given the extremely large number of warnings I didn't try to be clever when changing the name of the variable. If the shadowing happens in the constructor, I added an _ in the name of the variable. If the shadowing happens in another function then I tried to find a good name for the new variable.

@peterrum
Copy link
Member

Do need import this flag? I personally like the style bar(bar) and would like to keep as an option to the developer.

@masterleinad
Copy link
Member

Do we really export any warnings flags if Kokkos is compiled with KOKKOS_ENABLE_COMPILER_WARNINGS=OFF?

@Rombur
Copy link
Member Author

Rombur commented Nov 17, 2021

Do we need to import this flag? I personally like the style bar(bar) and would like to keep as an option to the developer.

I also like it but for the constructor but there are still many places where there is a real shadowing. Sometimes we declare the same variables multiple times in the same function with different scopes and we often uses the same name for an input parameter and a member variable and that's a lot more confusing.

@Rombur
Copy link
Member Author

Rombur commented Nov 17, 2021

Do we really export any warnings flags if Kokkos is compiled with KOKKOS_ENABLE_COMPILER_WARNINGS=OFF?

No we don't. I am so used to turn it on that I didn't pay attention. I guess we don't need this PR but it is still useful. I found a bug in MatrixFree because of it

  for (const auto dh : dof_handler)
-    dof_handlers.push_back(dof_handler);
+    dof_handler_vec.push_back(dh);

and there was another place in MatrixFree where we recomputed the same variables several times in different scope...

@masterleinad
Copy link
Member

I guess we don't need this PR but it is still useful.

I would be in favor of adding a less restrictive version. I would think that -Wshadow=compatible-local also catches the relevant bugs but also allows the bar(bar) initialization syntax, see https://godbolt.org/z/Mq6T51W5d.

@masterleinad
Copy link
Member

Also, it seems that clang is not complaining about this constructor-style with -Wshadow (but only with -Wshadow-field-in-constructor).

@Rombur
Copy link
Member Author

Rombur commented Nov 17, 2021

With -Wshadow=compatible-local, you won't get a warning for this:

struct Foo{
  Foo(int i) : i(i){}
  int i;

  void bar(int i) { i = 2; }
};

which we do.
If the consensus is that we really don't want to change the constructor that's fine with me but we will still be left with many places where the code is confusing.

@masterleinad
Copy link
Member

masterleinad commented Nov 17, 2021

With -Wshadow=compatible-local, you won't get a warning for this:

But you get a warning with clang and -Wshadow. I would be game with just enabling the flag with clang (or at least exploring how painful that is).

@Rombur Rombur closed this Nov 17, 2021
@bangerth
Copy link
Member

For the real bugs you found, would you be willing to extract that part of the patch at least and submit separately?

I don't have a strong feeling one way or the other about the rest, but the bugs you found we should fix :-)

@Rombur
Copy link
Member Author

Rombur commented Nov 17, 2021

@bangerth Yes, of course. That was my plan :)

@Rombur Rombur deleted the shadow_pr1 branch May 25, 2023 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants