This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix `TransformerClangTidyCheck`'s handling of include insertions.
ClosedPublic

Authored by ymandel on Feb 11 2021, 1:33 PM.

Details

Summary

Currently, all include insertions are directed to the main file. However,
Transformer rules can specify alternative destinations for include
insertions. This patch fixes the code to associate the include with the correct
file.

This patch was tested manually. The clang tidy unit test framework does not
support testing changes to header files. Given that this is a bug fix for a live
bug, the patch relies on manual testing rather than blocking on upgrading the
unit test framework.

Diff Detail

Event Timeline

ymandel created this revision.Feb 11 2021, 1:33 PM
ymandel requested review of this revision.Feb 11 2021, 1:33 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 11 2021, 1:33 PM
Eugene.Zelenko added a project: Restricted Project.
hokein accepted this revision.Feb 12 2021, 4:17 AM

Thanks, the fix looks good.

This patch was tested manually. The clang tidy unit test framework does not
support testing changes to header files. Given that this is a bug fix for a live
bug, the patch relies on manual testing rather than blocking on upgrading the
unit test framework.

If extending the test framework is too much work, I think we can write a lit test for a transformer-based check e.g. abseil-string-find-str-contains: setup an include header, and verify the include is inserted in the header.

This revision is now accepted and ready to land.Feb 12 2021, 4:17 AM
alexfh added inline comments.Feb 13 2021, 4:58 PM
clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
108

Can this be a macro file id? I'd suggest to add tests (probably for checks using this functionality) with a few nested includes and fixes in normal code, code in macros declared and expanded in different files, locations in macro bodies, macro arguments, and some tricky cases like fix pointing to a pasted token.

ymandel added inline comments.Feb 18 2021, 7:02 AM
clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
108

Can this be a macro file id? I'd suggest to add tests (probably for checks using this functionality) with a few nested includes and fixes in normal code, code in macros declared and expanded in different files, locations in macro bodies, macro arguments, and some tricky cases like fix pointing to a pasted token.

Unlikely, given that API that populates the location. Most use a default verion which explicitly maps the location to the expansion location. However, it is possible. Also, Transformer rejects changes inside macros (except arguments) so that would also be hard to engineer.

Still, I'll look into better tests in the check itself. I did try that but mostly failed because the lit tests don't support checking anything in headers. However, the macro checks sound worth it.