This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Introduce the CheckHeaderFile option to modernize-deprecated-headers
ClosedPublic

Authored by steakhal on May 17 2022, 6:01 AM.

Details

Summary

Unfortunately, we must restrict the checker to warn for deprecated headers
only if the header is included directly from a c++ source file.

For header files, we cannot know if the project has a C source file
that also directly/indirectly includes the offending header file
otherwise. Thus, it's better to be on the safe side and suppress those
reports.

One can opt-in the old behavior, emitting diagnostics into header files,
if one explicitly sets the CheckHeaderFile=true, in which case nothing
will be changed.

Diff Detail

Event Timeline

steakhal created this revision.May 17 2022, 6:01 AM
steakhal requested review of this revision.May 17 2022, 6:01 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2022, 6:01 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h
57
  1. How is this different from the clang-tidy option that specifies whether or not fixits are applied to header files?

    As an owner of a code base, I would know which header files are included from C source files and I would set my header-file regex (honestly, not a fan of a regex for that option; I'd prefer white/black lists, but that's another discussion) to exclude header files that are known to be included in C source files.
  1. Assuming that the header-file regex is somehow insufficient to cover this scenario, I like the functionality but the name of this option feels "off". (Naming things is hard.) Elsewhere we have options that say HeaderFile not Headers and Into just doesn't sound like the way normal conversation would state the situation. Something like CheckHeaderFile would be more consistent with existing options.
clang-tools-extra/test/clang-tidy/checkers/modernize-deprecated-headers-extern-c.cpp
2–4

IMO, all of this hackery is simply ducking the issue, which is that check_clang_tidy.py doesn't have proper support for validating fixits applied to header files.

See https://reviews.llvm.org/D17482

whisperity added inline comments.May 18 2022, 2:22 AM
clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h
57

I do not know the answer to question 1, but as an owner of a code-base, I would not want to know or deal with keeping additional lists (be it file-by-file, glob expressions, or regex...) specific to what headers are includable from C and what aren't. Especially considering that every C header could be included from C++. (Note how LLVM uses .h instead of .hpp for its headers, even though >90% of our headers would never compile in C mode...) If a check is misbehaving for my codebase, I'd just simply disable that check (1 line of code) and go on with my life, instead of creating a curated list, that is at least 2 LoC in a config file, and has to be maintained down the line...

Moreover, the -header-filter regex and -system-headers are Tidy-level global flags. This means that I would either have to let go of EVERY potential diagnostic that might be placed in headers, or tinker with my environment to run multiple instances of Clang-Tidy with different configurations.

steakhal updated this revision to Diff 430303.May 18 2022, 2:49 AM
steakhal retitled this revision from [clang-tidy] Introduce the WarnIntoHeaders option to modernize-deprecated-headers to [clang-tidy] Introduce the CheckHeaderFile option to modernize-deprecated-headers.
steakhal edited the summary of this revision. (Show Details)
  • Renamed WarnIntoHeaders -> CheckHeaderFile
  • I forgot to extend the check's docs about this option. I've addressed that one too.
steakhal added inline comments.May 18 2022, 2:51 AM
clang-tools-extra/test/clang-tidy/checkers/modernize-deprecated-headers-extern-c.cpp
2–4

I agree that this is nasty, but this was the only way I found to still test the feature I'm about to introduce.

clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h
57

Those are all good points. One of the weakest aspects of clang-tidy is the selection of files that should be analyzed and/or fixed. Otherwise, we wouldn't need clang-tidy/tool/run-clang-tidy.py at all.

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

Capitalize to C++

clang-tools-extra/test/clang-tidy/checkers/modernize-deprecated-headers-extern-c.cpp
2–4

Agreed

LGTM
Address one nit and ship.

This revision is now accepted and ready to land.May 20 2022, 10:12 AM
This revision was landed with ongoing or failed builds.May 20 2022, 12:44 PM
This revision was automatically updated to reflect the committed changes.
steakhal marked an inline comment as done.