This is an archive of the discontinued LLVM Phabricator instance.

[clang][Sema] removes -Wfree-nonheap-object reference param false positive
ClosedPublic

Authored by cjdb on May 18 2021, 3:02 PM.

Details

Summary

Taking the address of a reference parameter might be valid, and without
CFA, false positives are going to be more trouble than they're worth.

This reverts commit 499571ea835daf786626a0db1e12f890b6cd8f8d.

Diff Detail

Event Timeline

cjdb created this revision.May 18 2021, 3:02 PM
cjdb requested review of this revision.May 18 2021, 3:02 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMay 18 2021, 3:02 PM

Thanks for following up on this.

  • Probably best to commit the revert of the workarounds as a separate follow-up commit, but I appreciate seeing them here to understand that this patch addresses them
clang/lib/Sema/SemaChecking.cpp
10729–10731

It doesn't look like the parameter case is tested, is it? And is it necessary? I think it'd be reasonable to warn on void f1(int i) { free(&i); } - so I think it's only the "is it a reference type" that's the important property.

clang/test/Sema/warn-free-nonheap-object.cpp
38–41

Is it relevant that these are members? Or could they be globals just as well? Might be simpler/more obvious if they were globals, I think.

43–46

Are these notably different from the non-member examples above? I assume not & so I'd probably skip these test cases.

64–65

I don't think this code is necessary - since clang warnings aren't interprocedural, there's no need to flesh out the example this far - you can have the function standalone elsewhere, that takes a reference parameter and tries to free it after taking its address. Without any caller.

cjdb added a comment.Jun 28 2021, 11:42 AM

Oops, looks like I never submitted my replies!

clang/lib/Sema/SemaChecking.cpp
10729–10731

Right, but it's also reasonable to warn on this, which isn't a parameter.

int i = 0;
int& r = i;
std::free(&r);

However, I will take you up on making sure that parameters are tested.

clang/test/Sema/warn-free-nonheap-object.cpp
38–41

This tests that members aren't overlooked, which has a distinct code path to normal variables IIRC.

dblaikie added inline comments.Jun 28 2021, 1:07 PM
clang/lib/Sema/SemaChecking.cpp
10729–10731

Catching this case seems unlikely to be feasible in a compiler diagnostic - because differentiating the false positives and true positives would require more complicated analysis than is usually done.

eg: We shouldn't warn on this:

int *i = (int*)std::malloc(sizeof(int));
int &j = *i;
std::free(&j);
clang/test/Sema/warn-free-nonheap-object.cpp
13–16

I probably wouldn't bother testing the combinatorial expansion of all options (std versus global, rvalue versus lvalue) - if the features are treated orthogonally in the code (there's little chance that some particular combination would have a problem if each separately didn't have a problem).

& generally I don't think there's any need to have callers of these functions in any context (member function or otherwise - as mentioned further down) - clang warnings generally aren't interprocedural, so there's no real risk that checking the function alone would result in different results than checking it with a caller as well.

64–65

^ ping on this.

cjdb updated this revision to Diff 358127.Jul 12 2021, 7:45 PM
cjdb marked 6 inline comments as done.

responds to feedback

dblaikie accepted this revision.Jul 13 2021, 10:32 AM

Could probably haggle over the tests some more, but might be diminishing returns at this point. Looks good!

Might suggest separating the code cleanup mlir changes from the warning change (committing the warning change first, then as a follow-up committing the mlir change), ideally. Not a huge deal either way - but the usual "separable changes should be separated".

This revision is now accepted and ready to land.Jul 13 2021, 10:32 AM
cjdb updated this revision to Diff 360016.Jul 19 2021, 10:51 PM

rebases to activate CI

This revision was landed with ongoing or failed builds.Jul 21 2021, 2:30 PM
This revision was automatically updated to reflect the committed changes.