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

[RFC] cleanup of frequent shadow warnings #44855

Conversation

slava77
Copy link
Contributor

@slava77 slava77 commented Apr 26, 2024

While compiling with -Wshadow for a test I decided to cleanup some of the more frequent cases exposed mainly by being present in interface/.
Perhaps this can be a step towards enabling this warning in our builds.

One curious case was the use of CLHEP/Units/GlobalSystemOfUnits.h, which puts a few singe character (m, s, g) and other short-name symbols in the global scope. The scoped SystemOfUnits.h seems to be more appropriate for a large sw project.

The total count of warnings goes down from 48.6K to 14K.

@cmsbuild
Copy link
Contributor

cmsbuild commented Apr 26, 2024

cms-bot internal usage

@cmsbuild
Copy link
Contributor

-code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-44855/40105

  • This PR adds an extra 164KB to repository

Code check has found code style and quality issues which could be resolved by applying following patch(s)

@cmsbuild
Copy link
Contributor

+code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-44855/40106

  • This PR adds an extra 236KB to repository

@slava77
Copy link
Contributor Author

slava77 commented Apr 27, 2024

@slava77 , yes, itis "" --><>. an ideal solution for CLHEP headers is do not use inside header files except the situation when it is unavoidable. In such case to use <CLHEP/Units/SystemOfUnits.h> is the recommendation.

thanks for clarifying; I updated the headers now.
files were selected by
git diff --stat --name-only CMSSW_14_1_0_pre2 | grep h$ | while read -r f; do grep -l \"CLHEP/ $f; done

@cmsbuild
Copy link
Contributor

+code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-44855/40112

@cmsbuild
Copy link
Contributor

@slava77
Copy link
Contributor Author

slava77 commented Apr 27, 2024

@cmsbuild please test

@cmsbuild
Copy link
Contributor

+1

Summary: https://cmssdt.cern.ch/SDT/jenkins-artifacts/pull-request-integration/PR-631d6d/39127/summary.html
COMMIT: 46695cc
CMSSW: CMSSW_14_1_X_2024-04-27-1100/el8_amd64_gcc12
User test area: For local testing, you can use /cvmfs/cms-ci.cern.ch/week0/cms-sw/cmssw/44855/39127/install.sh to create a dev area with all the needed externals and cmssw changes.

Comparison Summary

Summary:

Comment on lines -54 to +58
LaunchParameters(dim3 gridDim, dim3 blockDim, size_t sharedMem = 0, cudaStream_t stream = nullptr)
: gridDim(gridDim), blockDim(blockDim), sharedMem(sharedMem), stream(stream) {}
LaunchParameters(dim3 lgridDim, dim3 lblockDim, size_t lsharedMem = 0, cudaStream_t lstream = nullptr)
: gridDim(lgridDim), blockDim(lblockDim), sharedMem(lsharedMem), stream(lstream) {}

LaunchParameters(int gridDim, int blockDim, size_t sharedMem = 0, cudaStream_t stream = nullptr)
: gridDim(gridDim), blockDim(blockDim), sharedMem(sharedMem), stream(stream) {}
LaunchParameters(int lgridDim, int lblockDim, size_t lsharedMem = 0, cudaStream_t lstream = nullptr)
: gridDim(lgridDim), blockDim(lblockDim), sharedMem(lsharedMem), stream(lstream) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any way to tell the compiler "please stop bothering me about this, I know what I am doing and I do like to keep my variable names like they are, thank you very much ?" - other than disabling the warning altogether ?

It's not like a variable can be constructed from itself.

Copy link
Contributor

Choose a reason for hiding this comment

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

@fwyzard , I am not an expert but why you need gridDim(gridDim) ? From pedestrian point of view, the name of parameters in a class constructor does not matter, what is matter for a code are names of class members. May be worse to check what gcc experts are thinking and why they introduced this warning. They use to add new warnings, which are not obvious to users, but usually have some arguments (may be very formal).

