This is an archive of the discontinued LLVM Phabricator instance.

[Analyzer] Handle unique_ptr::swap() in SmartPtrModeling
ClosedPublic

Authored by vrnithinkumar on Jul 15 2020, 8:01 AM.

Details

Summary

Enabling the SmartPtrModeling to handle swap method for unique_ptr

Diff Detail

Event Timeline

vrnithinkumar created this revision.Jul 15 2020, 8:01 AM
vrnithinkumar edited the summary of this revision. (Show Details)Jul 15 2020, 8:03 AM
vrnithinkumar marked an inline comment as done.Jul 15 2020, 8:04 AM
vrnithinkumar added inline comments.
clang/test/Analysis/smart-ptr.cpp
131

I will fix this.

xazax.hun accepted this revision.Jul 15 2020, 8:21 AM

LGTM, thanks!

clang/test/Analysis/Inputs/system-header-simulator-cxx.h
965

Maybe it is worth to add a TODO to introduce an additional template parameter once the deleter is added above.

https://en.cppreference.com/w/cpp/memory/unique_ptr/swap2

clang/test/Analysis/smart-ptr.cpp
124

This test case is very similar to the first one. Maybe you could move them closer. And you could make both pointers invalid to make them more distinct.

This revision is now accepted and ready to land.Jul 15 2020, 8:21 AM
NoQ added inline comments.Jul 15 2020, 9:36 AM
clang/test/Analysis/Inputs/system-header-simulator-cxx.h
964–965

You seem to be relying on the fact that global std::swap is implemented in terms of the member std::swap. That's an implementation detail of the standard library; i'm not sure that this is always the case. Ideally we should model the global std::swap separately.

xazax.hun added inline comments.Jul 15 2020, 9:59 AM
clang/test/Analysis/Inputs/system-header-simulator-cxx.h
964–965

I am not sure how reliable cppreference is, but I think this overload might actually be guaranteed by the standard: https://en.cppreference.com/w/cpp/memory/unique_ptr/swap2

vsavchenko added inline comments.Jul 15 2020, 10:32 AM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
198

I think it would be good to add some comments in the body of the function to be more explicit about the situation you are handling.

224–228

These two if's are clearly duplicates and it seems like a good candidate for a separate function.

clang/test/Analysis/smart-ptr.cpp
124

+1

The same thing with the commit message here, it would be better as just "Change"

vrnithinkumar retitled this revision from [Analyzer] Changed in SmartPtrModeling to handle Swap to [Analyzer] Handle unique_ptr::swap() in SmartPtrModeling.Jul 17 2020, 2:46 AM
vrnithinkumar marked 11 inline comments as done.
vrnithinkumar edited the summary of this revision. (Show Details)
  • Changes from review comments
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
198

Added comments

224–228

Thanks!
created a separate function updateSwappedRegion()

clang/test/Analysis/Inputs/system-header-simulator-cxx.h
964–965

I also made the assumption based on this (https://en.cppreference.com/w/cpp/memory/unique_ptr/swap2).

965

Added the TODO

clang/test/Analysis/smart-ptr.cpp
124

Moved and made both pointers invalid in the test

131

Fixed

NoQ accepted this revision.Jul 17 2020, 2:10 PM
NoQ added inline comments.
clang/test/Analysis/Inputs/system-header-simulator-cxx.h
964–965

Aha, interesting. We still probably can't rely on it being always inlined though, so we'll still probably have to model it separately.

NoQ added a comment.Aug 2 2020, 11:43 AM

These patches look like they're done, maybe let's land them?

vrnithinkumar marked an inline comment as done.Aug 2 2020, 1:34 PM
In D83877#2189637, @NoQ wrote:

These patches look like they're done, maybe let's land them?

I have already committed this changes.
https://github.com/llvm/llvm-project/commit/76c0577763505ea3db1017a9aab579c1c2f135d0

clang/test/Analysis/Inputs/system-header-simulator-cxx.h
964–965

Okay. We will add the separate modeling for std::swap

vrnithinkumar closed this revision.Aug 3 2020, 5:05 PM
vrnithinkumar marked an inline comment as done.