patch for warning on logical negation with known result

Richard Trieu rtrieu at
Tue Nov 4 14:30:58 PST 2014

Clang already has a lot of checking code for conversions, which should be
used for this checking.  To that end, this is how I think it should be laid

Define two new functions in SemaChecking.cpp:
void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC)
void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC)

The first function should be a private Sema function that serves as a
forward to the second function so that SemaExpr.cpp can access it.  The
second function should check the LangOpts and if bool is not present, call
CheckImplicitConversion.  The arguments to this function should be the Sema
reference, the Expr stripped of implicit casts, the bool type
(S.Context.BoolTy), and the source location.

In SemaExpr.cpp, remove the changes to CheckLogicalOperands.
In CheckBooleanCondition, call CheckBoolLikeConversion where it is
currently calling CheckAlwaysNonNullPointer.  This will catch the bool
conversions in if(...), while(...), etc.

Catching !.. and ... && ... can be done within in the checking functions in
SemaChecking.cpp, specifically, at the end of AnalyzeImplicitConversions.
If E is a unary expression with a logical not, call the
CheckBoolLikeConversion on the sub-expression.  If E is 1 binary expression
with a logical operator, call the function on both the LHS and RHS.

As for the testing, if the warning fires in an Analysis test, just disable
the warning in the run line.  Also, it looks like some test cases were
pulled in from the patch on nonnull.

On Wed, Oct 29, 2014 at 9:47 AM, jahanian <fjahanian at> wrote:

> - Ping.
> - Thanks, Fairborz
> Begin forwarded message:
> *Subject: **Re: patch for warning on logical negation with known result*
> *From: *jahanian <fjahanian at>
> *Date: *October 24, 2014 at 2:08:10 PM PDT
> *To: *Richard Trieu <rtrieu at>
> *Cc: *cfe commits <cfe-commits at>
> Minor change. I should not bail out early when types of implicit cast and
> its sub expression are identical as
> implicit cast may be for lvalue to rvalue conversion. This showed up for
> this test case (with old patch warning is not being issued).
> __attribute__((__nonnull__))
> void test1(void *nonnull) {
>    if (nonnull) {}
> }
> - Fariborz
> On Oct 24, 2014, at 1:23 PM, jahanian <fjahanian at> wrote:
> Thanks for the review. Here is the updated patch.
> <nonnull-patch.txt>
> - Fariborz
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the cfe-commits mailing list