[PATCH] D22968: [analyzer] A checker for macOS-specific bool-like objects.

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 29 11:56:20 PDT 2016


NoQ created this revision.
NoQ added reviewers: zaks.anna, dcoughlin.
NoQ added a subscriber: cfe-commits.

If you store a boolean value as an Objective-C object `NSNumber *X`, and want to see if it's true or false, than it's not a good idea to write this check as 'X == YES'. Because X is a pointer, and it is unlikely that it's equal to 1. You perhaps wanted to say `[X boolValue]` instead.

Similarly, in XNU/driver code, if you have a C++ object `OSBoolean *X`, which points to one of the two immutable objects - `kOSBooleanTrue` or `kOSBooleanFalse` , it's not a good idea to convert `X` to a C++ bool directly (eg. `bool B = X`). You want to compare it to `kOSBooleanTrue` or call the `->isTrue()` method instead.

Bugs of both kinds have been noticed in real-world code, so i'm attempting to make a simple checker for that. It is purely AST-based and moreover uses only ASTMatchers for everything (which makes it the first analyzer checker to use AST matchers). In fact i'm not sure if this checker should be in the analyzer or in clang-tidy; there should be no problem to move it around.

I'm still going to tweak these matchers a bit, as i'm in the middle of gathering statistics.

https://reviews.llvm.org/D22968

Files:
  include/clang/StaticAnalyzer/Checkers/Checkers.td
  lib/StaticAnalyzer/Checkers/BoolConversionChecker.cpp
  lib/StaticAnalyzer/Checkers/CMakeLists.txt
  test/Analysis/Inputs/system-header-simulator-objc.h
  test/Analysis/bool-conversion.cpp
  test/Analysis/bool-conversion.m

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22968.66157.patch
Type: text/x-patch
Size: 11400 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160729/87f427d2/attachment-0001.bin>


More information about the cfe-commits mailing list