Skip to content

Commit 6bd249a

Browse files
committed
Merge pull request godotengine#104850 from Repiteo/core/warning-macros
Core: Integrate warning suppression macro helpers
2 parents 7f1f4e4 + 207a2b6 commit 6bd249a

25 files changed

+119
-180
lines changed

core/io/packet_peer_udp.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,16 @@ Error PacketPeerUDP::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
105105
return ERR_UNAVAILABLE;
106106
}
107107

108-
/* Bogus GCC warning here:
109-
* In member function 'int RingBuffer<T>::read(T*, int, bool) [with T = unsigned char]',
110-
* inlined from 'virtual Error PacketPeerUDP::get_packet(const uint8_t**, int&)' at core/io/packet_peer_udp.cpp:112:9,
111-
* inlined from 'virtual Error PacketPeerUDP::get_packet(const uint8_t**, int&)' at core/io/packet_peer_udp.cpp:99:7:
112-
* Error: ./core/ring_buffer.h:68:46: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
113-
* 68 | p_buf[dst++] = read[pos + i];
114-
* | ~~~~~~~~~~~~~^~~~~~~
115-
*/
116-
#if defined(__GNUC__) && !defined(__clang__)
117-
#pragma GCC diagnostic push
118-
#pragma GCC diagnostic warning "-Wstringop-overflow=0"
119-
#endif
108+
/* Bogus GCC warning here:
109+
* In member function 'int RingBuffer<T>::read(T*, int, bool) [with T = unsigned char]',
110+
* inlined from 'virtual Error PacketPeerUDP::get_packet(const uint8_t**, int&)' at core/io/packet_peer_udp.cpp:112:9,
111+
* inlined from 'virtual Error PacketPeerUDP::get_packet(const uint8_t**, int&)' at core/io/packet_peer_udp.cpp:99:7:
112+
* Error: ./core/ring_buffer.h:68:46: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
113+
* 68 | p_buf[dst++] = read[pos + i];
114+
* | ~~~~~~~~~~~~~^~~~~~~
115+
*/
116+
GODOT_GCC_WARNING_PUSH
117+
GODOT_GCC_PRAGMA(GCC diagnostic warning "-Wstringop-overflow=0") // Can't "ignore" this for some reason.
120118

121119
uint32_t size = 0;
122120
uint8_t ipv6[16] = {};
@@ -129,9 +127,7 @@ Error PacketPeerUDP::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
129127
*r_buffer = packet_buffer;
130128
r_buffer_size = size;
131129

132-
#if defined(__GNUC__) && !defined(__clang__)
133-
#pragma GCC diagnostic pop
134-
#endif
130+
GODOT_GCC_WARNING_POP
135131

136132
return OK;
137133
}

core/math/geometry_2d.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,7 @@ class Geometry2D {
167167
return p_segment_a + n * d; // Inside.
168168
}
169169

170-
// Disable False Positives in MSVC compiler; we correctly check for 0 here to prevent a division by 0.
171-
// See: https://github.com/godotengine/godot/pull/44274
172-
#ifdef _MSC_VER
173-
#pragma warning(disable : 4723)
174-
#endif
170+
GODOT_MSVC_WARNING_PUSH_AND_IGNORE(4723) // Potential divide by 0. False positive (see: GH-44274).
175171

