the purpose of the -Wlifetime flag?
Asked Answered
H

2

6

What is the purpose of the -Wlifetime compile flag in clang?

The information I found on the Internet about it are very vague. Is this any noticeable feature?

Hyalo answered 5/10, 2018 at 9:10 Comment(7)
Clang source code commit.Hyalo
The compiler can detect dangling pointers etc. See herbsutter.com/2018/09/20/lifetime-profile-v1-0-posted or an example at godbolt godbolt.org/z/szJjnHNemathelminth
Would be nice if this were in documentation!Lumberjack
@Hyalo Isn't a git commit the best kind of documentation? ;pBacolod
@LightnessRacesinOrbit It's still a pet toy. Sutter gave a talk about it at the CppCon 2018 where he says where to find the paper about it. Sorry I'm late :DHorton
@YSC: Should still be documented!Lumberjack
Wlifetime doesn't work in my clang 13. Was it deprecated or it never existed really?Banter
G
6

This flag analyzes the local file to see if the code may use pointers to objects that are dead. You can see Herb Sutter cppcon video on YouTube where he explains this very well: https://youtu.be/80BZxujhY38

Grantor answered 5/10, 2018 at 9:15 Comment(0)
L
1

This was an experimental flag that lived in a clang development branch at https://github.com/mgehre/clang/tree/lifetime and later it moved to https://github.com/mgehre/llvm-project/tree/lifetime.

The intention is to enforce the C++ Guidelines sections about lifetimes, the so-called lifetime profile https://herbsutter.com/2018/09/20/lifetime-profile-v1-0-posted/.

Out of the many warnings the clang experiment implemented, two are now part of the regular clang (I see them in my clang 16).

  • -Wdangling-gsl
  • -Wreturn-stack-address

The rest does not seem to be in active development any more, according to discussion on https://github.com/mgehre/llvm-project/issues/98.

Therefore, the best bet on running these checks is currently to use the MSVC compiler, which implements (part of) the lifetime profile since Visual Studio 2019, https://devblogs.microsoft.com/cppblog/lifetime-profile-update-in-visual-studio-2019-preview-2/ and there is an update about Visual Studio 2022 https://devblogs.microsoft.com/cppblog/high-confidence-lifetime-checks-in-visual-studio-version-17-5-preview-2/.

Note that the C++ Core Guidelines Lifetimes is something different from the (rust interop-focused) clang lifetime annotations, proposed in https://discourse.llvm.org/t/rfc-lifetime-annotations-for-c/61377 with implementation happening in clang and in https://github.com/google/crubit/blob/main/docs/lifetimes_static_analysis.md.

Lipsey answered 22/4, 2023 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.