Let FOLLY_SAFE_DCHECK be erased by the optimizer in release builds
authorYedidya Feldblum <yfeldblum@fb.com>
Mon, 15 Aug 2016 22:22:00 +0000 (15:22 -0700)
committerFacebook Github Bot 0 <facebook-github-bot-0-bot@fb.com>
Mon, 15 Aug 2016 22:23:34 +0000 (15:23 -0700)
commit1d4854bc87a16e449760dab54e8b0c304323d133
tree97e6df7f7e9132f8f40ac9304f23fe9f931c2f5d
parentad2f228ab1359952d37acdc4f3032e1405cca221
Let FOLLY_SAFE_DCHECK be erased by the optimizer in release builds

Summary:
[Folly] Let `FOLLY_SAFE_DCHECK` be erased by the optimizer in release builds.

Just like `DCHECK` is erased by the optimizer, not by the preprocessor, in release builds. `assert`, by contrast, is erased by the preprocessor. But the intention here is to mimic `DCHECK`.

This makes a difference if the expression uses a parameter or local variable. This way, the AST still sees a use of that parameter or local variable in release builds (the optimizer later removes that use). Rather than the AST not seeing any uses of that parameter or local variable, and consequently the compiler emitting a warning or error that the parameter or local variable is unused.

Even so, the expression is never evaluated in release builds. We ensure that by actually using `true || (expr)` as the conditional of our expanded expression in release builds. The `true` comes from `!folly::kIsDebug`, which is a `constexpr bool` expression and is `true` in release builds. The `||` short-circuits; the optimizer sees that the whole expanded expression statically evaluates to `static_cast<void>(0)` and removes it the expanded expression entirely.

Reviewed By: simpkins

Differential Revision: D3701227

fbshipit-source-id: e4fa48ee5a88e45dc08757c14d1944de734796ff
folly/SafeAssert.cpp
folly/SafeAssert.h
folly/test/SafeAssertTest.cpp