176172
static bool line_intersects_line(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b, Vector2 &r_result) {
177173
// See http://paulbourke.net/geometry/pointlineplane/
@@ -187,10 +183,7 @@ class Geometry2D {
187183
return true;
188184
}
189185

190-
// Re-enable division by 0 warning
191-
#ifdef _MSC_VER
192-
#pragma warning(default : 4723)
193-
#endif
186+
GODOT_MSVC_WARNING_POP
194187

195188
static bool segment_intersects_segment(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b, Vector2 *r_result) {
196189
Vector2 B = p_to_a - p_from_a;

core/object/method_bind.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,16 @@ class MethodBindVarArgTR : public MethodBindVarArgBase<MethodBindVarArgTR<T, R>,
257257
friend class MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>;
258258

259259
public:
260-
#if defined(SANITIZERS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
261-
// Workaround GH-66343 raised only with UBSAN, seems to be a false positive.
262-
#pragma GCC diagnostic push
263-
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
264-
#endif
260+
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wmaybe-uninitialized") // Workaround GH-66343 raised only with UBSAN, seems to be a false positive.
261+
265262
virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
266263
#ifdef TOOLS_ENABLED
267264
ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == MethodBind::get_instance_class(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
268265
#endif
269266
return (static_cast<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>::method)(p_args, p_arg_count, r_error);
270267
}
271268

272-
#if defined(SANITIZERS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
273-
#pragma GCC diagnostic pop
274-
#endif
269+
GODOT_GCC_WARNING_POP
275270

276271
MethodBindVarArgTR(
277272
R (T::*p_method)(const Variant **, int, Callable::CallError &),

core/object/script_language_extension.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,7 @@ class ScriptInstanceExtension : public ScriptInstance {
711711

712712
GDExtensionScriptInstanceDataPtr instance = nullptr;
713713

714-
// There should not be warnings on explicit casts.
715-
#if defined(__GNUC__) && !defined(__clang__)
716-
#pragma GCC diagnostic push
717-
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
718-
#endif
714+
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wignored-qualifiers") // There should not be warnings on explicit casts.
719715

720716
virtual bool set(const StringName &p_name, const Variant &p_value) override {
721717
if (native_info->set_func) {
@@ -963,7 +959,5 @@ class ScriptInstanceExtension : public ScriptInstance {
963959
#endif // DISABLE_DEPRECATED
964960
}
965961

966-
#if defined(__GNUC__) && !defined(__clang__)
967-
#pragma GCC diagnostic pop
968-
#endif
962+
GODOT_GCC_WARNING_POP
969963
};

core/os/safe_binary_mutex.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636

3737
#ifdef THREADS_ENABLED
3838

39-
#ifdef __clang__
40-
#pragma clang diagnostic push
41-
#pragma clang diagnostic ignored "-Wundefined-var-template"
42-
#endif
39+
GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wundefined-var-template")
4340

4441
// A very special kind of mutex, used in scenarios where these
4542
// requirements hold at the same time:
@@ -119,9 +116,7 @@ class MutexLock<SafeBinaryMutex<Tag>> {
119116
// TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
120117
};
121118

122-
#ifdef __clang__
123-
#pragma clang diagnostic pop
124-
#endif
119+
GODOT_CLANG_WARNING_POP
125120

126121
#else // No threads.
127122

core/os/thread.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,9 @@ class Thread {
9292
};
9393

9494
#if defined(__cpp_lib_hardware_interference_size) && !defined(ANDROID_ENABLED) // This would be OK with NDK >= 26.
95-
#if defined(__GNUC__) && !defined(__clang__)
96-
#pragma GCC diagnostic push
97-
#pragma GCC diagnostic ignored "-Winterference-size"
98-
#endif
95+
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size")
9996
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
100-
#if defined(__GNUC__) && !defined(__clang__)
101-
#pragma GCC diagnostic pop
102-
#endif
97+
GODOT_GCC_WARNING_POP
10398
#else
10499
// At a negligible memory cost, we use a conservatively high value.
105100
static constexpr size_t CACHE_LINE_BYTES = 128;

core/templates/cowdata.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ class VMap;
4949

5050
static_assert(std::is_trivially_destructible_v<std::atomic<uint64_t>>);
5151

52-
// Silence a false positive warning (see GH-52119).
53-
#if defined(__GNUC__) && !defined(__clang__)
54-
#pragma GCC diagnostic push
55-
#pragma GCC diagnostic ignored "-Wplacement-new"
56-
#endif
52+
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wplacement-new") // Silence a false positive warning (see GH-52119).
5753

5854
template <typename T>
5955
class CowData {
@@ -460,9 +456,7 @@ CowData<T>::CowData(std::initializer_list<T> p_init) {
460456
}
461457
}
462458

463-
#if defined(__GNUC__) && !defined(__clang__)
464-
#pragma GCC diagnostic pop
465-
#endif
459+
GODOT_GCC_WARNING_POP
466460

467461
// Zero-constructing CowData initializes _ptr to nullptr (and thus empty).
468462
template <typename T>

core/templates/lru.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@
3333
#include "hash_map.h"
3434
#include "list.h"
3535

36-
#if defined(__GNUC__) && !defined(__clang__)
37-
#define ADDRESS_DIAGNOSTIC_WARNING_DISABLE \
38-
_Pragma("GCC diagnostic push"); \
39-
_Pragma("GCC diagnostic ignored \"-Waddress\"");
40-
41-
#define ADDRESS_DIAGNOSTIC_POP \
42-
_Pragma("GCC diagnostic pop");
43-
#else
44-
#define ADDRESS_DIAGNOSTIC_WARNING_DISABLE
45-
#define ADDRESS_DIAGNOSTIC_POP
46-
#endif
47-
4836
template <typename TKey, typename TData, typename Hasher = HashMapHasherDefault, typename Comparator = HashMapComparatorDefault<TKey>, void (*BeforeEvict)(TKey &, TData &) = nullptr>
4937
class LRUCache {
5038
public:
@@ -72,23 +60,23 @@ class LRUCache {
7260
Element n = _list.push_front(Pair(p_key, p_value));
7361

7462
if (e) {
75-
ADDRESS_DIAGNOSTIC_WARNING_DISABLE;
63+
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress")
7664
if constexpr (BeforeEvict != nullptr) {
7765
BeforeEvict((*e)->get().key, (*e)->get().data);
7866
}
79-
ADDRESS_DIAGNOSTIC_POP;
67+
GODOT_GCC_WARNING_POP
8068
_list.erase(*e);
8169
_map.erase(p_key);
8270
}
8371
_map[p_key] = _list.front();
8472

8573
while (_map.size() > capacity) {
8674
Element d = _list.back();
87-
ADDRESS_DIAGNOSTIC_WARNING_DISABLE
75+
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress")
8876
if constexpr (BeforeEvict != nullptr) {
8977
BeforeEvict(d->get().key, d->get().data);
9078
}
91-
ADDRESS_DIAGNOSTIC_POP
79+
GODOT_GCC_WARNING_POP
9280
_map.erase(d->get().key);
9381
_list.pop_back();
9482
}
@@ -141,11 +129,11 @@ class LRUCache {
141129
capacity = p_capacity;
142130
while (_map.size() > capacity) {
143131
Element d = _list.back();
144-
ADDRESS_DIAGNOSTIC_WARNING_DISABLE;
132+
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress")
145133
if constexpr (BeforeEvict != nullptr) {
146134
BeforeEvict(d->get().key, d->get().data);
147135
}
148-
ADDRESS_DIAGNOSTIC_POP;
136+
GODOT_GCC_WARNING_POP
149137
_map.erase(d->get().key);
150138
_list.pop_back();
151139
}
@@ -160,6 +148,3 @@ class LRUCache {
160148
capacity = p_capacity;
161149
}
162150
};
163-
164-
#undef ADDRESS_DIAGNOSTIC_WARNING_DISABLE
165-
#undef ADDRESS_DIAGNOSTIC_POP

core/typedefs.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,46 @@ struct is_zero_constructible<const volatile T> : is_zero_constructible<T> {};
344344

345345
template <typename T>
346346
inline constexpr bool is_zero_constructible_v = is_zero_constructible<T>::value;
347+
348+
// Warning suppression helper macros.
349+
#if defined(__clang__)
350+
#define GODOT_CLANG_PRAGMA(m_content) _Pragma(#m_content)
351+
#define GODOT_CLANG_WARNING_PUSH GODOT_CLANG_PRAGMA(clang diagnostic push)
352+
#define GODOT_CLANG_WARNING_IGNORE(m_warning) GODOT_CLANG_PRAGMA(clang diagnostic ignored m_warning)
353+
#define GODOT_CLANG_WARNING_POP GODOT_CLANG_PRAGMA(clang diagnostic pop)
354+
#define GODOT_CLANG_WARNING_PUSH_AND_IGNORE(m_warning) GODOT_CLANG_WARNING_PUSH GODOT_CLANG_WARNING_IGNORE(m_warning)
355+
#else
356+
#define GODOT_CLANG_PRAGMA(m_content)
357+
#define GODOT_CLANG_WARNING_PUSH
358+
#define GODOT_CLANG_WARNING_IGNORE(m_warning)
359+
#define GODOT_CLANG_WARNING_POP
360+
#define GODOT_CLANG_WARNING_PUSH_AND_IGNORE(m_warning)
361+
#endif
362+
363+
#if defined(__GNUC__) && !defined(__clang__)
364+
#define GODOT_GCC_PRAGMA(m_content) _Pragma(#m_content)
365+
#define GODOT_GCC_WARNING_PUSH GODOT_GCC_PRAGMA(GCC diagnostic push)
366+
#define GODOT_GCC_WARNING_IGNORE(m_warning) GODOT_GCC_PRAGMA(GCC diagnostic ignored m_warning)
367+
#define GODOT_GCC_WARNING_POP GODOT_GCC_PRAGMA(GCC diagnostic pop)
368+
#define GODOT_GCC_WARNING_PUSH_AND_IGNORE(m_warning) GODOT_GCC_WARNING_PUSH GODOT_GCC_WARNING_IGNORE(m_warning)
369+
#else
370+
#define GODOT_GCC_PRAGMA(m_content)
371+
#define GODOT_GCC_WARNING_PUSH
372+
#define GODOT_GCC_WARNING_IGNORE(m_warning)
373+
#define GODOT_GCC_WARNING_POP
374+
#define GODOT_GCC_WARNING_PUSH_AND_IGNORE(m_warning)
375+
#endif
376+
377+
#if defined(_MSC_VER) && !defined(__clang__)
378+
#define GODOT_MSVC_PRAGMA(m_command) __pragma(m_command)
379+
#define GODOT_MSVC_WARNING_PUSH GODOT_MSVC_PRAGMA(warning(push))
380+
#define GODOT_MSVC_WARNING_IGNORE(m_warning) GODOT_MSVC_PRAGMA(warning(disable : m_warning))
381+
#define GODOT_MSVC_WARNING_POP GODOT_MSVC_PRAGMA(warning(pop))
382+
#define GODOT_MSVC_WARNING_PUSH_AND_IGNORE(m_warning) GODOT_MSVC_WARNING_PUSH GODOT_MSVC_WARNING_IGNORE(m_warning)
383+
#else
384+
#define GODOT_MSVC_PRAGMA(m_command)
385+
#define GODOT_MSVC_WARNING_PUSH
386+
#define GODOT_MSVC_WARNING_IGNORE(m_warning)
387+
#define GODOT_MSVC_WARNING_POP
388+
#define GODOT_MSVC_WARNING_PUSH_AND_IGNORE(m_warning)
389+
#endif

core/variant/binder_common.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,7 @@ void call_with_validated_object_instance_args_static_retc(T *base, R (*p_method)
689689

690690
// GCC raises "parameter 'p_args' set but not used" when P = {},
691691
// it's not clever enough to treat other P values as making this branch valid.
692-
#if defined(__GNUC__) && !defined(__clang__)
693-
#pragma GCC diagnostic push
694-
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
695-
#endif
692+
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wunused-but-set-parameter")
696693

697694
template <typename Q>
698695
void call_get_argument_type_helper(int p_arg, int &index, Variant::Type &type) {
@@ -1033,6 +1030,4 @@ void call_with_variant_args_static_dv(void (*p_method)(P...), const Variant **p_
10331030
call_with_variant_args_static(p_method, args, r_error, BuildIndexSequence<sizeof...(P)>{});
10341031
}
10351032

1036-
#if defined(__GNUC__) && !defined(__clang__)
1037-
#pragma GCC diagnostic pop
1038-
#endif
1033+
GODOT_GCC_WARNING_POP

0 commit comments

Comments
 (0)