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?
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?
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
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.
© 2022 - 2024 — McMap. All rights reserved.