51faac8e9fa3fffd5d820c2b51bf1448b85c31d5
[alexxy/gromacs.git] / src / gromacs / utility / basedefinitions.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /*! \file
38  * \brief
39  * Basic types and macros used throughout \Gromacs.
40  *
41  * \inpublicapi
42  * \ingroup module_utility
43  */
44 #ifndef GMX_UTILITY_BASEDEFINITIONS_H
45 #define GMX_UTILITY_BASEDEFINITIONS_H
46
47 #include <stdint.h>
48
49 #if defined __cplusplus
50 #include <cinttypes>
51 #include <cstddef>
52 #else
53 #include <stdbool.h>
54 #endif
55
56 //! Identical to bool
57 typedef bool gmx_bool;
58
59 #ifndef FALSE
60 /** False value for ::gmx_bool. */
61 #  define FALSE   false
62 #endif
63 #ifndef TRUE
64 /** True value for ::gmx_bool. */
65 #  define TRUE    true
66 #endif
67 /** Number of gmx_bool values. */
68 #define BOOL_NR 2
69
70 #if defined __cplusplus
71 namespace gmx
72 {
73 /*! \brief Integer type for indexing into arrays or vectors
74  *
75  * Same as ptrdiff_t.
76  */
77 using index = std::ptrdiff_t;
78 }
79 #endif
80
81 /* ICC, GCC, MSVC, Pathscale, PGI, XLC support __restrict.
82  * Any other compiler can be added here. */
83 /*! \brief
84  * Keyword to use in instead of C99 `restrict`.
85  *
86  * We cannot use `restrict` because it is only in C99, but not in C++.
87  * This macro should instead be used to allow easily supporting different
88  * compilers.
89  */
90 #define gmx_restrict __restrict
91
92 /*! \def gmx_unused
93  * \brief
94  * Attribute to suppress compiler warnings about unused function parameters.
95  *
96  * This attribute suppresses compiler warnings about unused function arguments
97  * by marking them as possibly unused.  Some arguments are unused but
98  * have to be retained to preserve a function signature
99  * that must match that of another function.
100  * Some arguments are only used in *some* conditional compilation code paths
101  * (e.g. MPI).
102  */
103 #ifndef gmx_unused
104 #ifdef __GNUC__
105 /* GCC, clang, and some ICC pretending to be GCC */
106 #  define gmx_unused __attribute__ ((unused))
107 #elif (defined(__INTEL_COMPILER) || defined(__ECC)) && !defined(_MSC_VER)
108 /* ICC on *nix */
109 #  define gmx_unused __attribute__ ((unused))
110 #elif defined(__PGI)
111 /* Portland group compilers */
112 #  define gmx_unused __attribute__ ((unused))
113 #elif defined _MSC_VER
114 /* MSVC */
115 #  define gmx_unused /*@unused@*/
116 #elif defined(__xlC__)
117 /* IBM */
118 #  define gmx_unused __attribute__ ((unused))
119 #else
120 #  define gmx_unused
121 #endif
122 #endif
123
124 /*! \brief Attribute to explicitly indicate that a parameter or
125  * locally scoped variable is used just in debug mode.
126  *
127  * \ingroup module_utility
128  */
129 #ifdef NDEBUG
130 #define gmx_used_in_debug gmx_unused
131 #else
132 #define gmx_used_in_debug
133 #endif
134
135 #ifndef __has_feature
136 /** For compatibility with non-clang compilers. */
137 #define __has_feature(x) 0
138 #endif
139
140 /*! \brief
141  * Macro to explicitly ignore an unused value.
142  *
143  * \ingroup module_utility
144  *
145  * \todo Deprecated - use gmx_unused
146  */
147 #define GMX_UNUSED_VALUE(value) (void)value
148
149 #ifdef __clang__
150 #define DO_PRAGMA(x) _Pragma (#x)
151 #define CLANG_DIAGNOSTIC_IGNORE(warning) _Pragma("clang diagnostic push") \
152     DO_PRAGMA(clang diagnostic ignored #warning)
153 #define DIAGNOSTIC_RESET _Pragma("clang diagnostic pop")
154 #else
155 //! Ignore specified clang warning until DIAGNOSTIC_RESET
156 #define CLANG_DIAGNOSTIC_IGNORE(warning)
157 //! Reset all diagnostics to default
158 #define DIAGNOSTIC_RESET
159 #endif
160
161 #ifdef __cplusplus
162 namespace gmx
163 {
164 namespace internal
165 {
166 /*! \cond internal */
167 /*! \internal \brief
168  * Helper for ignoring values in macros.
169  *
170  * \ingroup module_utility
171  */
172 template <typename T>
173 static inline void ignoreValueHelper(const T & /*unused*/)
174 {
175 }
176 //! \endcond
177 }   // namespace internal
178 }   // namespace gmx
179
180 /*! \brief
181  * Macro to explicitly ignore a return value of a call.
182  *
183  * Mainly meant for ignoring values of functions declared with
184  * `__attribute__((warn_unused_return))`.  Makes it easy to find those places if
185  * they need to be fixed, and document the intent in cases where the return
186  * value really can be ignored.  It also makes it easy to adapt the approach so
187  * that they don't produce warnings.  A cast to void doesn't remove the warning
188  * in gcc, while adding a dummy variable can cause warnings about an unused
189  * variable.
190  *
191  * \ingroup module_utility
192  */
193 #define GMX_IGNORE_RETURN_VALUE(call) \
194         ::gmx::internal::ignoreValueHelper(call)
195 #endif
196
197 #endif