mwu's blog
A tale of a fearless coder
Killing PRBool with clang and a little bit of fire
July 23rd, 2011 by mwu
Once upon a time, there was a tool called prcheck which checked usage of PRBools and made sure only righteous 1s and 0s were assigned to PRBools. The goal was to correct all the uses of PRBool and hopefully turn PRBool into a Real Boolean.
This didn’t happen, though many abuses of PRBool were corrected along the way.
But, there may be hope now. A new PRBool checking tool based on clang has lead to a number of patches correcting various misuses of PRBool across the Mozilla codebase. The abuses of PRBool are varied and creative, but they all tend to fall under two categories:
- Using PRBool when some other type would be more appropriate or vice versa. (bug 671190 and bug 671417)
- Returning NS_ERROR_* codes in functions that return PRBool. This is particularly bad as error codes are likely to be treated as true, and true usually means success. (bug 671185)
With the help of this clang plugin, a bit of manual build fixing, and some hours lost in gdb, we can typedef PRBool as a (real) bool. The local build here is capable of starting up, passing make check, and passing all but one xpcshell test.
More updates and code release of this clang plugin to come.
Michael, do you plan on running the same kind of analysis on comm-central? We have lots of misuses of PRBool in there, some of which have caused severe pains (see https://bugzilla.mozilla.org/show_bug.cgi?id=654126 for instance), and I’d love to see these analyses run there as well!
Nice! (Death to PRBool!)
Oh, didn’t think of that. I’ll try running the tool on comm-central and see what happens.
Thanks! Please CC me (:protz) on the relevant bug if there’s and if you don’t mind
.