This patch handles the << operator defined for std::unique_ptr in
the std namespace (ignores custom overloads of the operator).
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Unit Tests
Event Timeline
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
223–224 | When you're doing evalCall you're responsible for modeling *all* aspects of the call. One does not simply say that they modeled all aspects of the call when they didn't even set the return value :) Similarly to how make_unique delegates work to the constructor of the managed object and ~unique_ptr delegates work to the destructor of the managed object, I suspect this code could delegate work to basic_ostream::operator<<(T *). We don't yet have any facilities to implement such logic yet (i.e., entering function call from a checker callback). Given that we don't do much modeling of basic_ostream yet, I think it's perfectly fine to invalidate the stream entirely (which would be as precise as whatever the default evaluation gave us) (see ProgramState::invalidateRegions) and return the reference to the stream (which is already better than what the default evaluation gave us); additionally, we get extra precision because we don't invalidate the rest of the heap. That's the bare minimum of what we have to do here if we are to do anything at all. This also gives some ideas of how to write tests for this patch. That said, I suspect that this patch is not critical to enabling the checker by default, because we probably already know that this method doesn't change the inner pointer value (simply through not doing anything special) (and accepting the smart pointer by const reference, so even if we implement invalidation it will probably still "just work") (?). |
Invalidating regions
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
223–224 | Does this work? |
Yes, I think this totally works now!
Can you also add some tests?
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
86 | This should be static right? | |
110 | I think the new functions that you've added aren't yet used in an inter-checker manner. Maybe keep them as static too until we have an actual use case? | |
226–227 | Might also be an UnknownVal which doesn't have a region. It's hard to test because there doesn't exist a test in which UnknownVal appears for a good reason (they only appear for bad reasons) but I'd still rather bail out. |
Good job, great to see how you are going through the whole list of methods!
As for the patch, some tests and COMMENTS will be nice. Also, I think that for a checker that models quite a lot of functions and methods, we need to follow the pattern:
if (isMethodA(...)) { return handleMethodA(...); } if (isMethodB(...)) { return handleMethodB(...); } ...
however small implementations for those methods are. It will give fast insight into which methods are actually supported.
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
87 | When you change those vectors of names to global arrays, we can change it to ArrayRef to be more idiomatic LLVM code. | |
119 | This is a compile-time constant, I don't think we should construct it every time in runtime. Global array of three names is totally fine. | |
197 | Same goes here |
Great, thanks for addressing my comments! I still have a couple of minor suggestions though.
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
99–100 | If it's a compile-time constant, let's make sure it is. | |
195 | Same here + don't call it "Name" (singular). It is a) an array and b) in the future, we might add more things to it, so we shouldn't need to rename it everywhere. |
Great, thanks!
clang/test/Analysis/smart-ptr.cpp | ||
---|---|---|
472–474 | Another thing to test could be that global variables are not invalidated. Eg.: int glob; void foo(std::unique_ptr<int> P) { int x = glob; std::cout << P; int y = glob; clang_analyzer_eval(x == y); // expected-warning{{TRUE}} } | |
476 | This deserves a FIXME: because the expected result is still TRUE. |
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
195 | Well, we won't have to rename but we will still have to change the array length everywhere if more names are added. |
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp | ||
---|---|---|
87 | ArrayRef is already a ref (and it says it in its name), so you should remove it there. | |
103 | ArrayRef has an implicit constructor from C-arrays, putting size here is unnecessary. | |
195 | No, we don't have to do that. See the reason in the other comment. |
For some reason this revision did not get automatically closed after commit, manually merging it.
This should be static right?