1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_COMPILERWARNINGS_HPP
  26 #define SHARE_VM_UTILITIES_COMPILERWARNINGS_HPP
  27 
  28 // Macros related to control of compiler warnings.
  29 
  30 // We presently only have interesting macros here for gcc and variants,
  31 // so it's not worth going through the TARGET_COMPILER_HEADER() dispatch,
  32 // with all the non-gcc files being empty.
  33 #ifdef TARGET_COMPILER_gcc
  34 
  35 // Diagnostic pragmas like the ones defined below in PRAGMA_FORMAT_NONLITERAL_IGNORED
  36 // were only introduced in GCC 4.2. Because we have no other possibility to ignore
  37 // these warnings for older versions of GCC, we simply don't decorate our printf-style
  38 // functions with __attribute__(format) in that case.
  39 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) || (__GNUC__ > 4)
  40 #ifndef ATTRIBUTE_PRINTF
  41 #define ATTRIBUTE_PRINTF(fmt,vargs)  __attribute__((format(printf, fmt, vargs)))
  42 #endif
  43 #ifndef ATTRIBUTE_SCANF
  44 #define ATTRIBUTE_SCANF(fmt,vargs)  __attribute__((format(scanf, fmt, vargs)))
  45 #endif
  46 #endif // gcc version check
  47 
  48 #define PRAGMA_FORMAT_NONLITERAL_IGNORED _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") \
  49                                          _Pragma("GCC diagnostic ignored \"-Wformat-security\"")
  50 #define PRAGMA_FORMAT_IGNORED _Pragma("GCC diagnostic ignored \"-Wformat\"")
  51 
  52 #if defined(__clang_major__) && \
  53       (__clang_major__ >= 4 || \
  54       (__clang_major__ >= 3 && __clang_minor__ >= 1)) || \
  55     ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4)
  56 // Tested to work with clang version 3.1 and better.
  57 #define PRAGMA_DIAG_PUSH             _Pragma("GCC diagnostic push")
  58 #define PRAGMA_DIAG_POP              _Pragma("GCC diagnostic pop")
  59 
  60 #endif // clang/gcc version check
  61 
  62 #endif // TARGET_COMPILER_gcc
  63 
  64 // Defaults when not defined for the TARGET_COMPILER_xxx.
  65 
  66 #ifndef ATTRIBUTE_PRINTF
  67 #define ATTRIBUTE_PRINTF(fmt, vargs)
  68 #endif
  69 #ifndef ATTRIBUTE_SCANF
  70 #define ATTRIBUTE_SCANF(fmt, vargs)
  71 #endif
  72 
  73 #ifndef PRAGMA_FORMAT_NONLITERAL_IGNORED
  74 #define PRAGMA_FORMAT_NONLITERAL_IGNORED
  75 #endif
  76 #ifndef PRAGMA_FORMAT_IGNORED
  77 #define PRAGMA_FORMAT_IGNORED
  78 #endif
  79 
  80 #ifndef PRAGMA_DIAG_PUSH
  81 #define PRAGMA_DIAG_PUSH
  82 #endif
  83 #ifndef PRAGMA_DIAG_POP
  84 #define PRAGMA_DIAG_POP
  85 #endif
  86 
  87 #endif // SHARE_VM_UTILITIES_COMPILERWARNINGS_HPP