-Wlifetime
编译标志在clang
中的作用是什么?
我在互联网上找到的信息非常模糊。这是一个显著的特性吗?
-Wlifetime
编译标志在clang
中的作用是什么?
我在互联网上找到的信息非常模糊。这是一个显著的特性吗?
这个标志分析本地文件,看代码是否可能使用指向已死对象的指针。您可以在YouTube上观看Herb Sutter的cppcon视频,他非常好地解释了这一点:https://youtu.be/80BZxujhY38
这是一个实验性标志,它存在于https://github.com/mgehre/clang/tree/lifetime的clang开发分支中,后来移动到了https://github.com/mgehre/llvm-project/tree/lifetime。
意图是强制执行C++指南关于生命周期的部分,即所谓的生命周期配置文件https://herbsutter.com/2018/09/20/lifetime-profile-v1-0-posted/。
在clang实验实现的许多警告中,现在有两个已成为常规clang的一部分(我在我的clang 16中看到它们)。
-Wdangling-gsl
-Wreturn-stack-address
根据https://github.com/mgehre/llvm-project/issues/98上的讨论,其余部分似乎不再处于活跃开发状态。
因此,目前运行这些检查的最佳选择是使用MSVC编译器,该编译器自Visual Studio 2019以来实现了(部分)生命周期配置文件,https://devblogs.microsoft.com/cppblog/lifetime-profile-update-in-visual-studio-2019-preview-2/,并且有关Visual Studio 2022的更新https://devblogs.microsoft.com/cppblog/high-confidence-lifetime-checks-in-visual-studio-version-17-5-preview-2/。
请注意,C++核心准则中的生命周期与(针对Rust互操作性的)clang生命周期注释不同,这些注释在https://discourse.llvm.org/t/rfc-lifetime-annotations-for-c/61377中提出,并在clang和https://github.com/google/crubit/blob/main/docs/lifetimes_static_analysis.md中实现。