This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Introduce HeaderFileExtensions and ImplementationFileExtensions options
ClosedPublic

Authored by carlosgalvezp on Jan 26 2023, 11:13 AM.

Details

Summary

Re-introduce the patch that was reverted previously.
In the first attempt, the checks would not be able to
read from the global option, since getLocalOrGlobal
only works with string types. Additional logic is needed
in order to support both use cases in the transition
period. All that logic will be removed when the local
options are fully removed.

We have a number of checks designed to analyze problems
in header files only, for example:

bugprone-suspicious-include
google-build-namespaces
llvm-header-guard
misc-definitions-in-header
...

All these checks duplicate the same logic and options
to determine whether a location is placed in the main
source file or in the header. More checks are coming
up with similar requirements.

Thus, to remove duplication, let's move this option
to the top-level configuration of clang-tidy (since
it's something all checks should share).

Add a deprecation notice for all checks that use the
local option, prompting to update to the global option.

Diff Detail

Event Timeline

carlosgalvezp created this revision.Jan 26 2023, 11:13 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.Jan 26 2023, 11:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 26 2023, 11:13 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
carlosgalvezp retitled this revision from [clang-tidy] Introduce HeaderFileExtensions and ImplementationFileExtensions options to [WIP][clang-tidy] Introduce HeaderFileExtensions and ImplementationFileExtensions options.

Mark it as WIP to clarify it's not ready for review.

Add functions to ClangTidyCheck so checks can get
access to them.

carlosgalvezp added inline comments.Jan 26 2023, 1:35 PM
clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
233–234

@njames93 Would appreciate feedback on if putting these here sounds reasonable.

clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp
32–43

This is messy, but only needs to be kept during the transition period until the local option is fully removed. After that, checks simply need to call the function getHeaderFileExtensions() that is available via ClangTidyCheck where needed. Therefore, no more private local variables needed to handle this.

PiotrZSL added inline comments.
clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
149

what about .c++ ? like file.c++ for implementation and h++ for header.
Its listed on https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/File_Organization

I know that probably no-one uses them, but who knows.

PiotrZSL removed a subscriber: PiotrZSL.Jan 26 2023, 1:56 PM
carlosgalvezp added inline comments.Jan 26 2023, 11:48 PM
clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
149

First time I've seen it! If someone uses them they can add it in their config file, which is the purpose of this option :)

Anyhow the scope of this patch is not add new functionality, simply move the configuration from local to global options. I plan to add more default extensions (cuda, objc) in a follow up patch.

  • Apply fix to remaining checks.
  • Fix documentation and release notes.
carlosgalvezp retitled this revision from [WIP][clang-tidy] Introduce HeaderFileExtensions and ImplementationFileExtensions options to [clang-tidy] Introduce HeaderFileExtensions and ImplementationFileExtensions options.
carlosgalvezp edited the summary of this revision. (Show Details)

Update commit message

Use get functions from ClangTidyCheck instead of Context

carlosgalvezp edited the summary of this revision. (Show Details)

Update commit message to clarify the difference
between the first attempt and this one.

Remove unneeded newlines and braces in single-line
if/else statements.

carlosgalvezp edited the summary of this revision. (Show Details)

Update commit message

Fix release notes, ImplementationFileExtensions only
applies to one check.

Eugene.Zelenko added inline comments.Feb 5 2023, 7:18 AM
clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp
31

Will be good idea to make function in utils instead of code duplication.

carlosgalvezp added inline comments.Feb 5 2023, 9:03 AM
clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp
31

I thought about that, but found it quite difficult due to the dependencies to Options and diag(), which are only available in ClangTidyCheck. This is probably why there was duplication to begin with.

I could add a helper function in ClangTidyCheck, but I find it out of place and I see risk in people using it as if it were a solid function meant to stay. Since this code is temporary I think the duplication is a small price to pay for keeping backwards compatibility. It will all be gone when we remove the local options.

I might have missed something, if you have some concrete suggestion that could work I'm happy to use it!

Eugene.Zelenko accepted this revision.Feb 5 2023, 9:11 AM

Looks OK for me, but please wait for other opinion(s).

This revision is now accepted and ready to land.Feb 5 2023, 9:11 AM

Document options now that the documentation improvement
patch is merged.

Friendly ping @njames93 . Since there are more checks coming up introducing more debt and duplication, I believe we should land this rather soon. I intend to land this by Feb 19th if I don't receive any more feedback.

  • Rebase.
  • Fix code directly for the newly introduce check in llvmlibc. We do not need the deprecation process here since the check is brand new.
  • Rebase the getter functions from ClangTidyCheck.h, they don't seem to belong. Solve it by having each check that needs it store a copy of the file extensions, fetching it from the Context in the constructor of the check.