This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Emit warnings when comparing result of a function with `returns_nonnull` to null
ClosedPublic

Authored by george.burgess.iv on Dec 7 2015, 10:41 PM.

Details

Summary

Addresses a problem brought up by Xavier here: http://permalink.gmane.org/gmane.comp.compilers.clang.devel/46163

tl;dr: We get a warning on the following code

void foo(void *p) __attribute__((nonnull(1))) {
	if (p == NULL) {} // warning: always equal to false
}

…But not this code:

void foo() __attribute__((returns_nonnull));
int main() {
	if (foo() == NULL) {} // no warning
}

This patch makes us give a warning in the second case.

Diff Detail

Repository
rL LLVM

Event Timeline

george.burgess.iv retitled this revision from to [Sema] Emit warnings when comparing result of a function with `returns_nonnull` to null.
george.burgess.iv updated this object.
george.burgess.iv added a reviewer: rtrieu.
george.burgess.iv added a subscriber: cfe-commits.
aaron.ballman added inline comments.
include/clang/Basic/DiagnosticSemaKinds.td
2747 ↗(On Diff #42139)

I know this is existing behavior, but can you quote true and false in this diagnostic, similar to the way true is quoted above?

lib/Sema/SemaChecking.cpp
7674 ↗(On Diff #42139)

No need to cast IsParam to int; the diagnostic builder already does the right thing here.

7682 ↗(On Diff #42139)

I think this should be:

if (Callee->hasAttr<ReturnsNonNullAttr>() &&
    ComplainAboutNonnullParamOrCall(false))
  return;

Otherwise, we skip out on the rest of the checking in the presence of ReturnsNonNullAttr.

7716 ↗(On Diff #42139)

Same here as above. The return should only be on failure.

george.burgess.iv marked 4 inline comments as done.
  • Addressed all feedback
  • Refactored a loop to make its intent more clear
lib/Sema/SemaChecking.cpp
7674 ↗(On Diff #42139)

Neat. Thanks!

7682 ↗(On Diff #42139)

The function this is in returns void, not bool. I've separated the return from the function call in order to hopefully make things a bit more clear. :)

7716 ↗(On Diff #42139)

Separated into two lines, like above.

aaron.ballman accepted this revision.Dec 8 2015, 12:21 PM
aaron.ballman added a reviewer: aaron.ballman.

Thank you for this, LGTM!

lib/Sema/SemaChecking.cpp
7682 ↗(On Diff #42204)

Oops, I didn't notice that the function returns void, sorry about that. I also didn't notice that early return here is the correct thing because we're only checking one expression at a time within this function.

This revision is now accepted and ready to land.Dec 8 2015, 12:21 PM
This revision was automatically updated to reflect the committed changes.