This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add a checker for implicit bool conversion of a bool*.
ClosedPublic

Authored by bkramer on Jul 10 2014, 2:57 AM.

Details

Summary

The goal is to find code like the example below, which is likely a typo
where someone meant to write "if (*b)".
bool *b = SomeFunction();
if (b) {

// b never dereferenced

}

This checker naturally has a relatively high false positive rate so it
applies some heuristics to avoid cases where the pointer is checked for
nullptr before being written.

Diff Detail

Event Timeline

bkramer updated this revision to Diff 11257.Jul 10 2014, 2:57 AM
bkramer retitled this revision from to [clang-tidy] Add a checker for implicit bool conversion of a bool*..
bkramer updated this object.
bkramer added reviewers: alexfh, djasper.
bkramer added a subscriber: Unknown Object (MLST).
djasper added inline comments.Jul 10 2014, 3:14 AM
clang-tidy/misc/BoolPointerImplicitConversion.cpp
48

You could move this part into your matcher with something like

expr(ignoringImpCasts(declRefExpr().bind("expr")))
test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp
2

Maybe add a test cases with a binary expression in the if condition.

16

Don't you need at least one more CHECK-MESSAGES-NOT: warning to verify that there aren't any further warnings?

alexfh edited edge metadata.Jul 10 2014, 3:46 AM

Thank you for working on this!

test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp
8

Please add tests with macros and templates.

16

I'm also working on a patch for FileCheck to add an option to add implicit CHECK-NOT with certain pattern around each CHECK. It is rather trivial to do, I'm just stuck with the naming. Current version is -explicit-matches=<pattern>

bkramer updated this revision to Diff 11261.Jul 10 2014, 5:50 AM
bkramer edited edge metadata.

Addressed the first round of Daniel's comments.

bkramer updated this revision to Diff 11263.Jul 10 2014, 5:58 AM

Add a test with macros and templates. We seem to be doing the right thing here.

djasper accepted this revision.Jul 10 2014, 7:18 AM
djasper edited edge metadata.

I think there are still many corner cases that can be improved, but this is good as a first version.

test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp
8

Can you name this something other than CHECK? I find this a bit confusing with CHECK-MESSAGES and CHECK-FIXES..

Also consider moving this down so it is close to its invocation.

This revision is now accepted and ready to land.Jul 10 2014, 7:18 AM
bkramer updated this revision to Diff 11271.Jul 10 2014, 7:20 AM
bkramer edited edge metadata.

Don't call a macro CHECK :)

alexfh added inline comments.Jul 10 2014, 7:37 AM
clang-tidy/misc/BoolPointerImplicitConversion.cpp
52

As discussed off-the-list, presence of other pointer-specific operations could be used to avoid some false positives: use of "delete" and passing the value as an argument of a function taking bool*, reference to bool* or anything that bool* can be converted to, but bool can't (void*, maybe something else).

57

I'd check here the type of the corresponding parameter. If it is "bool", this would be a strong indicator of a true positive.

test/clang-tidy/misc-bool-pointer-implicit-conversion.cpp
33

How is this supposed to check the right thing?

38

I'd go for a "CHECK-MESSAGES-NOT: warning:", at least for consistency.

bkramer updated this revision to Diff 11276.Jul 10 2014, 8:35 AM

Allow delete expressions in the if block. I also tried to extend the callExpr filter to allow calls with implicit conversions on the arguments. Sadly hasAnyArgument has the annoying behavior of stripping all implicit casts early so this is a FIXME for now.

alexfh accepted this revision.Jul 10 2014, 9:55 AM
alexfh edited edge metadata.

Looks good.

clang-tidy/misc/BoolPointerImplicitConversion.h
23

Maybe "// Never used in a pointer-specific way."?

bkramer closed this revision.Jul 11 2014, 1:17 AM
bkramer updated this revision to Diff 11311.

Closed by commit rL212797 (authored by d0k).