This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Fix for faulty namespace test in SmartPtrModelling
ClosedPublic

Authored by RedDocMD on Jul 19 2021, 11:49 AM.

Details

Summary

This patch:

  • Fixes how the std-namespace test is written in SmartPtrModelling

(now accounts for functions with no Decl available)

  • Adds the smart pointer checker flag check where it was missing

Diff Detail

Event Timeline

RedDocMD created this revision.Jul 19 2021, 11:49 AM
RedDocMD requested review of this revision.Jul 19 2021, 11:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 19 2021, 11:49 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
RedDocMD updated this revision to Diff 359865.Jul 19 2021, 11:51 AM

Reformatted patch

RedDocMD retitled this revision from [analyer] Fix for faulty namespace test in SmartPtrModelling to [analyzer] Fix for faulty namespace test in SmartPtrModelling.Jul 19 2021, 11:54 AM
xazax.hun requested changes to this revision.Jul 19 2021, 12:17 PM

Commented some nits, but overall looks good to me.

However, could you include some tests? We usually do not commit any changes without tests unless it is really hard to create one. But I suspect that this is not the case here.

clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
274

Can we model a function call without a declaration? I wonder if we should make this check more eagerly in evalCall.

285–289

I'd prefer not repeating the ModelSmartPtrDereference check.

This revision now requires changes to proceed.Jul 19 2021, 12:17 PM
RedDocMD added inline comments.Jul 19 2021, 11:21 PM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
274

I think it is not that there is no Decl, but it is more likely the Decl is not available at that time.

void foo(void (*bar)(bool, bool)) {
    bar();
}

If just foo is analyzed, then there is no Decl.
If a call to foo is analyzed, then there is a Decl.
That said, I think the check can be done in evalCall itself. I don't think it will cause a problem.

285–289

I don't think we can do that, since the isBoolConversion() check has a branch which is executed when ModelSmartPtrDereference is false.

RedDocMD updated this revision to Diff 360027.Jul 19 2021, 11:35 PM

Added a simple test

Would this test do?

vsavchenko added inline comments.Jul 20 2021, 1:05 AM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
258–261

I think you can have a separate function:

bool isStdFunctionCall(const CallEvent &Call) {
  return Call.getDecl() && Call.getDecl()->getDeclContext()->isStdNamespace();
}

and then use it like this:

if (Call.getNumArgs() != 2 || !isStdFunctionCall(Call))
  return false;

Also, I tested this fix on a set of open-source projects and I don't see any problems.

RedDocMD updated this revision to Diff 360051.Jul 20 2021, 1:42 AM

Refactored out check

vsavchenko added inline comments.Jul 20 2021, 1:52 AM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
252–255

Can we use a one-liner that I suggested?

RedDocMD updated this revision to Diff 360061.Jul 20 2021, 2:48 AM

More refactor

clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
252–255

Sure

vsavchenko added inline comments.Jul 20 2021, 2:53 AM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
252

nit: there's an extra space here.

RedDocMD added inline comments.Jul 20 2021, 2:54 AM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
252

Wow, I didn't know this is even valid syntax 😂

RedDocMD updated this revision to Diff 360064.Jul 20 2021, 2:56 AM

Removed unnecessary white space

vsavchenko accepted this revision.Jul 20 2021, 3:02 AM

Great, LGTM!
But let's wait for @xazax.hun anyways

This revision is now accepted and ready to land.Jul 20 2021, 7:05 AM
xazax.hun added inline comments.Jul 20 2021, 7:08 AM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
285–289

That function is way below. I was just thinking about merging the first two check.

Can we please land the fix?

This revision was landed with ongoing or failed builds.Jul 21 2021, 5:54 AM
This revision was automatically updated to reflect the committed changes.
RedDocMD added inline comments.Jul 21 2021, 5:57 AM
clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp
285–289

Looks like I missed this. Sorry!