Implementing checkRegionChanges for SmartPtrModeling.
To improve the accuracy, when a smart pointer is passed by a non-const reference into a function, removed the tracked region data. Since it is not sure what happens to the smart pointer inside the function.
Details
- Reviewers
NoQ Szelethus vsavchenko xazax.hun
Diff Detail
- Repository
- rG LLVM Github Monorepo
Unit Tests
Event Timeline
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
180 | I am using the same code from the MoveChecker since the same logic applies here too. | |
clang/test/Analysis/Inputs/system-header-simulator-cxx.h | ||
962 | added this to support use case like Q = std::move(P) | |
clang/test/Analysis/smart-ptr.cpp | ||
145 | @xazax.hun , Is this the type of test you were suggesting? |
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
189–190 | The whole reason why we are making this patch first is because we *don't* fully handle methods :) Also, unlike the move checker, once we handle them, they'll probably stop changing regions, so no exceptional code path would be necessary in most cases. So i'd rather start with fully wiping all the data on any sneeze and later see if we can relax it. |
clang/test/Analysis/Inputs/system-header-simulator-cxx.h | ||
---|---|---|
962 | This operation should be noexcept: https://en.cppreference.com/w/cpp/memory/unique_ptr/operator%3D While it makes little difference for the analyzer at this point we should try to be as close to the standard as possible. If you have some time feel free to add noexcept to other methods that miss it :) | |
clang/test/Analysis/smart-ptr.cpp | ||
145 | Yes, looks good to me, thanks! :) |
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
189–190 | I completely missed that point and jumped ahead. |
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
97 | nit: *need to be removed | |
98 | Maybe Subregions would fit better in this name then? | |
186 | It is not critical, but potentially we can allocate/deallocate a whole bunch of states here. We can do the same sort of operation with the map itself (State->get<TrackedRegionMap>()), which still have similar problem but to a lesser degree. Additionally, this get<MAP_TYPE> method is not compile-time, it searches for the corresponding map in runtime (also in a map), so you repeat that as many times as you have Regions. And super-duper over-optimization note on my part: making the loop over Regions to be the inner-most is more cache-friendly. It is not really critical here, but it is overall good to have an eye for things like that. | |
clang/test/Analysis/smart-ptr.cpp | ||
190 | Usually we simply add the test with expectations matching current state of things, but add a TODO over those particular lines. This way when you fix the issue the test will start failing and you won't forget to uncomment it. |
Additionally, I would prefer commit message to be imperative. It is sorta like a rule of a good commit message.
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
97 | fixed | |
98 | Changed to Subregions | |
186 | Updated removeTrackedSubregions for passing TrackedRegionMap | |
clang/test/Analysis/Inputs/system-header-simulator-cxx.h | ||
962 | Added noexcept for all applicable methods | |
clang/test/Analysis/smart-ptr.cpp | ||
190 | Enabled the test and added the todo. |
I think this looks great!
P.S. I unforgot why we won't be able to remove the option after all. That's because when we partially evalCall and partially inline, inlining breaks. Like, once we evalCalled a single method call, we can no longer trust RegionStore to have the right data inside of it on the current path, so further inlining cannot work correctly. We could forbid inlining and resort to conservative evaluation but that'd still be a regression compared to inlining. So i'd rather keep the option for as long as possible :)
This one also committed. (https://github.com/llvm/llvm-project/commit/a5609102117d2384fb73a14f37d24a0c844e3864)
clang-format not found in user's PATH; not linting file.