fbcode: __x__-protect all __attribute__ keywords, mechanically
authorJim Meyering <meyering@fb.com>
Sun, 17 Aug 2014 21:34:32 +0000 (14:34 -0700)
committerSara Golemon <sgolemon@fb.com>
Tue, 9 Sep 2014 21:22:22 +0000 (14:22 -0700)
commita42878e365ad97ef653a823ac5d5b62f91f63663
tree84266ce2c78b7804aa97ece207605a96e90d128d
parent4547b603051edf32b219d357fe1ded184348003e
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
16 files changed:
folly/Arena.h
folly/Bits.cpp
folly/Conv.h
folly/Demangle.cpp
folly/Malloc.h
folly/Portability.h
folly/SmallLocks.h
folly/VersionCheck.h
folly/detail/BitsDetail.h
folly/detail/CacheLocality.h
folly/detail/Malloc.h
folly/detail/MemoryIdler.cpp
folly/experimental/exception_tracer/ExceptionTracer.cpp
folly/io/test/IOBufTest.cpp
folly/test/ThreadCachedArenaTest.cpp
folly/wangle/Later.h