-Wlifetime 标志的目的是什么?

the purpose of the -Wlifetime flag?

本文关键字:是什么 -Wlifetime 标志      更新时间:2023-10-16

clang-Wlifetime编译标志的目的是什么?

我在互联网上找到的有关它的信息非常模糊。这有什么明显的功能吗?

此标志分析本地文件,以查看代码是否可以使用指向已死对象的指针。 你可以在YouTube上看到Herb Sutter cppcon的视频,他很好地解释了这一点: https://youtu.be/80BZxujhY38

这是一个实验性旗帜,住在 https://github.com/mgehre/clang/tree/lifetime 的叮当发展分支中,后来它搬到了 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 中实现。