This is an archive of the discontinued LLVM Phabricator instance.

Fix `performance-unnecessary-value-param` for template specialization
ClosedPublic

Authored by Sockke on Jan 4 2022, 4:29 AM.

Details

Summary

The checker missed a check for parameter type of primary template of specialization template and this could cause build breakages.

Diff Detail

Event Timeline

Sockke created this revision.Jan 4 2022, 4:29 AM
Sockke requested review of this revision.Jan 4 2022, 4:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2022, 4:29 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
flx added a comment.Jan 4 2022, 6:44 AM

Thanks for improving this check!

clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
147–148

Could you add a comment here why we're skipping the fix here?

clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-value-param.cpp
388

Should we apply the fixes or just issue the warning? For virtual methods we suppress the fix since we can't necessarily update all overrides of the method. Are template specializations always guaranteed to be in the same translation unit which would make this safe?

MTC added a subscriber: MTC.Jan 4 2022, 6:10 PM
Sockke added a comment.Jan 4 2022, 7:44 PM

Thanks for your review!

clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
147–148

Could you add a comment here why we're skipping the fix here?

Specialization template may match the primary template again by getPreviousDecl. Skipping the fix to avoid repeated fixes for the primary template.

clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-value-param.cpp
388

Should we apply the fixes or just issue the warning? For virtual methods we suppress the fix since we can't necessarily update all overrides of the method. Are template specializations always guaranteed to be in the same translation unit which would make this safe?

Do you mean that specialization templates are defined in different translation units? If fixing one by one translation unit does have the problem, the readability-const-return-type also has such a problem. clang-tidy can not analyze across translation units, but the diagnosis and fix of it are separate, we can specify the complete compile_commands.json to avoid it.
I'm not sure whether this is reasonable, we may make a choice between clang-tidy's fault tolerance and advantages. What's your suggestion?

Friendly ping.

flx added inline comments.Jan 14 2022, 7:10 AM
clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
147–148

Thanks for the explanation. Would you mind adding this as code comment?

clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-value-param.cpp
388

Should we apply the fixes or just issue the warning? For virtual methods we suppress the fix since we can't necessarily update all overrides of the method. Are template specializations always guaranteed to be in the same translation unit which would make this safe?

Do you mean that specialization templates are defined in different translation units? If fixing one by one translation unit does have the problem, the readability-const-return-type also has such a problem. clang-tidy can not analyze across translation units, but the diagnosis and fix of it are separate, we can specify the complete compile_commands.json to avoid it.

I'm not sure how I understand how the compile command json file would help. I think std::hash (https://en.cppreference.com/w/cpp/utility/hash) is an example where you would specialize a type across translation units. If std::hash itself had the issue you wouldn't be able to modify it since it is an external library.

I'm not sure whether this is reasonable, we may make a choice between clang-tidy's fault tolerance and advantages. What's your suggestion?

Yeah, this is not an easy balance to strike. ClangTidy is used in many different contexts, in some where you would like to see aggressive fixes, in others where only the safest fixes should be applied.

From previous reviews and my own experience we should by default only provide safe fixes with low false positive rate. If you think its' not enough to only notify the user of an inefficiency in template specializations you could add an option to the check for fixing these occurrences.

This was done for example here to make a check find issues (including false positives) and report them: https://github.com/llvm/llvm-project/blob/765dd8b8a44cd9689c87c0433739f421b9871061/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp#L26

In summary, I would make this a warning only, but not issue the fix here.

Sockke updated this revision to Diff 411336.Feb 25 2022, 12:17 AM

Removed the fix for the template.

It looks like precommit CI is failing:

Failed Tests (1):

Clang Tools :: clang-tidy/checkers/performance-unnecessary-value-param.cpp
Sockke updated this revision to Diff 411729.EditedFeb 27 2022, 7:49 PM

Added a release note.

flx added a comment.Mar 1 2022, 9:54 AM

I'm still seeing build failures. Could you resolve them?

Sockke updated this revision to Diff 412334.Mar 2 2022, 12:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2022, 12:29 AM
Sockke updated this revision to Diff 412340.Mar 2 2022, 12:47 AM
flx accepted this revision.Mar 8 2022, 7:16 AM
This revision is now accepted and ready to land.Mar 8 2022, 7:16 AM
Sockke updated this revision to Diff 430621.May 19 2022, 3:06 AM

rebased.

Sockke added a comment.EditedMay 19 2022, 5:09 AM

I'm not quite sure why the test case is not passing on Windows, could you please check this problem? Thanks for your assistance. @flx

flx added a comment.May 20 2022, 5:55 PM

I don't have access to Windows, perhaps rebase and try again? It looks like the template specialization related tests are failing there though.

aaron.ballman added inline comments.May 24 2022, 6:51 AM
clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-value-param.cpp
1

This will fix your Windows testing issue, I believe.

Sockke updated this revision to Diff 432520.May 27 2022, 4:18 AM

Rebased and added '-fno-delayed-template-parsing' option for the test file.

aaron.ballman accepted this revision.May 28 2022, 4:55 AM

LGTM aside from a very minor nit that can be corrected when landing.

clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
142

No need for the extra parens.

steakhal added inline comments.
clang-tools-extra/docs/ReleaseNotes.rst
119–122

This is not at the expected alphabetical place.