[analyzer] Model lifetime of a variable declared in for condition in CFG correctly
authortomasz-kaminski-sonarsource <79814193+tomasz-kaminski-sonarsource@users.noreply.github.com>
Tue, 18 Jul 2023 14:55:50 +0000 (16:55 +0200)
committerTomasz Kamiński <tomasz.kamiński@sonarsource.com>
Wed, 19 Jul 2023 07:01:41 +0000 (09:01 +0200)
commitc3dd2f35b876f9af39c01de49941a3920fd59a33
tree40541e91483008ea2edd31ab9c32760220d31bad
parentfaf77f8dec2f83ada139a0d6fe7acb9b3902ae8b
[analyzer] Model lifetime of a variable declared in for condition in CFG correctly

Per [stmt.for] p1 (https://eel.is/c++draft/stmt.for#1) the following
`for` and `while` statements are equivalent
```
for (; A c = b; b.c) {
  A d;
}

while (A c = b) {
  A d;
  b.c;
}
```
As a consequence, the variable declared for the condition expression
should be destroyed after the increment expression.

This fixed the handling of the object lifetime `continue`, and now
destructors, scope and lifetime elements are present for continue
path in following code:
```
for (; A c = b; b.c) {
  if (cond)
     continue;
  A d;
}
```

Reviewed By: xazax.hun

Differential Revision: https://reviews.llvm.org/D155547
clang/lib/Analysis/CFG.cpp
clang/test/Analysis/auto-obj-dtors-cfg-output.cpp
clang/test/Analysis/lifetime-cfg-output.cpp
clang/test/Analysis/scopes-cfg-output.cpp