folly/Foreach.h: allow FOR_EACH to be nested with no shadowing v2016.10.10.00
authorJim Meyering <meyering@fb.com>
Sun, 9 Oct 2016 21:53:30 +0000 (14:53 -0700)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Sun, 9 Oct 2016 22:08:29 +0000 (15:08 -0700)
commitba22e52f4bfb727161128904e726f37def797b62
tree655f876602fea57d38cec4d6ee63b8510470e7b2
parent9f8538ee10e3f96fca029fdde18797878e3ebcff
folly/Foreach.h: allow FOR_EACH to be nested with no shadowing

Summary:
Prior to this change, any nested use of FOR_EACH would
induce a shadowed declaration for each of the two local
state variables it declares.

This makes the names of those variables __LINE__-dependent,
so that there is no shadowing, as long as each nested use
is on a different line.

This also adds a new test that (prior to this change) would fail to
compile with an option like -Werror=shadow-compatible-local.

Since this change relies on cpp token concatenation, I have included
The fix defines a new helper macro, _FE_ANON, to derive each new variable
name. I wondered whether to do this for every other FOR_* macro here, but
since so far, I have encountered more than 10 cases of nested FOR_EACH
uses in a large corpus, but no nesting of any other FOR_* macro, I am
content to do it only for this one.

Reviewed By: yfeldblum

Differential Revision: D3992956

fbshipit-source-id: f26fba89bc661bb9d22747dec0acdcf8c648fb83
folly/Foreach.h
folly/test/ForeachTest.cpp