This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add misc-static-declaration-in-header check
Needs ReviewPublic

Authored by carlosgalvezp on Dec 19 2022, 3:58 AM.

Details

Summary

Static declarations in header files is considered bad practice,
since header files are meant for sharing code, and static
leads to internal linkage. This can cause code bloat and lead
to subtle issues, for example ODR violations. Essentially, this
is as problematic as having anonymous namespaces in headers,
which we already have checks for.

Diff Detail

Event Timeline

carlosgalvezp created this revision.Dec 19 2022, 3:58 AM
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: xazax.hun. · View Herald Transcript
carlosgalvezp requested review of this revision.Dec 19 2022, 3:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 19 2022, 3:58 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Eugene.Zelenko added inline comments.Dec 19 2022, 7:37 AM
clang-tools-extra/clang-tidy/misc/StaticDeclarationInHeaderCheck.cpp
47

Look like this setting should be global for Clang-tidy, because it already used in other checks. It also accompanied by other option.

clang-tools-extra/docs/ReleaseNotes.rst
110

Please add first sentence from documentation.

clang-tools-extra/docs/clang-tidy/checks/misc/static-declaration-in-header.rst
40

Please replace quotes with single back-ticks.

carlosgalvezp added inline comments.Dec 19 2022, 8:05 AM
clang-tools-extra/clang-tidy/misc/StaticDeclarationInHeaderCheck.cpp
47

Fully agree, I can put up a separate patch for that. I suppose we need to wait 2 releases to be able to remove the existing duplicated options from the checks?

@njames93 Do you agree?

carlosgalvezp marked an inline comment as done.

Add description in release notes.

clang-tools-extra/docs/clang-tidy/checks/misc/static-declaration-in-header.rst
40

There's 3 other such instances of this pattern:

  • 1 of them has: `";...."` (double back tick followed by quotes).
  • 2 of them have it like I wrote it here.

None of them follow the format you propose, so I'd be reluctant to introduce a third convention :) Would it be OK to keep the most prominent format for consistency, and fix the formatting for all in a separate patch?

carlosgalvezp added inline comments.Dec 19 2022, 9:10 AM
clang-tools-extra/docs/clang-tidy/checks/misc/static-declaration-in-header.rst
40

Sorry, there's 2, not 3 (the third one is this check :) )

Eugene.Zelenko added inline comments.Dec 19 2022, 9:19 AM
clang-tools-extra/docs/clang-tidy/checks/misc/static-declaration-in-header.rst
40

Single back-ticks are used for option values. Will be good idea to fix other places too.

Replace quotes with single backticks.

carlosgalvezp marked 2 inline comments as done.Dec 19 2022, 10:25 AM

Warn only about cases where there really is a problem.
There are other cases which are just a readability issue,
where a separate check for "redundant static" would fit
better. Remove also the automatic fix-it since it leads
to potentially broken code.

Remove remaining function declaration in the check class.

carlosgalvezp marked an inline comment as done.Dec 26 2022, 11:50 PM

Narrow warnings further, and provide fix-its.

I'm happy with the check now, feel free to review! I have run it on the LLVM repo and it seems to do what it's supposed to (including fix-its).

lebedev.ri resigned from this revision.Jan 12 2023, 5:34 PM

Friendly ping. I realized @aaron.ballman you probably have very good insights into this use case, since (AFAICT) you wrote a guideline about anonymous namespaces in headers, which is very closely related to this:
https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL59-CPP.+Do+not+define+an+unnamed+namespace+in+a+header+file

Would appreciate your feedback!