Commit a42878e3 authored by Jim Meyering's avatar Jim Meyering Committed by Sara Golemon

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
parent 4547b603
...@@ -149,7 +149,7 @@ class Arena { ...@@ -149,7 +149,7 @@ class Arena {
private: private:
Block() { } Block() { }
~Block() { } ~Block() { }
} __attribute__((aligned)); } __attribute__((__aligned__));
// This should be alignas(std::max_align_t) but neither alignas nor // This should be alignas(std::max_align_t) but neither alignas nor
// max_align_t are supported by gcc 4.6.2. // max_align_t are supported by gcc 4.6.2.
......
...@@ -73,7 +73,7 @@ namespace detail { ...@@ -73,7 +73,7 @@ namespace detail {
// or popcount_builtin // or popcount_builtin
int popcount(unsigned int x) int popcount(unsigned int x)
#if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS) #if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS)
__attribute__((ifunc("folly_popcount_ifunc"))); __attribute__((__ifunc__("folly_popcount_ifunc")));
#else #else
{ return popcount_builtin(x); } { return popcount_builtin(x); }
#endif #endif
...@@ -82,7 +82,7 @@ int popcount(unsigned int x) ...@@ -82,7 +82,7 @@ int popcount(unsigned int x)
// or popcountll_builtin // or popcountll_builtin
int popcountll(unsigned long long x) int popcountll(unsigned long long x)
#if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS) #if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS)
__attribute__((ifunc("folly_popcountll_ifunc"))); __attribute__((__ifunc__("folly_popcountll_ifunc")));
#else #else
{ return popcountll_builtin(x); } { return popcountll_builtin(x); }
#endif #endif
......
...@@ -869,7 +869,7 @@ namespace detail { ...@@ -869,7 +869,7 @@ namespace detail {
// still not overflow uint16_t. // still not overflow uint16_t.
constexpr int32_t OOR = 10000; constexpr int32_t OOR = 10000;
__attribute__((aligned(16))) constexpr uint16_t shift1[] = { __attribute__((__aligned__(16))) constexpr uint16_t shift1[] = {
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
...@@ -898,7 +898,7 @@ __attribute__((aligned(16))) constexpr uint16_t shift1[] = { ...@@ -898,7 +898,7 @@ __attribute__((aligned(16))) constexpr uint16_t shift1[] = {
OOR, OOR, OOR, OOR, OOR, OOR // 250 OOR, OOR, OOR, OOR, OOR, OOR // 250
}; };
__attribute__((aligned(16))) constexpr uint16_t shift10[] = { __attribute__((__aligned__(16))) constexpr uint16_t shift10[] = {
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
...@@ -927,7 +927,7 @@ __attribute__((aligned(16))) constexpr uint16_t shift10[] = { ...@@ -927,7 +927,7 @@ __attribute__((aligned(16))) constexpr uint16_t shift10[] = {
OOR, OOR, OOR, OOR, OOR, OOR // 250 OOR, OOR, OOR, OOR, OOR, OOR // 250
}; };
__attribute__((aligned(16))) constexpr uint16_t shift100[] = { __attribute__((__aligned__(16))) constexpr uint16_t shift100[] = {
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
...@@ -956,7 +956,7 @@ __attribute__((aligned(16))) constexpr uint16_t shift100[] = { ...@@ -956,7 +956,7 @@ __attribute__((aligned(16))) constexpr uint16_t shift100[] = {
OOR, OOR, OOR, OOR, OOR, OOR // 250 OOR, OOR, OOR, OOR, OOR, OOR // 250
}; };
__attribute__((aligned(16))) constexpr uint16_t shift1000[] = { __attribute__((__aligned__(16))) constexpr uint16_t shift1000[] = {
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
// //
// TODO(tudorb): Detect this with autoconf for the open-source version. // TODO(tudorb): Detect this with autoconf for the open-source version.
// //
// __attribute__((weak)) doesn't work, because cplus_demangle_v3_callback // __attribute__((__weak__)) doesn't work, because cplus_demangle_v3_callback
// is exported by an object file in libiberty.a, and the ELF spec says // is exported by an object file in libiberty.a, and the ELF spec says
// "The link editor does not extract archive members to resolve undefined weak // "The link editor does not extract archive members to resolve undefined weak
// symbols" (but, interestingly enough, will resolve undefined weak symbols // symbols" (but, interestingly enough, will resolve undefined weak symbols
......
...@@ -51,11 +51,11 @@ namespace folly { ...@@ -51,11 +51,11 @@ namespace folly {
* using another malloc implementation. * using another malloc implementation.
*/ */
extern "C" int rallocm(void**, size_t*, size_t, size_t, int) extern "C" int rallocm(void**, size_t*, size_t, size_t, int)
__attribute__((weak)); __attribute__((__weak__));
extern "C" int allocm(void**, size_t*, size_t, int) extern "C" int allocm(void**, size_t*, size_t, int)
__attribute__((weak)); __attribute__((__weak__));
extern "C" int mallctl(const char*, void*, size_t*, void*, size_t) extern "C" int mallctl(const char*, void*, size_t*, void*, size_t)
__attribute__((weak)); __attribute__((__weak__));
#include <bits/functexcept.h> #include <bits/functexcept.h>
#define FOLLY_HAVE_MALLOC_H 1 #define FOLLY_HAVE_MALLOC_H 1
......
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
// MaxAlign: max_align_t isn't supported by gcc // MaxAlign: max_align_t isn't supported by gcc
#ifdef __GNUC__ #ifdef __GNUC__
struct MaxAlign { char c; } __attribute__((aligned)); struct MaxAlign { char c; } __attribute__((__aligned__));
#else /* !__GNUC__ */ #else /* !__GNUC__ */
# error Cannot define MaxAlign on this platform # error Cannot define MaxAlign on this platform
#endif #endif
...@@ -71,14 +71,14 @@ struct MaxAlign { char c; } __attribute__((aligned)); ...@@ -71,14 +71,14 @@ struct MaxAlign { char c; } __attribute__((aligned));
#else #else
# define FOLLY_PRINTF_FORMAT /**/ # define FOLLY_PRINTF_FORMAT /**/
# define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \ # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
__attribute__((format(printf, format_param, dots_param))) __attribute__((__format__(__printf__, format_param, dots_param)))
#endif #endif
// noreturn // noreturn
#if defined(_MSC_VER) #if defined(_MSC_VER)
# define FOLLY_NORETURN __declspec(noreturn) # define FOLLY_NORETURN __declspec(noreturn)
#elif defined(__clang__) || defined(__GNUC__) #elif defined(__clang__) || defined(__GNUC__)
# define FOLLY_NORETURN __attribute__((noreturn)) # define FOLLY_NORETURN __attribute__((__noreturn__))
#else #else
# define FOLLY_NORETURN # define FOLLY_NORETURN
#endif #endif
...@@ -87,7 +87,7 @@ struct MaxAlign { char c; } __attribute__((aligned)); ...@@ -87,7 +87,7 @@ struct MaxAlign { char c; } __attribute__((aligned));
#ifdef _MSC_VER #ifdef _MSC_VER
# define FOLLY_NOINLINE __declspec(noinline) # define FOLLY_NOINLINE __declspec(noinline)
#elif defined(__clang__) || defined(__GNUC__) #elif defined(__clang__) || defined(__GNUC__)
# define FOLLY_NOINLINE __attribute__((noinline)) # define FOLLY_NOINLINE __attribute__((__noinline__))
#else #else
# define FOLLY_NOINLINE # define FOLLY_NOINLINE
#endif #endif
...@@ -96,7 +96,7 @@ struct MaxAlign { char c; } __attribute__((aligned)); ...@@ -96,7 +96,7 @@ struct MaxAlign { char c; } __attribute__((aligned));
#ifdef _MSC_VER #ifdef _MSC_VER
# define FOLLY_ALWAYS_INLINE __forceinline # define FOLLY_ALWAYS_INLINE __forceinline
#elif defined(__clang__) || defined(__GNUC__) #elif defined(__clang__) || defined(__GNUC__)
# define FOLLY_ALWAYS_INLINE inline __attribute__((always_inline)) # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
#else #else
# define FOLLY_ALWAYS_INLINE # define FOLLY_ALWAYS_INLINE
#endif #endif
...@@ -114,7 +114,7 @@ struct MaxAlign { char c; } __attribute__((aligned)); ...@@ -114,7 +114,7 @@ struct MaxAlign { char c; } __attribute__((aligned));
# define FOLLY_PACK_PUSH __pragma(pack(push, 1)) # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
# define FOLLY_PACK_POP __pragma(pack(pop)) # define FOLLY_PACK_POP __pragma(pack(pop))
#elif defined(__clang__) || defined(__GNUC__) #elif defined(__clang__) || defined(__GNUC__)
# define FOLLY_PACK_ATTR __attribute__((packed)) # define FOLLY_PACK_ATTR __attribute__((__packed__))
# define FOLLY_PACK_PUSH /**/ # define FOLLY_PACK_PUSH /**/
# define FOLLY_PACK_POP /**/ # define FOLLY_PACK_POP /**/
#else #else
......
...@@ -96,7 +96,7 @@ namespace detail { ...@@ -96,7 +96,7 @@ namespace detail {
* init(), since the free state is guaranteed to be all-bits zero. * init(), since the free state is guaranteed to be all-bits zero.
* *
* This class should be kept a POD, so we can used it in other packed * This class should be kept a POD, so we can used it in other packed
* structs (gcc does not allow __attribute__((packed)) on structs that * structs (gcc does not allow __attribute__((__packed__)) on structs that
* contain non-POD data). This means avoid adding a constructor, or * contain non-POD data). This means avoid adding a constructor, or
* making some members private, etc. * making some members private, etc.
*/ */
...@@ -318,7 +318,7 @@ struct SpinLockArray { ...@@ -318,7 +318,7 @@ struct SpinLockArray {
char padding_[FOLLY_CACHE_LINE_SIZE]; char padding_[FOLLY_CACHE_LINE_SIZE];
std::array<PaddedSpinLock, N> data_; std::array<PaddedSpinLock, N> data_;
} __attribute__((aligned)); } __attribute__((__aligned__));
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
......
...@@ -70,9 +70,9 @@ ...@@ -70,9 +70,9 @@
#ifdef __APPLE__ #ifdef __APPLE__
// OS X doesn't support constructor priorities. Just pray it works, I guess. // OS X doesn't support constructor priorities. Just pray it works, I guess.
#define FOLLY_VERSION_CHECK_PRIORITY __attribute__((constructor)) #define FOLLY_VERSION_CHECK_PRIORITY __attribute__((__constructor__))
#else #else
#define FOLLY_VERSION_CHECK_PRIORITY __attribute__((constructor(101))) #define FOLLY_VERSION_CHECK_PRIORITY __attribute__((__constructor__(101)))
#endif #endif
// Note that this is carefully crafted: PRODUCT##Version must have external // Note that this is carefully crafted: PRODUCT##Version must have external
......
...@@ -22,7 +22,7 @@ namespace detail { ...@@ -22,7 +22,7 @@ namespace detail {
// If we're targeting an architecture with popcnt support, use // If we're targeting an architecture with popcnt support, use
// __builtin_popcount directly, as it's presumably inlined. // __builtin_popcount directly, as it's presumably inlined.
// If not, use runtime detection using __attribute__((ifunc)) // If not, use runtime detection using __attribute__((__ifunc__))
// (see Bits.cpp) // (see Bits.cpp)
#ifdef _MSC_VER #ifdef _MSC_VER
inline int popcount(unsigned int x) { inline int popcount(unsigned int x) {
......
...@@ -127,7 +127,7 @@ struct CacheLocality { ...@@ -127,7 +127,7 @@ struct CacheLocality {
/// An attribute that will cause a variable or field to be aligned so that /// An attribute that will cause a variable or field to be aligned so that
/// it doesn't have false sharing with anything at a smaller memory address. /// it doesn't have false sharing with anything at a smaller memory address.
#define FOLLY_ALIGN_TO_AVOID_FALSE_SHARING __attribute__((aligned(128))) #define FOLLY_ALIGN_TO_AVOID_FALSE_SHARING __attribute__((__aligned__(128)))
/// Holds a function pointer to the VDSO implementation of getcpu(2), /// Holds a function pointer to the VDSO implementation of getcpu(2),
/// if available /// if available
......
...@@ -24,9 +24,9 @@ ...@@ -24,9 +24,9 @@
extern "C" { extern "C" {
#if FOLLY_HAVE_WEAK_SYMBOLS #if FOLLY_HAVE_WEAK_SYMBOLS
int rallocm(void**, size_t*, size_t, size_t, int) __attribute__((weak)); int rallocm(void**, size_t*, size_t, size_t, int) __attribute__((__weak__));
int allocm(void**, size_t*, size_t, int) __attribute__((weak)); int allocm(void**, size_t*, size_t, int) __attribute__((__weak__));
int mallctl(const char*, void*, size_t*, void*, size_t) __attribute__((weak)); int mallctl(const char*, void*, size_t*, void*, size_t) __attribute__((__weak__));
#else #else
extern int (*rallocm)(void**, size_t*, size_t, size_t, int); extern int (*rallocm)(void**, size_t*, size_t, size_t, int);
extern int (*allocm)(void**, size_t*, size_t, int); extern int (*allocm)(void**, size_t*, size_t, int);
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
// of a link failure // of a link failure
extern "C" int mallctl(const char *name, void *oldp, size_t *oldlenp, extern "C" int mallctl(const char *name, void *oldp, size_t *oldlenp,
void *newp, size_t newlen) void *newp, size_t newlen)
__attribute__((weak)); __attribute__((__weak__));
namespace folly { namespace detail { namespace folly { namespace detail {
......
...@@ -33,7 +33,7 @@ using namespace ::folly::symbolizer; ...@@ -33,7 +33,7 @@ using namespace ::folly::symbolizer;
using namespace __cxxabiv1; using namespace __cxxabiv1;
extern "C" { extern "C" {
StackTraceStack* getExceptionStackTraceStack(void) __attribute__((weak)); StackTraceStack* getExceptionStackTraceStack(void) __attribute__((__weak__));
typedef StackTraceStack* (*GetExceptionStackTraceStackType)(void); typedef StackTraceStack* (*GetExceptionStackTraceStackType)(void);
GetExceptionStackTraceStackType getExceptionStackTraceStackFn; GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
} }
......
...@@ -795,7 +795,7 @@ TEST(IOBuf, Alignment) { ...@@ -795,7 +795,7 @@ TEST(IOBuf, Alignment) {
// max_align_t doesn't exist in gcc 4.6.2 // max_align_t doesn't exist in gcc 4.6.2
struct MaxAlign { struct MaxAlign {
char c; char c;
} __attribute__((aligned)); } __attribute__((__aligned__));
size_t alignment = alignof(MaxAlign); size_t alignment = alignof(MaxAlign);
std::vector<size_t> sizes {0, 1, 64, 256, 1024, 1 << 10}; std::vector<size_t> sizes {0, 1, 64, 256, 1024, 1 << 10};
......
...@@ -94,7 +94,7 @@ void ArenaTester::merge(ArenaTester&& other) { ...@@ -94,7 +94,7 @@ void ArenaTester::merge(ArenaTester&& other) {
} // namespace } // namespace
TEST(ThreadCachedArena, BlockSize) { TEST(ThreadCachedArena, BlockSize) {
struct Align { char c; } __attribute__((aligned)); struct Align { char c; } __attribute__((__aligned__));
static const size_t alignment = alignof(Align); static const size_t alignment = alignof(Align);
static const size_t requestedBlockSize = 64; static const size_t requestedBlockSize = 64;
......
...@@ -192,7 +192,7 @@ class Later { ...@@ -192,7 +192,7 @@ class Later {
/* /*
* Deprecated. Use launch() * Deprecated. Use launch()
*/ */
void fireAndForget() __attribute__ ((deprecated)) { launch(); } void fireAndForget() __attribute__ ((__deprecated__)) { launch(); }
private: private:
Promise<void> starter_; Promise<void> starter_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment