1 /* Poison symbols at compile time.
3 Copyright (C) 2017 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it 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.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
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/>. */
20 #ifndef COMMON_POISON_H
21 #define COMMON_POISON_H
25 /* Poison memset of non-POD types. The idea is catching invalid
26 initialization of non-POD structs that is easy to be introduced as
27 side effect of refactoring. For example, say this:
29 struct S { VEC(foo_s) *m_data; };
31 is converted to this at some point:
34 S() { m_data.reserve (10); }
35 std::vector<foo> m_data;
38 and old code was initializing S objects like this:
41 memset (&s, 0, sizeof (S)); // whoops, now wipes vector.
43 Declaring memset as deleted for non-POD types makes the memset above
44 be a compile-time error. */
46 /* Helper for SFINAE. True if "T *" is memsettable. I.e., if T is
47 either void, or POD. */
50 : gdb::Or
<std::is_void
<T
>,
55 typename
= gdb::Requires
<gdb::Not
<IsMemsettable
<T
>>>>
56 void *memset (T
*s
, int c
, size_t n
) = delete;
58 #if HAVE_IS_TRIVIALLY_COPYABLE
60 /* Similarly, poison memcpy and memmove of non trivially-copyable
61 types, which is undefined. */
63 /* True if "T *" is relocatable. I.e., copyable with memcpy/memmove.
64 I.e., T is either trivially copyable, or void. */
67 : gdb::Or
<std::is_void
<T
>,
68 std::is_trivially_copyable
<T
>>
71 /* True if both source and destination are relocatable. */
73 template <typename D
, typename S
>
74 using BothAreRelocatable
75 = gdb::And
<IsRelocatable
<D
>, IsRelocatable
<S
>>;
77 template <typename D
, typename S
,
78 typename
= gdb::Requires
<gdb::Not
<BothAreRelocatable
<D
, S
>>>>
79 void *memcpy (D
*dest
, const S
*src
, size_t n
) = delete;
81 template <typename D
, typename S
,
82 typename
= gdb::Requires
<gdb::Not
<BothAreRelocatable
<D
, S
>>>>
83 void *memmove (D
*dest
, const S
*src
, size_t n
) = delete;
85 #endif /* HAVE_IS_TRIVIALLY_COPYABLE */
87 #endif /* COMMON_POISON_H */
This page took 0.042128 seconds and 4 git commands to generate.