michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_COMPILER_SPECIFIC_H_ michael@0: #define BASE_COMPILER_SPECIFIC_H_ michael@0: michael@0: #include "build/build_config.h" michael@0: michael@0: #if defined(COMPILER_MSVC) michael@0: michael@0: // Macros for suppressing and disabling warnings on MSVC. michael@0: // michael@0: // Warning numbers are enumerated at: michael@0: // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx michael@0: // michael@0: // The warning pragma: michael@0: // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx michael@0: // michael@0: // Using __pragma instead of #pragma inside macros: michael@0: // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx michael@0: michael@0: // MSVC_SUPPRESS_WARNING disables warning |n| for the remainder of the line and michael@0: // for the next line of the source file. michael@0: #define MSVC_SUPPRESS_WARNING(n) __pragma(warning(suppress:n)) michael@0: michael@0: // MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled. michael@0: // The warning remains disabled until popped by MSVC_POP_WARNING. michael@0: #define MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \ michael@0: __pragma(warning(disable:n)) michael@0: michael@0: // MSVC_PUSH_WARNING_LEVEL pushes |n| as the global warning level. The level michael@0: // remains in effect until popped by MSVC_POP_WARNING(). Use 0 to disable all michael@0: // warnings. michael@0: #define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n)) michael@0: michael@0: // Pop effects of innermost MSVC_PUSH_* macro. michael@0: #define MSVC_POP_WARNING() __pragma(warning(pop)) michael@0: michael@0: #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off)) michael@0: #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on)) michael@0: michael@0: // Allows exporting a class that inherits from a non-exported base class. michael@0: // This uses suppress instead of push/pop because the delimiter after the michael@0: // declaration (either "," or "{") has to be placed before the pop macro. michael@0: // michael@0: // Example usage: michael@0: // class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) { michael@0: // michael@0: // MSVC Compiler warning C4275: michael@0: // non dll-interface class 'Bar' used as base for dll-interface class 'Foo'. michael@0: // Note that this is intended to be used only when no access to the base class' michael@0: // static data is done through derived classes or inline methods. For more info, michael@0: // see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx michael@0: #define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \ michael@0: code michael@0: michael@0: #else // Not MSVC michael@0: michael@0: #define MSVC_SUPPRESS_WARNING(n) michael@0: #define MSVC_PUSH_DISABLE_WARNING(n) michael@0: #define MSVC_PUSH_WARNING_LEVEL(n) michael@0: #define MSVC_POP_WARNING() michael@0: #define MSVC_DISABLE_OPTIMIZE() michael@0: #define MSVC_ENABLE_OPTIMIZE() michael@0: #define NON_EXPORTED_BASE(code) code michael@0: michael@0: #endif // COMPILER_MSVC michael@0: michael@0: michael@0: // Annotate a variable indicating it's ok if the variable is not used. michael@0: // (Typically used to silence a compiler warning when the assignment michael@0: // is important for some other reason.) michael@0: // Use like: michael@0: // int x ALLOW_UNUSED = ...; michael@0: #if defined(COMPILER_GCC) michael@0: #define ALLOW_UNUSED __attribute__((unused)) michael@0: #else michael@0: #define ALLOW_UNUSED michael@0: #endif michael@0: michael@0: // Annotate a function indicating it should not be inlined. michael@0: // Use like: michael@0: // NOINLINE void DoStuff() { ... } michael@0: #if defined(COMPILER_GCC) michael@0: #define NOINLINE __attribute__((noinline)) michael@0: #elif defined(COMPILER_MSVC) michael@0: #define NOINLINE __declspec(noinline) michael@0: #else michael@0: #define NOINLINE michael@0: #endif michael@0: michael@0: // Specify memory alignment for structs, classes, etc. michael@0: // Use like: michael@0: // class ALIGNAS(16) MyClass { ... } michael@0: // ALIGNAS(16) int array[4]; michael@0: #if defined(COMPILER_MSVC) michael@0: #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) michael@0: #elif defined(COMPILER_GCC) michael@0: #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) michael@0: #endif michael@0: michael@0: // Return the byte alignment of the given type (available at compile time). Use michael@0: // sizeof(type) prior to checking __alignof to workaround Visual C++ bug: michael@0: // http://goo.gl/isH0C michael@0: // Use like: michael@0: // ALIGNOF(int32) // this would be 4 michael@0: #if defined(COMPILER_MSVC) michael@0: #define ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type)) michael@0: #elif defined(COMPILER_GCC) michael@0: #define ALIGNOF(type) __alignof__(type) michael@0: #endif michael@0: michael@0: // Annotate a virtual method indicating it must be overriding a virtual michael@0: // method in the parent class. michael@0: // Use like: michael@0: // virtual void foo() OVERRIDE; michael@0: #if defined(COMPILER_MSVC) michael@0: #define OVERRIDE override michael@0: #elif defined(__clang__) michael@0: #define OVERRIDE override michael@0: #else michael@0: #define OVERRIDE michael@0: #endif michael@0: michael@0: // Annotate a virtual method indicating that subclasses must not override it, michael@0: // or annotate a class to indicate that it cannot be subclassed. michael@0: // Use like: michael@0: // virtual void foo() FINAL; michael@0: // class B FINAL : public A {}; michael@0: #if defined(COMPILER_MSVC) michael@0: // TODO(jered): Change this to "final" when chromium no longer uses MSVC 2010. michael@0: #define FINAL sealed michael@0: #elif defined(__clang__) michael@0: #define FINAL final michael@0: #else michael@0: #define FINAL michael@0: #endif michael@0: michael@0: // Annotate a function indicating the caller must examine the return value. michael@0: // Use like: michael@0: // int foo() WARN_UNUSED_RESULT; michael@0: // To explicitly ignore a result, see |ignore_result()| in . michael@0: #if defined(COMPILER_GCC) michael@0: #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) michael@0: #else michael@0: #define WARN_UNUSED_RESULT michael@0: #endif michael@0: michael@0: // Tell the compiler a function is using a printf-style format string. michael@0: // |format_param| is the one-based index of the format string parameter; michael@0: // |dots_param| is the one-based index of the "..." parameter. michael@0: // For v*printf functions (which take a va_list), pass 0 for dots_param. michael@0: // (This is undocumented but matches what the system C headers do.) michael@0: #if defined(COMPILER_GCC) michael@0: #define PRINTF_FORMAT(format_param, dots_param) \ michael@0: __attribute__((format(printf, format_param, dots_param))) michael@0: #else michael@0: #define PRINTF_FORMAT(format_param, dots_param) michael@0: #endif michael@0: michael@0: // WPRINTF_FORMAT is the same, but for wide format strings. michael@0: // This doesn't appear to yet be implemented in any compiler. michael@0: // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . michael@0: #define WPRINTF_FORMAT(format_param, dots_param) michael@0: // If available, it would look like: michael@0: // __attribute__((format(wprintf, format_param, dots_param))) michael@0: michael@0: michael@0: // MemorySanitizer annotations. michael@0: #ifdef MEMORY_SANITIZER michael@0: extern "C" { michael@0: void __msan_unpoison(const void *p, unsigned long s); michael@0: } // extern "C" michael@0: michael@0: // Mark a memory region fully initialized. michael@0: // Use this to annotate code that deliberately reads uninitialized data, for michael@0: // example a GC scavenging root set pointers from the stack. michael@0: #define MSAN_UNPOISON(p, s) __msan_unpoison(p, s) michael@0: #else // MEMORY_SANITIZER michael@0: #define MSAN_UNPOISON(p, s) michael@0: #endif // MEMORY_SANITIZER michael@0: michael@0: #endif // BASE_COMPILER_SPECIFIC_H_