blob: 4d1bb8303fa8b8137ba5a232dc73f8a13a28453e [file] [log] [blame]
// Copyright 2012 The Goma Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DEVTOOLS_GOMA_BASE_COMPILER_SPECIFIC_H_
#define DEVTOOLS_GOMA_BASE_COMPILER_SPECIFIC_H_
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once
#if defined(_MSC_VER) // COMPILER_MSVC
// Macros for suppressing and disabling warnings on MSVC.
//
// Warning numbers are enumerated at:
// http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx
//
// The warning pragma:
// http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
//
// Using __pragma instead of #pragma inside macros:
// http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
// MSVC_SUPPRESS_WARNING disables warning |n| for the remainder of the line and
// for the next line of the source file.
#define MSVC_SUPPRESS_WARNING(n) __pragma(warning(suppress:n))
// MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled.
// The warning remains disabled until popped by MSVC_POP_WARNING.
#define MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \
__pragma(warning(disable:n))
// MSVC_PUSH_WARNING_LEVEL pushes |n| as the global warning level. The level
// remains in effect until popped by MSVC_POP_WARNING(). Use 0 to disable all
// warnings.
#define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n))
// Pop effects of innermost MSVC_PUSH_* macro.
#define MSVC_POP_WARNING() __pragma(warning(pop))
#define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
#define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
// Allows |this| to be passed as an argument in constructor initializer lists.
// This uses push/pop instead of the seemingly simpler suppress feature to avoid
// having the warning be disabled for more than just |code|.
//
// Example usage:
// Foo::Foo() : x(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(y(this)), z(3) {}
//
// Compiler warning C4355: 'this': used in base member initializer list:
// http://msdn.microsoft.com/en-us/library/3c594ae3(VS.80).aspx
#define ALLOW_THIS_IN_INITIALIZER_LIST(code) MSVC_PUSH_DISABLE_WARNING(4355) \
code \
MSVC_POP_WARNING()
// Allows exporting a class that inherits from a non-exported base class.
// This uses suppress instead of push/pop because the delimiter after the
// declaration (either "," or "{") has to be placed before the pop macro.
//
// Example usage:
// class EXPORT_API Foo : NON_EXPORTED_BASE(public Bar) {
//
// MSVC Compiler warning C4275:
// non dll-interface class 'Bar' used as base for dll-interface class 'Foo'.
// Note that this is intended to be used only when no access to the base class'
// static data is done through derived classes or inline methods. For more info,
// see http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
#define NON_EXPORTED_BASE(code) MSVC_SUPPRESS_WARNING(4275) \
code
// Added for proto warnings
// - third_party\protobuf\src\google/protobuf/io/coded_stream.h(901):
// warning C4244: '=' : conversion from 'google::protobuf::uint32' to
// 'google::protobuf::uint8', possible loss of data
// - third_party\protobuf\src\google/protobuf/repeated_field.h(474):
// warning C4127: conditional expression is constant
// - third_party\protobuf\src\google/protobuf/stubs/common.h(1201):
// warning C4512: 'google::protobuf::FatalException' :
// assignment operator could not be generated
#define MSVC_PUSH_DISABLE_WARNING_FOR_PROTO() \
__pragma(warning(push)) \
__pragma(warning(disable:4244 4127 4512))
// C4127: conditional expression is constant. FD_SET uses do {..} while (0,0)
// C4389: '==' : signed/unsigned mismatch if we use 'int' for fd (fd is SOCKET)
#define MSVC_PUSH_DISABLE_WARNING_FOR_FD_SET() \
MSVC_PUSH_DISABLE_WARNING(4127 4389)
#else // Not MSVC
#define MSVC_SUPPRESS_WARNING(n)
#define MSVC_PUSH_DISABLE_WARNING(n)
#define MSVC_PUSH_WARNING_LEVEL(n)
#define MSVC_POP_WARNING()
#define MSVC_DISABLE_OPTIMIZE()
#define MSVC_ENABLE_OPTIMIZE()
#define ALLOW_THIS_IN_INITIALIZER_LIST(code) code
#define NON_EXPORTED_BASE(code) code
#define MSVC_PUSH_DISABLE_WARNING_FOR_PROTO()
#define MSVC_PUSH_DISABLE_WARNING_FOR_FD_SET()
#endif // COMPILER_MSVC
// Annotate a variable indicating it's ok if the variable is not used.
// (Typically used to silence a compiler warning when the assignment
// is important for some other reason.)
// Use like:
// int x ALLOW_UNUSED = ...;
#if defined(__GNUC__) // COMPILER_GCC
#define ALLOW_UNUSED __attribute__((unused))
#else
#define ALLOW_UNUSED
#endif
// Annotate a function indicating it should not be inlined.
// Use like:
// NOINLINE void DoStuff() { ... }
#if defined(__GNUC__) // COMPILER_GCC
#define NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER) // COMPILER_MSVC
#define NOINLINE __declspec(noinline)
#else
#define NOINLINE
#endif
// Specify memory alignment for structs, classes, etc.
// Use like:
// class ALIGNAS(16) MyClass { ... }
// ALIGNAS(16) int array[4];
#if defined(_MSC_VER) // COMPILER_MSVC
#define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
#elif defined(__GNUC__) // COMPILER_GCC
#define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
#endif
// Return the byte alignment of the given type (available at compile time).
// Use like:
// ALIGNOF(int32) // this would be 4
#if defined(_MSC_VER) // COMPILER_MSVC
#define ALIGNOF(type) __alignof(type)
#elif defined(__GNUC__) // COMPILER_GCC
#define ALIGNOF(type) __alignof__(type)
#endif
// Annotate a function indicating the caller must examine the return value.
// Use like:
// int foo() WARN_UNUSED_RESULT;
// To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
#if defined(__GNUC__) // COMPILER_GCC
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define WARN_UNUSED_RESULT
#endif
// Tell the compiler a function is using a printf-style format string.
// |format_param| is the one-based index of the format string parameter;
// |dots_param| is the one-based index of the "..." parameter.
// For v*printf functions (which take a va_list), pass 0 for dots_param.
// (This is undocumented but matches what the system C headers do.)
#if defined(__GNUC__) // COMPILER_GCC
#define PRINTF_FORMAT(format_param, dots_param) \
__attribute__((format(printf, format_param, dots_param)))
#else
#define PRINTF_FORMAT(format_param, dots_param)
#endif
// WPRINTF_FORMAT is the same, but for wide format strings.
// This doesn't appear to yet be implemented in any compiler.
// See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
#define WPRINTF_FORMAT(format_param, dots_param)
// If available, it would look like:
// __attribute__((format(wprintf, format_param, dots_param)))
#endif // DEVTOOLS_GOMA_BASE_COMPILER_SPECIFIC_H_