This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Model lifetime of a variable declared in for condition in CFG correctly
ClosedPublic

Authored by tomasz-kaminski-sonarsource on Jul 17 2023, 11:21 PM.

Details

Summary

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;
}

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald Transcript
tomasz-kaminski-sonarsource requested review of this revision.Jul 17 2023, 11:21 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 17 2023, 11:21 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tomasz-kaminski-sonarsource retitled this revision from CPP-4580 Model lifetime of a variable declared in for condition in CFG correctly to [analyzer] Model lifetime of a variable declared in for condition in CFG correctly.Jul 17 2023, 11:22 PM
tomasz-kaminski-sonarsource added a subscriber: necto.
clang/test/Analysis/lifetime-cfg-output.cpp
607

Note that the lifetime ends for [B10.4]/c was missing here for the continue case, as we jumped to increment block. Same happens for the automatic object destructor that was not called in this case.

tomasz-kaminski-sonarsource edited the summary of this revision. (Show Details)
xazax.hun added inline comments.Jul 18 2023, 6:35 AM
clang/test/Analysis/auto-obj-dtors-cfg-output.cpp
1228

Would there be any value in also keeping a couple of the original test cases? (When we have empty increment blocks).

How about a test case where the increment operation generates multiple basic blocks (e.g., have a ternary)?

Updated unit tests

clang/test/Analysis/auto-obj-dtors-cfg-output.cpp
1228

Would there be any value in also keeping a couple of the original test cases? (When we have empty increment blocks).

I have removed the increment op from test_for_jumps, to cover both situation without extending the number of test.

How about a test case where the increment operation generates multiple basic blocks (e.g., have a ternary)?

Added, check test_for_inc_conditional.

tomasz-kaminski-sonarsource marked an inline comment as done.Jul 18 2023, 7:33 AM
tomasz-kaminski-sonarsource added inline comments.
clang/lib/Analysis/CFG.cpp
3511

To help clarify, the code always created a transition block, that could be empty.
This block will either contain an increment statement or be the successor for any blocks created from increment statements.
And we are filling this transition block with the handling of the condition variable.

xazax.hun accepted this revision.Jul 18 2023, 7:49 AM

LG, thanks!

This revision is now accepted and ready to land.Jul 18 2023, 7:49 AM
This revision was landed with ongoing or failed builds.Jul 19 2023, 12:06 AM
This revision was automatically updated to reflect the committed changes.
clang/test/Analysis/lifetime-cfg-output.cpp
1

@xazax.hun
A side question, this test disables cfg-temporary-dtors, cfg-rich-constructors, and cfg-implicit-dtors that are enabled by default for the analyzer.
This was done because our changes D153273 these functionalities were incompatible, and there were assertions triggering.
I would like to update the test to just enable cfg-lifetime similarly to what cfg-scopes do, so we catch any regressions, however, we will not test this functionality alone.
Is this something you (upstream) would accept? If so I can create NFC patch.

steakhal added inline comments.Jul 19 2023, 12:44 AM
clang/test/Analysis/lifetime-cfg-output.cpp
1

Makes sense to me.

xazax.hun added inline comments.Jul 19 2023, 6:37 AM
clang/test/Analysis/lifetime-cfg-output.cpp
1

Sounds good to me.

tomasz-kaminski-sonarsource marked 2 inline comments as done.Jul 19 2023, 6:59 AM
tomasz-kaminski-sonarsource added inline comments.
clang/test/Analysis/lifetime-cfg-output.cpp
1
tomasz-kaminski-sonarsource marked an inline comment as done.Jul 19 2023, 11:18 PM