This is an archive of the discontinued LLVM Phabricator instance.

[NFC][CLANG][API] Fix coverity remarks about large copies by values
AbandonedPublic

Authored by Manna on Apr 9 2023, 5:47 PM.

Details

Summary

Reported by Coverity Static Analyzer Tool:

Big parameter passed by value (PASS_BY_VALUE)
Copying large values is inefficient, consider passing by reference; Low, medium, and high size thresholds for detection can be adjusted.

  1. pass_by_value: Passing parameter Availabilities of type clang::extractapi::AvailabilitySet (size 320 bytes) by value, which exceeds the medium threshold of 256 bytes in "API.cpp" and "API.h" files.

This patch passes parameter as const AvailabilitySet &Availabilities instead of AvailabilitySet Availabilities.

  1. Inside "APIIgnoresList.h" file, in clang::​extractapi::​APIIgnoresList::​APIIgnoresList(llvm::​SmallVector<llvm::​StringRef, 32u>, llvm::​SmallVector<std::​unique_ptr<llvm::​MemoryBuffer, std::​default_delete<llvm::​MemoryBuffer>>, 13u>): A large function call parameter exceeding the medium threshold is passed by value.

pass_by_value: Passing parameter SymbolsToIgnore of type clang::extractapi::APIIgnoresList::SymbolNameList (size 268 bytes) by value, which exceeds the medium threshold of 256 bytes.

This patch passes parameter as const SymbolNameList &SymbolsToIgnore instead of SymbolNameList SymbolsToIgnore.

Diff Detail

Event Timeline

Manna created this revision.Apr 9 2023, 5:47 PM
Herald added a project: Restricted Project. · View Herald Transcript
Manna requested review of this revision.Apr 9 2023, 5:47 PM
Herald added a project: Restricted Project. · View Herald Transcript
aaron.ballman added inline comments.Apr 10 2023, 6:14 AM
clang/include/clang/ExtractAPI/API.h
138

A lot of these changes look to be regressions, so I think Coverity is incorrect to flag these. The old code is passing an AvailabilitySet by value because it's doing a move operation on initialization: Availabilities(std::move(Availabilities)). Making this into a const reference defeats that optimization because you can't steal resources from a const object (so this turns a move into a copy).

You should look through the rest of the patch for similar problematic changes.

RKSimon added inline comments.
clang/include/clang/ExtractAPI/API.h
138

I've never been sure whether coverity is being particularly poor at recognising the std::move pattern or particularly smart at realising it can't occur for some reason.

dang added inline comments.Apr 11 2023, 4:46 AM
clang/include/clang/ExtractAPI/API.h
138

Yeah this certainly doesn't look right, since the new version tries to move from the const reference.

Manna added a comment.Apr 18 2023, 5:42 PM

Thank you everyone for reviews and feedback.

Manna abandoned this revision.Apr 18 2023, 5:43 PM