[PATCH] New warning to detect when a logical not is applied to the LHS of a comparison when the intent was for the logical not to apply to the whole comparison

Richard Smith richard at metafoo.co.uk
Mon Apr 1 11:21:01 PDT 2013



================
Comment at: lib/Sema/SemaExpr.cpp:7027-7029
@@ +7026,5 @@
+
+  // Check that left hand side is !something.
+  UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS.get());
+  if (!UO || UO->getOpcode() != UO_LNot) return;
+
----------------
Tiny thing: this is probably cheaper than the type check for the RHS, and will bail out much more often; maybe move this check earlier?

================
Comment at: lib/Sema/SemaExpr.cpp:7041
@@ +7040,3 @@
+  SourceLocation FirstOpen = SubExpr->getLocStart();
+  SourceLocation FirstClose = RHS.get()->getSourceRange().getEnd();
+  FirstClose = S.getPreprocessor().getLocForEndOfToken(FirstClose);
----------------
RHS.get()->getLocEnd()

================
Comment at: lib/Sema/SemaExpr.cpp:7048-7049
@@ +7047,4 @@
+  // Second note suggests (!x) < y
+  SourceLocation SecondOpen = LHS.get()->getLocStart();
+  SourceLocation SecondClose = LHS.get()->getSourceRange().getEnd();
+  SecondClose = S.getPreprocessor().getLocForEndOfToken(SecondClose);
----------------
LHS.get()->getLocEnd(), or use LHS.get()->getSourceRange() to get both at once (which is slightly cheaper).


http://llvm-reviews.chandlerc.com/D505



More information about the cfe-commits mailing list