spot  2.9.8
common.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2013-2019 Laboratoire de Recherche et Développement
3 // de l'Epita (LRDE).
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #include <cstdlib>
21 #include <stdexcept>
22 #include <cassert>
23 
24 #pragma once
25 
26 #ifdef __GNUC__
27 #define SPOT_LIKELY(expr) __builtin_expect(!!(expr), 1)
28 #define SPOT_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
29 #else
30 #define SPOT_LIKELY(expr) (expr)
31 #define SPOT_UNLIKELY(expr) (expr)
32 #endif
33 
34 #ifdef __has_cpp_attribute
35 # if __has_cpp_attribute(deprecated) && __cplusplus >= 201402L
36 # define SPOT_DEPRECATED(msg) [[deprecated(msg)]]
37 # elif __has_cpp_attribute(gnu::deprecated)
38 # define SPOT_DEPRECATED(msg) [[gnu::deprecated(msg)]]
39 # elif __has_cpp_attribute(clang::deprecated)
40 # define SPOT_DEPRECATED(msg) [[clang::deprecated(msg)]]
41 # endif
42 #endif
43 #ifndef SPOT_DEPRECATED
44 # ifdef __GNUC__
45 # define SPOT_DEPRECATED(msg) __attribute__ ((deprecated))
46 # elif defined(_MSC_VER)
47 # define SPOT_DEPRECATED(msg) __declspec(deprecated)
48 # else
49 # define SPOT_DEPRECATED(msg)
50 # endif
51 #endif
52 
53 #if defined _WIN32 || defined __CYGWIN__
54  #define SPOT_HELPER_DLL_IMPORT __declspec(dllimport)
55  #define SPOT_HELPER_DLL_EXPORT __declspec(dllexport)
56  #define SPOT_HELPER_DLL_LOCAL
57 #else
58  #if __GNUC__ >= 4
59  #define SPOT_HELPER_DLL_IMPORT __attribute__ ((visibility ("default")))
60  #define SPOT_HELPER_DLL_EXPORT __attribute__ ((visibility ("default")))
61  #define SPOT_HELPER_DLL_LOCAL __attribute__ ((visibility ("hidden")))
62  #else
63  #define SPOT_HELPER_DLL_IMPORT
64  #define SPOT_HELPER_DLL_EXPORT
65  #define SPOT_HELPER_DLL_LOCAL
66  #endif
67 #endif
68 
69 #ifdef SPOT_BUILD
70  #define SPOT_DLL
71 #endif
72 
73 
74 // We should not call assert() in headers. For the rare cases where
75 // we do really want to call assert(), use spot_assert__ instead.
76 // Else use SPOT_ASSERT so the assert() are removed from user's
77 // builds.
78 #define spot_assert__ assert
79 #if defined(SPOT_BUILD) or defined(SPOT_DEBUG)
80  #define SPOT_ASSERT(x) spot_assert__(x)
81 #else
82  #define SPOT_ASSERT(x) while (0)
83 #endif
84 
85 // SPOT_API is used for the public API symbols. It either DLL imports
86 // or DLL exports (or does nothing for static build) SPOT_LOCAL is
87 // used for non-api symbols that may occur in header files.
88 #ifdef SPOT_DLL
89  #ifdef SPOT_BUILD
90  #define SPOT_API SPOT_HELPER_DLL_EXPORT
91  #else
92  #define SPOT_API SPOT_HELPER_DLL_IMPORT
93  #endif
94  #define SPOT_LOCAL SPOT_HELPER_DLL_LOCAL
95 #else
96  #define SPOT_API
97  #define SPOT_LOCAL
98 #endif
99 #define SPOT_API_VAR extern SPOT_API
100 
101 
102 // Swig 3.0.2 does not understand 'final' when used
103 // at class definition.
104 #ifdef SWIG
105  #define final
106 #endif
107 
108 
109 // Do not use those in code, prefer SPOT_UNREACHABLE() instead.
110 #if defined __clang__ || defined __GNUC__
111 # define SPOT_UNREACHABLE_BUILTIN() __builtin_unreachable()
112 # elif defined _MSC_VER
113 # define SPOT_UNREACHABLE_BUILTIN() __assume(0)
114 # else
115 # define SPOT_UNREACHABLE_BUILTIN() abort()
116 #endif
117 
118 // The extra parentheses in assert() is so that this
119 // pattern is not caught by the style checker.
120 #define SPOT_UNREACHABLE() do { \
121  SPOT_ASSERT(!("unreachable code reached")); \
122  SPOT_UNREACHABLE_BUILTIN(); \
123  } while (0)
124 
125 #define SPOT_UNIMPLEMENTED() throw std::runtime_error("unimplemented");
126 
127 
128 #if SPOT_DEBUG
129 #define SPOT_ASSUME(cond) assert(cond)
130 #else
131 #define SPOT_ASSUME(cond) \
132  do \
133  { \
134  if (!(cond)) \
135  SPOT_UNREACHABLE_BUILTIN(); \
136  } \
137  while (0)
138 #endif
139 
140 
141 // Useful when forwarding methods such as:
142 // auto func(int param) SPOT_RETURN(implem_.func(param));
143 #define SPOT_RETURN(code) -> decltype(code) { return code; }
144 
145 // We hope compilers that implement -Wimplicit-fallthrough also
146 // support __has_cpp_attribute and some form of [[fallthrough]]. Do
147 // not use [[fallthough]] if the code is compiled in a pre-C++17
148 // standard since clang's -Wpedantic would complain that we are using
149 // a feature from the future.
150 #ifdef __has_cpp_attribute
151 # if __has_cpp_attribute(fallthrough) && __cplusplus > 201402L
152 # define SPOT_FALLTHROUGH [[fallthrough]]
153 # elif __has_cpp_attribute(clang::fallthrough)
154 # define SPOT_FALLTHROUGH [[clang::fallthrough]]
155 # elif __has_cpp_attribute(gnu::fallthrough)
156 # define SPOT_FALLTHROUGH [[gnu::fallthrough]]
157 # endif
158 #endif
159 #ifndef SPOT_FALLTHROUGH
160 // Clang 3.5 does not support __has_cpp_attribute but has
161 // [[clang::fallthrough]].
162 # if __clang__
163 # define SPOT_FALLTHROUGH [[clang::fallthrough]]
164 # else
165 # define SPOT_FALLTHROUGH while (0)
166 # endif
167 #endif
168 
169 namespace spot
170 {
171  struct SPOT_API parse_error: public std::runtime_error
172  {
173  parse_error(const std::string& s)
174  : std::runtime_error(s)
175  {
176  }
177  };
178 }
179 
180 // This is a workaround for the issue described in GNU GCC bug 89303.
181 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89303
182 //
183 // In brief: with some version of gcc distributed by Debian unstable
184 // and that correspond to something a bit newer than 8.2.1 (Debian is
185 // tracking the gcc-8-branch instead of following releases), mixing
186 // make_shared with enable_shared_from_this produces memory leaks or
187 // bad_weak_ptr exceptions.
188 //
189 // Our workaround is simply to avoid calling make_shared in those
190 // cases.
191 //
192 // The use of "enabled" in the macro name is just here to remember
193 // that we only need this macro for classes that inherit from
194 // enable_shared_from_this.
195 #if __GNUC__ == 8 && __GNUC_MINOR__ == 2
196 # define SPOT_make_shared_enabled__(TYPE, ...) \
197  std::shared_ptr<TYPE>(new TYPE(__VA_ARGS__))
198 #else
199 # define SPOT_make_shared_enabled__(TYPE, ...) \
200  std::make_shared<TYPE>(__VA_ARGS__)
201 #endif
Definition: automata.hh:26
Definition: common.hh:171

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.8.13