llvm / llvm/llvm-project

[Clang] `[[clang::lifetimebound]]` should support function parameters without a name

Open
#198,484 4 comments 0 reactions 0 assignees View on GitHub
clang:temporal-safety
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/6M1sK8q6P

```cpp
int* f(int* [[clang::lifetimebound]]);

int* f(int* p) { return p; }
```

```
:1:15: error: 'clang::lifetimebound' attribute only applies to parameters and implicit object parameters
1 | int* f(int* [[clang::lifetimebound]]);
| ^
1 error generated.
Compiler returned: 1
```

The forward declaration may live in a separate file (e.g. public header). Sometimes the type name is self-explanatory (e.g. `auto use_config(const Config&`) and therefore a lifetimebound nameless parameter is useful. This is especially relevant to the lifetime inference in [LifetimeSafety](https://clang.llvm.org/docs/LifetimeSafety.html) analysis: If you have `int* f(int*);` in the public header, the fix-it suggestion would place `lifetimebound` attribute after the nameless parameter which is currently invalid:

https://godbolt.org/z/o1Y8ao7aj

```cpp
// io.h
int* foo(int*);
```

```cpp
// io.cpp
int* foo(int* p) {
return p;
}

```

```
/app/io.h:8:10: warning: parameter in cross-TU function should be marked [[clang::lifetimebound]] [-Wlifetime-safety-cross-tu-suggestions]
8 | int* foo(int*);
| ^~~~
| [[clang::lifetimebound]]
/app/io.cpp:13:12: note: param returned here
13 | return p;
| ^
1 warning generated.
```

After applying the fix-it:

```cpp
/app/io.h:8:17: error: 'clang::lifetimebound' attribute only applies to parameters and implicit object parameters
8 | int* foo(int* [[clang::lifetimebound]]);
| ^
1 error generated.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.