folly: disable address sanitizer warnings on fbstring
[folly.git] / folly / Portability.h
index 6d373a2cef8840a5fb9aed3d37b6c3bb5195d56c..07852f2d97792825937ab5dceb182030f0642f8b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2013 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #ifndef FOLLY_PORTABILITY_H_
 #define FOLLY_PORTABILITY_H_
 
+#ifndef FOLLY_NO_CONFIG
 #include "folly-config.h"
+#endif
+
+#ifdef FOLLY_HAVE_FEATURES_H
+#include <features.h>
+#endif
+
 
 #ifdef FOLLY_HAVE_SCHED_H
  #include <sched.h>
  #endif
 #endif
 
-// Define macro wrappers for C++11's "final" and "override" keywords, which
-// are supported in gcc 4.7 but not gcc 4.6.
-//
-// TODO(tudorb/agallagher): Autotoolize this.
-#undef FOLLY_FINAL
-#undef FOLLY_OVERRIDE
 
+// MaxAlign: max_align_t isn't supported by gcc
 #ifdef __GNUC__
-# if __GNUC_PREREQ(4,7)
+struct MaxAlign { char c; } __attribute__((aligned));
+#else /* !__GNUC__ */
+# error Cannot define MaxAlign on this platform
+#endif
+
+
+// noreturn
+#if defined(__clang__) || defined(__GNUC__)
+# define FOLLY_NORETURN __attribute__((noreturn))
+#else
+# define FOLLY_NORETURN
+#endif
+
+
+// portable version check
+#ifndef __GNUC_PREREQ
+# if defined __GNUC__ && defined __GNUC_MINOR__
+#  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
+                                   ((maj) << 16) + (min))
+# else
+#  define __GNUC_PREREQ(maj, min) 0
+# endif
+#endif
+
+
+/* Define macro wrappers for C++11's "final" and "override" keywords, which
+ * are supported in gcc 4.7 but not gcc 4.6. */
+#if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
+# if defined(__clang__) || __GNUC_PREREQ(4, 7)
 #  define FOLLY_FINAL final
 #  define FOLLY_OVERRIDE override
+# else
+#  define FOLLY_FINAL /**/
+#  define FOLLY_OVERRIDE /**/
 # endif
 #endif
 
-#ifndef FOLLY_FINAL
-# define FOLLY_FINAL
-#endif
 
-#ifndef FOLLY_OVERRIDE
-# define FOLLY_OVERRIDE
+// Define to 1 if you have the `preadv' and `pwritev' functions, respectively
+#if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
+# if defined(__GLIBC_PREREQ)
+#  if __GLIBC_PREREQ(2, 10)
+#   define FOLLY_HAVE_PREADV 1
+#   define FOLLY_HAVE_PWRITEV 1
+#  endif
+# endif
 #endif
 
+/* Define attribute wrapper for function attribute used to disable
+ * address sanitizer instrumentation */
+#if defined(__clang__)
+# if __has_attribute(__no_address_safety_analysis__)
+#  define FOLLY_DISABLE_ADDRESS_SANITIZER \
+     __attribute__((__no_address_safety_analysis__))
+# elif __has_attribute(__no_sanitize_address__)
+#  define FOLLY_DISABLE_ADDRESS_SANITIZER \
+     __attribute__((__no_sanitize_address__))
+# else
+#  define FOLLY_DISABLE_ADDRESS_SANITIZER
+# endif
+#elif defined (__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)
+# define FOLLY_DISABLE_ADDRESS_SANITIZER \
+    __attribute__((__no_address_safety_analysis__))
+#else
+# define FOLLY_DISABLE_ADDRESS_SANITIZER
+#endif
 
 #endif // FOLLY_PORTABILITY_H_