This is an archive of the discontinued LLVM Phabricator instance.

[-Wcalled-once-parameter] Harden analysis in terms of block use
ClosedPublic

Authored by vsavchenko on Mar 16 2021, 2:16 AM.

Details

Summary

This patch introduces a very simple inter-procedural analysis
between blocks and enclosing functions.

We always analyze blocks first (analysis is done as part of semantic
analysis that goes side-by-side with the parsing process), and at the
moment of reporting we don't know how that block will be actually
used.

This patch introduces new logic delaying reports of the "never called"
warnings on blocks. If we are not sure that the block will be called
exactly once, we shouldn't warn our users about that. Double calls,
however, don't require such delays. While analyzing the enclosing
function, we can actually decide what we should do with those
warnings.

Additionally, as a side effect, we can be more confident about blocks
in such context and can treat them not as escapes, but as direct
calls.

rdar://74090107

Diff Detail

Event Timeline

vsavchenko created this revision.Mar 16 2021, 2:16 AM
vsavchenko requested review of this revision.Mar 16 2021, 2:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 16 2021, 2:16 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
NoQ added inline comments.Mar 16 2021, 9:16 PM
clang/lib/Sema/AnalysisBasedWarnings.cpp
1729

Do i understand correctly that you're relying on the order in which your analysis is invoked from Sema? I.e., Sema parses the inner block before the outer function, so it'll analyze the block first, so by the time you see a block expression in the surrounding function you'll already have all diagnostics for the block readily available and no other diagnostics will ever show up, right?

2207

Do we really need manual new-delete here? Ownership semantics don't seem to be that complicated, a unique_ptr would probably suffice. Or maybe even just store it by value.

vsavchenko added inline comments.Mar 17 2021, 12:07 AM
clang/lib/Sema/AnalysisBasedWarnings.cpp
1729

Exactly, function is analyzed the moment it's fully parsed.

2207

I definitely don't want to move InterProceduralData definition to the header, so value is not an option.
std::unique_ptr will definitely work, but we still need to move the destructor into the cpp file because of the forward declaration as far as I understand.

Replace manual memory management of IPData with std::unique_ptr

vsavchenko marked an inline comment as done.Mar 17 2021, 2:07 AM
NoQ accepted this revision.Mar 17 2021, 8:08 PM

Great, thanks!!

clang/test/SemaObjC/warn-called-once.m
861

What would it take to move this note to the callback(); line? It would be great to do so because blocks are often huge and ^ is often hard to notice.

This revision is now accepted and ready to land.Mar 17 2021, 8:08 PM
vsavchenko added inline comments.Mar 17 2021, 11:43 PM
clang/test/SemaObjC/warn-called-once.m
861

It will be tricky not to say more. We will need to store the call after we finish analyzing the block. The tricky part is that technically it might not have any "calls", but simply escapes.
If one day we track aliases of our parameters (and the capturing block is an alias), then such blocks might be called multiple times on different paths and we'll need to report the call site of the block for sure.