[PATCH] D139122: Generalize clang-tidy modernize-pass-by-value

Martin Bidlingmaier via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 1 08:40:28 PST 2022


mbid created this revision.
Herald added a subscriber: carlosgalvezp.
Herald added a reviewer: njames93.
Herald added a project: All.
mbid added a comment.
mbid published this revision for review.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Hi Nathan,

Could you have a look at whether something like this looks reasonable? The docs of modernize-pass-by-value currently say that a patch that generalizes this check is welcome. I'd polish the docs and add some more test cases before merge if this looks reasonable overall.

- Martin


Previously, the clang-tidy modernize-pass-by-value check would only fire
for arguments of constructors that are used exactly once and in a member
or base class initializers. This commit generalizes the check to general
functions and usages in the body of the function.

We now consider the control-flow graph of the function. If every
control-flow path must transition through one final usage of a given
parameter, we fire the diagnostic. We additionally require that the
variable is not referenced or pointed to by a local variable in the
function. There are still some false-positives, however, because we do
not try to figure out whether a pointer to the local variable escapes
through other means, e.g. here:

  struct HasStrPtr {
    const std::string* str;
  };
  
  void set_ptr(HasStrPtr& has_ptr, const std::string& str) {
    has_ptr.str = &str;
  }
  
  // Take argument by value.
  void foo(std::string);
  
  void foo(const std::string& str) {
    HasStrPtr obj;
    set_ptr(obj, str);
    foo(str);
    foo(*HasStrPtr.str);
  }

The example looks at first sight like we can std::move in the call to
foo, but this would change the value pointed to by the pointer in
HasStrPtr, so that the semantics of the program change.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139122

Files:
  clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
  clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h
  clang-tools-extra/docs/clang-tidy/checks/modernize/pass-by-value.rst
  clang-tools-extra/test/clang-tidy/checkers/modernize/pass-by-value.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139122.479309.patch
Type: text/x-patch
Size: 37808 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221201/34aac00d/attachment-0001.bin>


More information about the cfe-commits mailing list