llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Add details about the storage like variable name, stack/temporary

Open aeft opened this issue 8 months ago • 4 comments

For example, it helps the following case:

// TODO: Include the name of the variable in the diagnostic to improve
// clarity, especially for implicit lambda captures where multiple warnings
// can point to the same source location.
auto implicit_ref_capture_multiple() {
  int local = 1, local2 = 2;
  auto lambda = [&]() { return local + local2; }; // expected-warning 2 {{address of stack memory is returned later}}
  return lambda; // expected-note 2 {{returned here}}
}

It can be similar to the pre-existing analysis, such as -Wreturn-stack-address. For example: https://godbolt.org/z/ecb4dMYKG

<source>:28:13: warning: address of stack memory associated with local variable 'integer' returned [-Wreturn-stack-address]
   28 |     return [&]() { Foo(integer, ptr, str, view); };
      |             ^
<source>:28:24: note: implicitly captured by reference due to use here
   28 |     return [&]() { Foo(integer, ptr, str, view); };
      |             ~          ^

cc @usx95

aeft avatar Mar 11 '26 23:03 aeft

@llvm/issue-subscribers-clang-temporal-safety

Author: Zhijie Wang (aeft)

For example, it helps the following case: ```cpp // TODO: Include the name of the variable in the diagnostic to improve // clarity, especially for implicit lambda captures where multiple warnings // can point to the same source location. auto implicit_ref_capture_multiple() { int local = 1, local2 = 2; auto lambda = [&]() { return local + local2; }; // expected-warning 2 {{address of stack memory is returned later}} return lambda; // expected-note 2 {{returned here}} } ```

It can be similar to the pre-existing analysis, such as -Wreturn-stack-address. For example: https://godbolt.org/z/ecb4dMYKG

&lt;source&gt;:28:13: warning: address of stack memory associated with local variable 'integer' returned [-Wreturn-stack-address]
   28 |     return [&amp;]() { Foo(integer, ptr, str, view); };
      |             ^
&lt;source&gt;:28:24: note: implicitly captured by reference due to use here
   28 |     return [&amp;]() { Foo(integer, ptr, str, view); };
      |             ~          ^

cc @usx95

llvmbot avatar Mar 11 '26 23:03 llvmbot

I would like to work on this.

NeKon69 avatar May 22 '26 18:05 NeKon69

This is now fixed:

<source>:28:13: warning: stack memory associated with local variable 'ptr' is returned [-Wlifetime-safety-return-stack-addr]
   28 |     return [&]() { Foo(integer, ptr, str, view); }; 
      |             ^
<source>:28:12: note: returned here
   28 |     return [&]() { Foo(integer, ptr, str, view); }; 
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:28:13: warning: stack memory associated with local variable 'str' is returned [-Wlifetime-safety-return-stack-addr]
   28 |     return [&]() { Foo(integer, ptr, str, view); }; 
      |             ^
<source>:28:12: note: returned here
   28 |     return [&]() { Foo(integer, ptr, str, view); }; 
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:28:13: warning: stack memory associated with local variable 'integer' is returned [-Wlifetime-safety-return-stack-addr]
   28 |     return [&]() { Foo(integer, ptr, str, view); }; 
      |             ^
<source>:28:12: note: returned here
   28 |     return [&]() { Foo(integer, ptr, str, view); }; 
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:28:13: warning: stack memory associated with local variable 'view' is returned [-Wlifetime-safety-return-stack-addr]
   28 |     return [&]() { Foo(integer, ptr, str, view); }; 
      |             ^
<source>:28:12: note: returned here
   28 |     return [&]() { Foo(integer, ptr, str, view); }; 

Similarly for capture by value, https://godbolt.org/z/xvqzMje4E:

auto CreateCallback() {
    int integer;
    int* ptr;
    std::string str;
    std::string_view view;
    ptr = &integer;
    view = str;
    return [=]() { Foo(integer, ptr, str, view); }; 
}

<source>:16:12: warning: stack memory associated with local variable 'integer' is returned [-Wlifetime-safety-return-stack-addr]
   16 |     ptr = &integer;
      |            ^~~~~~~
<source>:18:12: note: returned here
   18 |     return [=]() { Foo(integer, ptr, str, view); }; 
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:17:12: warning: stack memory associated with local variable 'str' is returned [-Wlifetime-safety-return-stack-addr]
   17 |     view = str;
      |            ^~~
<source>:18:12: note: returned here
   18 |     return [=]() { Foo(integer, ptr, str, view); }; 
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

usx95 avatar May 28 '26 17:05 usx95

Same for temporary. https://godbolt.org/z/qxs6r6jx9

std::string_view get(const std::string& str [[clang::lifetimebound]]) {
    return str;
}

std::string_view foo() {
    std::string str;
    return get(std::string("temp"));
}

<source>:12:16: warning: stack memory associated with local temporary object is returned [-Wlifetime-safety-return-stack-addr]
   12 |     return get(std::string("temp"));
      |                ^~~~~~~~~~~~~~~~~~~
<source>:12:12: note: returned here
   12 |     return get(std::string("temp"));
      |            ^~~~~~~~~~~~~~~~~~~~~~~~

usx95 avatar May 28 '26 17:05 usx95