Copy link
Contributor

Choose a reason for hiding this comment

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

The name of any single variable does not matter, the compiled code will happily work in the same way if we call them all aaa, aab, aac, etc.

Of course the code will be very hard to read and follow in that case...

So, yes, we could name the parameters

aunchParameters(dim3 par1, dim3 par2, size_t par3 = 0, cudaStream_t par4 = nullptr)
          : gridDim(par1), blockDim(par2), sharedMem(par3), stream(par4) {}

Then the code would compile, and -Wshadow would not trigger any exception.

Of course the code would be harder to read and maintain - so we give parameters names that make it easier to read and maintain the code.

If you call the parameter lgridDim - what does the l stand for ?

To improve the naming in that class one could

  • rename all member variables from gridDim to gridDim_;
  • add accessors for gridDim_ named gridDim();
  • rewrite the constructor to take a parameter named gridDim and use it to initialise the data member gridDim_.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well LaunchParameters() : gridDim(gridDim) {} will still compile (with a warning about uninitialized variable)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you call the parameter lgridDim - what does the l stand for ?

To improve the naming in that class one could

* rename all member variables from `gridDim` to `gridDim_`;

* add accessors for `gridDim_` named `gridDim()`;

* rewrite the constructor to take a parameter named `gridDim` and use it to initialise the data member `gridDim_`.

sure, l prefix is somewhat random (I have it to mean local).

@fwyzard
Would gridDim_l (or _a for arg) keep the code still readable in your opinion?

The option to rewrite the class to change members to follow CMSSW naming rules is also possible, but would require more effort.

@fwyzard
Copy link
Contributor

fwyzard commented Apr 28, 2024

-1

sorry, but for me these changes are simply making the code worse less readable.

@civanch
Copy link
Contributor

civanch commented Apr 28, 2024

@slava77 , may be it is possible to split PR into two: one for "law and order for CLHEP" and another for heterogenous?

@VinInn
Copy link
Contributor

VinInn commented Apr 28, 2024

"himself" on the subject
https://lkml.org/lkml/2006/11/28/253

@VinInn
Copy link
Contributor

VinInn commented Apr 28, 2024

Anyhow I'm sure you all read this ttps://stackoverflow.com/questions/2958457/gcc-wshadow-is-too-strict
and are aware of the possible options to attach to Wshadow (https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html)
So why "global" has been chosen? would not be better to move to -Wshadow=compatible-local ?

@VinInn
Copy link
Contributor

VinInn commented Apr 28, 2024

Anyhow for this trivial example (that is surely not doing what the naive author intended)

class foo {
foo(int);
int i;
};

foo::foo(int i) : i(i){
    for (int j=0; j<i; ++j)
      i++;
}

-Wshadow=local (or compatible-local) will not issue any warning.
Only -Wshadow(global) will.

So: do we want to warn for such cases or we consider the authors able to avoid armful shadow of global (actually class-member) variables?

@slava77
Copy link
Contributor Author

slava77 commented Apr 28, 2024

@slava77 , may be it is possible to split PR into two: one for "law and order for CLHEP" and another for heterogenous?

yes; this clearly became an RFC PR

@slava77 slava77 changed the title cleanup of frequent shadow warnings [RFC] cleanup of frequent shadow warnings Apr 28, 2024
@slava77
Copy link
Contributor Author

slava77 commented May 10, 2024

@cms-sw/core-l2 please comment if any of these are useful.
Thank you.

@makortel
Copy link
Contributor

@cms-sw/core-l2 please comment if any of these are useful.

Do you mean in the core packages specifically, or in general?

In core packages some of the changes looked useful, some could have been kept as they were (as the scoping was very clear), but I'd rather have the renamed versions than adding #pragma GCC diagnostic.

@slava77
Copy link
Contributor Author

slava77 commented May 23, 2024

it looks like there is no significant interest to have fixes other than in CLHEP (done in #44882 )

@slava77 slava77 closed this May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment