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
private:
Block() { }
~Block() { }
- } __attribute__((aligned));
+ } __attribute__((__aligned__));
// This should be alignas(std::max_align_t) but neither alignas nor
// max_align_t are supported by gcc 4.6.2.
// or popcount_builtin
int popcount(unsigned int x)
#if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS)
- __attribute__((ifunc("folly_popcount_ifunc")));
+ __attribute__((__ifunc__("folly_popcount_ifunc")));
#else
{ return popcount_builtin(x); }
#endif
// or popcountll_builtin
int popcountll(unsigned long long x)
#if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS)
- __attribute__((ifunc("folly_popcountll_ifunc")));
+ __attribute__((__ifunc__("folly_popcountll_ifunc")));
#else
{ return popcountll_builtin(x); }
#endif
// still not overflow uint16_t.
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, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
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, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
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, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
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, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
//
// 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
// "The link editor does not extract archive members to resolve undefined weak
// symbols" (but, interestingly enough, will resolve undefined weak symbols
* using another malloc implementation.
*/
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)
-__attribute__((weak));
+__attribute__((__weak__));
extern "C" int mallctl(const char*, void*, size_t*, void*, size_t)
-__attribute__((weak));
+__attribute__((__weak__));
#include <bits/functexcept.h>
#define FOLLY_HAVE_MALLOC_H 1
// MaxAlign: max_align_t isn't supported by gcc
#ifdef __GNUC__
-struct MaxAlign { char c; } __attribute__((aligned));
+struct MaxAlign { char c; } __attribute__((__aligned__));
#else /* !__GNUC__ */
# error Cannot define MaxAlign on this platform
#endif
#else
# define FOLLY_PRINTF_FORMAT /**/
# define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
- __attribute__((format(printf, format_param, dots_param)))
+ __attribute__((__format__(__printf__, format_param, dots_param)))
#endif
// noreturn
#if defined(_MSC_VER)
# define FOLLY_NORETURN __declspec(noreturn)
#elif defined(__clang__) || defined(__GNUC__)
-# define FOLLY_NORETURN __attribute__((noreturn))
+# define FOLLY_NORETURN __attribute__((__noreturn__))
#else
# define FOLLY_NORETURN
#endif
#ifdef _MSC_VER
# define FOLLY_NOINLINE __declspec(noinline)
#elif defined(__clang__) || defined(__GNUC__)
-# define FOLLY_NOINLINE __attribute__((noinline))
+# define FOLLY_NOINLINE __attribute__((__noinline__))
#else
# define FOLLY_NOINLINE
#endif
#ifdef _MSC_VER
# define FOLLY_ALWAYS_INLINE __forceinline
#elif defined(__clang__) || defined(__GNUC__)
-# define FOLLY_ALWAYS_INLINE inline __attribute__((always_inline))
+# define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
#else
# define FOLLY_ALWAYS_INLINE
#endif
# define FOLLY_PACK_PUSH __pragma(pack(push, 1))
# define FOLLY_PACK_POP __pragma(pack(pop))
#elif defined(__clang__) || defined(__GNUC__)
-# define FOLLY_PACK_ATTR __attribute__((packed))
+# define FOLLY_PACK_ATTR __attribute__((__packed__))
# define FOLLY_PACK_PUSH /**/
# define FOLLY_PACK_POP /**/
#else
* 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
- * 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
* making some members private, etc.
*/
char padding_[FOLLY_CACHE_LINE_SIZE];
std::array<PaddedSpinLock, N> data_;
-} __attribute__((aligned));
+} __attribute__((__aligned__));
//////////////////////////////////////////////////////////////////////
#ifdef __APPLE__
// 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
-#define FOLLY_VERSION_CHECK_PRIORITY __attribute__((constructor(101)))
+#define FOLLY_VERSION_CHECK_PRIORITY __attribute__((__constructor__(101)))
#endif
// Note that this is carefully crafted: PRODUCT##Version must have external
// If we're targeting an architecture with popcnt support, use
// __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)
#ifdef _MSC_VER
inline int popcount(unsigned int x) {
/// 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.
-#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),
/// if available
extern "C" {
#if FOLLY_HAVE_WEAK_SYMBOLS
-int rallocm(void**, size_t*, 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 rallocm(void**, size_t*, 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__));
#else
extern int (*rallocm)(void**, size_t*, size_t, size_t, int);
extern int (*allocm)(void**, size_t*, size_t, int);
// of a link failure
extern "C" int mallctl(const char *name, void *oldp, size_t *oldlenp,
void *newp, size_t newlen)
- __attribute__((weak));
+ __attribute__((__weak__));
namespace folly { namespace detail {
using namespace __cxxabiv1;
extern "C" {
-StackTraceStack* getExceptionStackTraceStack(void) __attribute__((weak));
+StackTraceStack* getExceptionStackTraceStack(void) __attribute__((__weak__));
typedef StackTraceStack* (*GetExceptionStackTraceStackType)(void);
GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
}
// max_align_t doesn't exist in gcc 4.6.2
struct MaxAlign {
char c;
- } __attribute__((aligned));
+ } __attribute__((__aligned__));
size_t alignment = alignof(MaxAlign);
std::vector<size_t> sizes {0, 1, 64, 256, 1024, 1 << 10};
} // namespace
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 requestedBlockSize = 64;
/*
* Deprecated. Use launch()
*/
- void fireAndForget() __attribute__ ((deprecated)) { launch(); }
+ void fireAndForget() __attribute__ ((__deprecated__)) { launch(); }
private:
Promise<void> starter_;