Factor out JSON stripComments().

Summary:
Comments are a useful extension to JSON, especially for configuration files.

Facebook: twagent would previously barf on JSON files that contained '//' in their strings, and this commit allows twagent to strip comments properly. Fixes T4686066.

Test Plan: fbconfig common/json mcrouter/lib/config tupperware/agent && fbmake runtests

Reviewed By: aravindn@fb.com

Subscribers: anarayanan, pavlo, stepan, dipanshu, alikhtarov

FB internal diff: D1493963

Tasks: 4686066
Clang Love

Summary: Added support for clang in Checksum.cpp preprocessor statements.

Test Plan: The right code path is chosen using both gcc and clang now.

Reviewed By: brianp@fb.com

Subscribers: mathieubaudet, sroy, satadru

FB internal diff: D1502289
Enabling folly::Synchronized for non-default constructible objects

Summary:
Currently folly::Synchronized can't be used with non-default
constructible types because copy and move constructors require
creation of datum_ before it's assignment.
To fix this we can initialize them in initializer-list and
handle guard creation using helper constructor

Test Plan:
fbconfig -r folly && fbmake runtests
Fixes compilation error: Synchronized started working for non-default
constructible types in my project(rfe).

Reviewed By: yuri@fb.com

Subscribers: dhruvbird

FB internal diff: D1502110
Fix json_test's lookup of test files.

Summary:
The test runner for folly seems to run from folly/test rather than the base fbcode directory. This hack ensures that we find the necessary test files in either case.

This fixes the breakage introduced by {D1493963}.

Test Plan: fbconfig -r folly && fbmake runtests

Reviewed By: davejwatson@fb.com

Subscribers: dipanshu

FB internal diff: D1502161
Add Optional::value_or

Summary:
Common pattern: a function returns Optional<X>, indicating whether some X was
present or not; you want a default value if the X was not present. Requires
that X is copiable.

Test Plan: optional_test (test added)

Reviewed By: tjackson@fb.com

Subscribers: aalexandre, fjargsto, jhj, ntv, lesha, kma

FB internal diff: D1502397
(Wangle) Optimize empty continuation

Summary: It should remove one malloc as per the task.

Test Plan: `fbconfig folly/wangle && fbmake runtests` Also, `--clang`.

Reviewed By: davejwatson@fb.com

Subscribers: fugalh

FB internal diff: D1498331

Tasks: 4913277
hash_range

Test Plan: Unit test

Reviewed By: andrei.alexandrescu@fb.com

Subscribers: shilin

FB internal diff: D1497250
Fix Optional test for -fb platform

Summary:
The Optional test relied on std::string clearing its data when
it is the source of a move. This does not happen for in-situ fbstrings,
so the test breaks in the -fb platform. The solution: wrap the string in
a class that explicitly invalidates its data upon a move.

Test Plan:
fbconfig --platform-all=gcc-4.8.1-glibc-2.17-fb -r folly
fbmake runtests
fbconfig -r folly
fbmake runtests

Reviewed By: tudorb@fb.com

Subscribers: sdwilsh

FB internal diff: D1506840

Tasks: 4943996
made toAppendDelim better

Summary:
preallocate once and for all, so that we
are really malloc efficient in toAppendDelim

Test Plan: unit tests

Reviewed By: marcelo.juchem@fb.com

FB internal diff: D1496310

Tasks: 4886092
Add link-local scope handling

Summary: Also save the link-local scope in the V6 address.  See D1479365 for more details

Test Plan: fbconfig folly/test:network_address_test; fbmake runtests

Reviewed By: simpkins@fb.com

Subscribers: marccelani, doug, ps, bmatheny

FB internal diff: D1486435
fbcode: __x__-protect all __attribute__ keywords, mechanically

Summary:
The first (and sometimes 2nd) argument to __attribute__
should begin and end with "__". If it does not, then it
is at risk of conflicting with user cpp-defined symbols.
Don't do that.  This change mechanically corrects most
such violations in fbcode.  A companion change (see parent task)
adds a lint rule to help keep things clean going forwards.

Define this function to filter the list of file names to which
we will apply the transformations. First, ensure we modify only
C and C++ sources.  The second process excludes a few directories
that appear to contain third-party code:

filter() {
grep -E '\.(c|h|cc|cpp|hpp)$' | grep -vE \
'^external/|/unbound/|/gearmand_|(3rd|third.)party|/external/libevent'
}

then, run this command,

git grep -l __attribute__ | filter \
| xargs perl -pi -e 's/(__attribute__ *)\(\(( *[^_]\w+)/$1((__${2}__/g'

inducing changes like this, for each such keyword:

-} __attribute__((packed));
+} __attribute__((__packed__));

