Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_COMPILER_SPECIFIC_H_
      6 #define BASE_COMPILER_SPECIFIC_H_
      7 
      8 #include "build/build_config.h"
      9 
     10 #if defined(COMPILER_MSVC)
     11 
     12 // Macros for suppressing and disabling warnings on MSVC.
     13 //
     14 // Warning numbers are enumerated at:
     15 // http://msdn.microsoft.com/en-us/library/8x5x43k7(VS.80).aspx
     16 //
     17 // The warning pragma:
     18 // http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
     19 //
     20 // Using __pragma instead of #pragma inside macros:
     21 // http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
     22 
     23 // MSVC_SUPPRESS_WARNING disables warning |n| for the remainder of the line and
     24 // for the next line of the source file.
     25 #define MSVC_SUPPRESS_WARNING(n) __pragma(warning(suppress:n))
     26 
     27 // MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled.
     28 // The warning remains disabled until popped by MSVC_POP_WARNING.
     29 #define MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \
     30                                      __pragma(warning(disable:n))
     31 
     32 // MSVC_PUSH_WARNING_LEVEL pushes |n| as the global warning level.  The level
     33 // remains in effect until popped by MSVC_POP_WARNING().  Use 0 to disable all
     34 // warnings.
     35 #define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n))
     36 
     37 // Pop effects of innermost MSVC_PUSH_* macro.
     38 #define MSVC_POP_WARNING() __pragma(warning(pop))
     39 
     40 #define MSVC_DISABLE_OPTIMIZE() __pragma(optimize("", off))
     41 #define MSVC_ENABLE_OPTIMIZE() __pragma(optimize("", on))
     42 
     43 // Allows |this| to be passed as an argument in constructor initializer lists.
     44 // This uses push/pop instead of the seemingly simpler suppress feature to avoid
     45 // having the warning be disabled for more than just |code|.
     46 //
     47 // Example usage:
     48 // Foo::Foo() : x(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(y(this)), z(3) {}
     49 //
     50 // Compiler warning C4355: 'this': used in base member initializer list:
     51 // http://msdn.microsoft.com/en-us/library/3c594ae3(VS.80).aspx
     52 #define ALLOW_THIS_IN_INITIALIZER_LIST(code) MSVC_PUSH_DISABLE_WARNING(4355) \
     53                                              code \
     54                                              MSVC_POP_WARNING()
     55 
     56 #else  // Not MSVC
     57 
     58 #define MSVC_SUPPRESS_WARNING(n)
     59 #define MSVC_PUSH_DISABLE_WARNING(n)
     60 #define MSVC_PUSH_WARNING_LEVEL(n)
     61 #define MSVC_POP_WARNING()
     62 #define MSVC_DISABLE_OPTIMIZE()
     63 #define MSVC_ENABLE_OPTIMIZE()
     64 #define ALLOW_THIS_IN_INITIALIZER_LIST(code) code
     65 
     66 #endif  // COMPILER_MSVC
     67 
     68 
     69 #if defined(COMPILER_GCC)
     70 
     71 #define ALLOW_UNUSED __attribute__((unused))
     72 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
     73 
     74 // Tell the compiler a function is using a printf-style format string.
     75 // |format_param| is the one-based index of the format string parameter;
     76 // |dots_param| is the one-based index of the "..." parameter.
     77 // For v*printf functions (which take a va_list), pass 0 for dots_param.
     78 // (This is undocumented but matches what the system C headers do.)
     79 #define PRINTF_FORMAT(format_param, dots_param) \
     80     __attribute__((format(printf, format_param, dots_param)))
     81 
     82 // WPRINTF_FORMAT is the same, but for wide format strings.
     83 // This doesn't appear to yet be implemented.
     84 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
     85 #define WPRINTF_FORMAT(format_param, dots_param)
     86 // If available, it would look like:
     87 //   __attribute__((format(wprintf, format_param, dots_param)))
     88 
     89 #else  // Not GCC
     90 
     91 #define ALLOW_UNUSED
     92 #define WARN_UNUSED_RESULT
     93 #define PRINTF_FORMAT(x, y)
     94 #define WPRINTF_FORMAT(x, y)
     95 
     96 #endif
     97 
     98 #endif  // BASE_COMPILER_SPECIFIC_H_
     99