[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
commitc22bb6f5b1b43484b47dd896a147bf54f8f44c9a
authorUtkarsh Saxena <usx@google.com>
Wed, 20 Nov 2024 14:17:00 +0000 (20 15:17 +0100)
committerGitHub <noreply@github.com>
Wed, 20 Nov 2024 14:17:00 +0000 (20 15:17 +0100)
tree4911a9baca83ec35df1bf4e2e5d315854663f91a
parent3e15bce9e1e144c0e568eed10010fa0e359e8ec2
[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)

This PR uses the existing lifetime analysis for the `capture_by`
attribute.

The analysis is behind `-Wdangling-capture` warning and is disabled by
default for now. Once it is found to be stable, it will be default
enabled.

Planned followup:
- add implicit inference of this attribute on STL container methods like
`std::vector::push_back`.
- (consider) warning if capturing `X` cannot capture anything. It should
be a reference, pointer or a view type.
- refactoring temporary visitors and other related handlers.
- start discussing `__global` vs `global` in the annotation in a
separate PR.

---------

Co-authored-by: Boaz Brickner <brickner@google.com>
14 files changed:
clang/include/clang/Basic/AttrDocs.td
clang/include/clang/Basic/DiagnosticGroups.td
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Sema/Sema.h
clang/lib/Sema/CheckExprLifetime.cpp
clang/lib/Sema/CheckExprLifetime.h
clang/lib/Sema/SemaChecking.cpp
clang/lib/Sema/SemaExpr.cpp
clang/lib/Sema/SemaInit.cpp
clang/lib/Sema/SemaOverload.cpp
clang/test/Sema/Inputs/lifetime-analysis.h [new file with mode: 0644]
clang/test/Sema/warn-lifetime-analysis-capture-by.cpp [new file with mode: 0644]
clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
clang/test/SemaCXX/attr-lifetimebound.cpp