That got all but the __format__ archetype arguments like this:
__attribute__((__format__(printf.  That final "printf"
keyword should have the "__" prefix and suffix, too.

Run this command to transform those remaining uses:

git grep -l '__attribute__.*__format' | filter | xargs perl -pi -e \
's/(__attribute__ *\(\(__format__ *)\( *(printf|scanf|strftime|strfmon)/$1(__${2}__/g'

That command induces changes like this:

-static inline int __attribute__ ((__format__ (printf, 3, 4)))
+static inline int __attribute__ ((__format__ (__printf__, 3, 4)))

This exercise is useful to avoid spurious name-space conflicts,
especially when the unprotected keyword is used in a header file.

Test Plan:
I've ensured tao builds and passes its tests.
I will watch the arc-run tests.

Reviewed By: njormrod@fb.com

Subscribers: rounak, trunkagent, hphp-diffs@, nli, ps, fbcode-common-diffs@, mcdonald, chaoyc, bill, search-fbcode-diffs@, lars, net-systems@, davejwatson, varunk, ruibalp, hero-diffs@, andrewcox, sorg, dfechete, dhruba, sdoroshenko, mcduff, marccelani, hitesh, mshneer, cold-storage-diffs@, omry, jcoens, unicorn-diffs@, ldbrandy, sumeet, abirchall, fugalh, atlas2-eng@, dcapra, mpawlowski, alandau, nkgupta, shilin, bmatheny, everstore-dev@, zhuohuang, wormhole-diffs@, vnalla, msk, maoy, mwa, jgehring, adsatlasreporting@, mconnor, oujin, bwester, micha, tulloch, ptc, logdevice-diffs, alikhtarov, shikong, fuegen

FB internal diff: D1508983

Tasks: 4947824
@override-unit-failures
Fix #includes

Summary:
A few of folly's includes do not start with folly/, and so were
missed by D1411225's include-quotation ##""## -> ##<>## transformation change.
There were also some folly includes in deeper folly directories and the
docs, which I also fixed.

The search ##egrep "# *include *\"" -R folly/*## now only includes two
lines in FBString.h, which are included for libgcc purposes.

The wangle/test/Thens.cpp file, which is auto-generated, was special.
I changed the generator .rb script, but when I ran it I overwrote some
manual changes to Thens.cpp. For simplicity, I have manually applied the
change to Thens.cpp as well.

Test Plan: fbconfig -r folly && fbmake runtests

Reviewed By: hannesr@fb.com

Subscribers: sdwilsh, fugalh

FB internal diff: D1511712
Fix various compiler warnings

Summary:
This allows projects that use -Wextra (sans -Wunused-parameter) to
compile folly.

Test Plan: compiled with gcc and clang

Reviewed By: meyering@fb.com

Subscribers: njormrod, kmdent, fma, benyluo, shikong, ranjeeth, subodh, pgriess

FB internal diff: D1509827
Fix block overhead estimation in tests

Summary:
folly/test/ArenaTest.h assumes that goodMallocSize will account
for the block overhead, if the size is +1. However, when compiling
without jemalloc (as, I would imagine, most of our open-source clients
do), goodMallocSize(64 + 1) returns 65, which is less than 64 +
sizeof(Block), and so the tests fail.

I have added a constant to Arena.h which exposes the overhead taken by
the Block, and changed the tests to use that value instead of 1.
(Arena::getTotalSize relies on the totalAllocatedSize_ variable, which
is only non-trivially set in allocateSlow, where the size of the heap is
added to sizeof(Block)).

Test Plan: fbconfig --allocator=malloc folly/test:arena_test && fbmake runtests

Reviewed By: jon.coens@fb.com

Subscribers: sdwilsh, njormrod

FB internal diff: D1512231
Fix compilation warning

Summary:
Defining hash as a struct and a class was causing some junk to
get printed during fbmake.

Test Plan: `fbconfig -r proxygen; fbmake dbg`, ran unit tests, didn't see output

Reviewed By: njormrod@fb.com

Subscribers: doug, shilin, njormrod

FB internal diff: D1514793
EventCountTest cleanups

Summary: based on code review feedback in D1516171

Test Plan: ran the test

Reviewed By: philipp@fb.com

Subscribers: njormrod

FB internal diff: D1516187
Improve messaging when registrationComplete hasn't been called

Summary:
The previous message from CHECK_THROW was difficult to debug.
This improves the messaging to be more actionable.

Test Plan: runtests

Reviewed By: lins@fb.com

Subscribers: lins, anca, fugalh

FB internal diff: D1483915
Make it work more generically

Summary:
Change the strategy here: provide a more generic interface
which can be used safely with any exception types, but operates more
efficiently for explicitly named types.  Update the callers to use the
new pattern, which mostly means get() and operator* are replaced with
other more generic methods.  (This diff was constructed by merging
D1490304, D1501644, and D1500861.)

Test Plan:
run tests.  Run something which generates an exception
message, and make sure that message isn't "std::exception"

Reviewed By: marccelani@fb.com

Subscribers: ruibalp, mcduff, marccelani, hitesh, mshneer, rtgw-diffs@, alandau, bmatheny, adityab, wormhole-diffs@, bwester, njormrod

FB internal diff: D1517701
use folly::Baton in waitWithSemaphore

Test Plan:
fbconfig -r folly/wangle && fbmake runtests -j32

Reviewed By: lucian@fb.com

Subscribers: sdoroshenko, fugalh, njormrod

FB internal diff: D1525044

Tasks: 2680246

Blame Revision: D1358230
Make using folly::Singleton easier: names and direct referencing

Summary:
There are times when you want multiple singletons of the same
underlying type -- for instance, a fast and slow request handling
singleton.  This diff allows for that with an optional name that
disambiguates multiple singletons of the same type.

In addition, we now also allow direct dereferencing of the
Singleton<Foo> object to get to the underlying singleton.  This is most
useful in cases where a singleton is used inside of the same cpp file it
is defined.

Finally, make get() faster by caching the underlying pointer rather than
accessing the shared pointer.  If you're using it, you already have a
race condition and hopefully your singleton lifecycle is sane and safe
from problems this may cause.

Test Plan: runtests

Reviewed By: hans@fb.com

Subscribers: njormrod, lins, anca

FB internal diff: D1485887