Skip to content

Commit

Permalink
Merge pull request #56999 from lawnjelly/no_discard
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Jan 20, 2022
2 parents 39e93a6 + adf14bf commit 08cabf2
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 26 deletions.
2 changes: 1 addition & 1 deletion core/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "core/math/math_funcs.h"
#include "core/ustring.h"

struct Color {
struct _NO_DISCARD_CLASS_ Color {
union {
struct {
float r;
Expand Down
2 changes: 1 addition & 1 deletion core/math/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* This is implemented by a point (position) and the box size
*/

class AABB {
class _NO_DISCARD_CLASS_ AABB {
public:
Vector3 position;
Vector3 size;
Expand Down
2 changes: 1 addition & 1 deletion core/math/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "core/math/quat.h"
#include "core/math/vector3.h"

class Basis {
class _NO_DISCARD_CLASS_ Basis {
public:
Vector3 elements[3] = {
Vector3(1, 0, 0),
Expand Down
2 changes: 1 addition & 1 deletion core/math/face3.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "core/math/transform.h"
#include "core/math/vector3.h"

class Face3 {
class _NO_DISCARD_CLASS_ Face3 {
public:
enum Side {
SIDE_OVER,
Expand Down
2 changes: 1 addition & 1 deletion core/math/plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#include "core/math/vector3.h"

class Plane {
class _NO_DISCARD_CLASS_ Plane {
public:
Vector3 normal;
real_t d;
Expand Down
4 changes: 2 additions & 2 deletions core/math/quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "core/math/vector3.h"
#include "core/ustring.h"

class Quat {
class _NO_DISCARD_CLASS_ Quat {
public:
real_t x, y, z, w;

Expand Down Expand Up @@ -127,7 +127,7 @@ class Quat {
w(p_q.w) {
}

Quat operator=(const Quat &p_q) {
Quat &operator=(const Quat &p_q) {
x = p_q.x;
y = p_q.y;
z = p_q.z;
Expand Down
4 changes: 2 additions & 2 deletions core/math/rect2.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

struct Transform2D;

struct Rect2 {
struct _NO_DISCARD_CLASS_ Rect2 {
Point2 position;
Size2 size;

Expand Down Expand Up @@ -259,7 +259,7 @@ struct Rect2 {
}
};

struct Rect2i {
struct _NO_DISCARD_CLASS_ Rect2i {
Point2i position;
Size2i size;

Expand Down
2 changes: 1 addition & 1 deletion core/math/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "core/math/plane.h"
#include "core/pool_vector.h"

class Transform {
class _NO_DISCARD_CLASS_ Transform {
public:
Basis basis;
Vector3 origin;
Expand Down
2 changes: 1 addition & 1 deletion core/math/transform_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "core/math/rect2.h" // also includes vector2, math_funcs, and ustring
#include "core/pool_vector.h"

struct Transform2D {
struct _NO_DISCARD_CLASS_ Transform2D {
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
// M = (elements[0][0] elements[1][0])
// (elements[0][1] elements[1][1])
Expand Down
4 changes: 2 additions & 2 deletions core/math/vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

struct Vector2i;

struct Vector2 {
struct _NO_DISCARD_CLASS_ Vector2 {
static const int AXIS_COUNT = 2;

enum Axis {
Expand Down Expand Up @@ -269,7 +269,7 @@ typedef Vector2 Point2;

/* INTEGER STUFF */

struct Vector2i {
struct _NO_DISCARD_CLASS_ Vector2i {
enum Axis {
AXIS_X,
AXIS_Y,
Expand Down
2 changes: 1 addition & 1 deletion core/math/vector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

class Basis;

struct Vector3 {
struct _NO_DISCARD_CLASS_ Vector3 {
static const int AXIS_COUNT = 3;

enum Axis {
Expand Down
41 changes: 41 additions & 0 deletions core/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,47 @@

#endif

// No discard allows the compiler to flag warnings if we don't use the return value of functions / classes
#ifndef _NO_DISCARD_
// c++ 17 onwards
#if __cplusplus >= 201703L
#define _NO_DISCARD_ [[nodiscard]]
#else
// __warn_unused_result__ supported on clang and GCC
#if (defined(__clang__) || defined(__GNUC__)) && defined(__has_attribute)
#if __has_attribute(__warn_unused_result__)
#define _NO_DISCARD_ __attribute__((__warn_unused_result__))
#endif
#endif

// Visual Studio 2012 onwards
#if _MSC_VER >= 1700
#define _NO_DISCARD_ _Check_return_
#endif

// If nothing supported, just noop the macro
#ifndef _NO_DISCARD_
#define _NO_DISCARD_
#endif
#endif // not c++ 17
#endif // not defined _NO_DISCARD_

// In some cases _NO_DISCARD_ will get false positives,
// we can prevent the warning in specific cases by preceding the call with a cast.
#ifndef _ALLOW_DISCARD_
#define _ALLOW_DISCARD_ (void)
#endif

// GCC (prior to c++ 17) Does not seem to support no discard with classes, only functions.
// So we will use a specific macro for classes.
#ifndef _NO_DISCARD_CLASS_
#if (defined(__clang__) || defined(_MSC_VER))
#define _NO_DISCARD_CLASS_ _NO_DISCARD_
#else
#define _NO_DISCARD_CLASS_
#endif
#endif

//custom, gcc-safe offsetof, because gcc complains a lot.
template <class T>
T *_nullptr() {
Expand Down
4 changes: 2 additions & 2 deletions modules/bullet/cone_twist_joint_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
ConeTwistJointBullet::ConeTwistJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &rbAFrame, const Transform &rbBFrame) :
JointBullet() {
Transform scaled_AFrame(rbAFrame.scaled(rbA->get_body_scale()));
scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
_ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);

btTransform btFrameA;
G_TO_B(scaled_AFrame, btFrameA);

if (rbB) {
Transform scaled_BFrame(rbBFrame.scaled(rbB->get_body_scale()));
scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
_ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);

btTransform btFrameB;
G_TO_B(scaled_BFrame, btFrameB);
Expand Down
4 changes: 2 additions & 2 deletions modules/bullet/generic_6dof_joint_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ Generic6DOFJointBullet::Generic6DOFJointBullet(RigidBodyBullet *rbA, RigidBodyBu
JointBullet() {
Transform scaled_AFrame(frameInA.scaled(rbA->get_body_scale()));

scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
_ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);

btTransform btFrameA;
G_TO_B(scaled_AFrame, btFrameA);

if (rbB) {
Transform scaled_BFrame(frameInB.scaled(rbB->get_body_scale()));

scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
_ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);

btTransform btFrameB;
G_TO_B(scaled_BFrame, btFrameB);
Expand Down
4 changes: 2 additions & 2 deletions modules/bullet/hinge_joint_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
HingeJointBullet::HingeJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &frameA, const Transform &frameB) :
JointBullet() {
Transform scaled_AFrame(frameA.scaled(rbA->get_body_scale()));
scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
_ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);

btTransform btFrameA;
G_TO_B(scaled_AFrame, btFrameA);

if (rbB) {
Transform scaled_BFrame(frameB.scaled(rbB->get_body_scale()));
scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
_ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);

btTransform btFrameB;
G_TO_B(scaled_BFrame, btFrameB);
Expand Down
4 changes: 2 additions & 2 deletions modules/bullet/slider_joint_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
SliderJointBullet::SliderJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &frameInA, const Transform &frameInB) :
JointBullet() {
Transform scaled_AFrame(frameInA.scaled(rbA->get_body_scale()));
scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);
_ALLOW_DISCARD_ scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis);

btTransform btFrameA;
G_TO_B(scaled_AFrame, btFrameA);

if (rbB) {
Transform scaled_BFrame(frameInB.scaled(rbB->get_body_scale()));
scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);
_ALLOW_DISCARD_ scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis);

btTransform btFrameB;
G_TO_B(scaled_BFrame, btFrameB);
Expand Down
6 changes: 3 additions & 3 deletions platform/osx/os_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ - (void)windowDidMove:(NSNotification *)notification {

- (void)windowDidBecomeKey:(NSNotification *)notification {
if (OS_OSX::singleton->get_main_loop()) {
get_mouse_pos([OS_OSX::singleton->window_object mouseLocationOutsideOfEventStream]);
_ALLOW_DISCARD_ get_mouse_pos([OS_OSX::singleton->window_object mouseLocationOutsideOfEventStream]);
OS_OSX::singleton->input->set_mouse_position(Point2(mouse_x, mouse_y));

OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
Expand Down Expand Up @@ -1370,7 +1370,7 @@ inline void sendPanEvent(double dx, double dy, int modifierFlags) {
- (void)scrollWheel:(NSEvent *)event {
double deltaX, deltaY;

get_mouse_pos([event locationInWindow]);
_ALLOW_DISCARD_ get_mouse_pos([event locationInWindow]);

deltaX = [event scrollingDeltaX];
deltaY = [event scrollingDeltaY];
Expand Down Expand Up @@ -2167,7 +2167,7 @@ virtual void log_error(const char *p_function, const char *p_file, int p_line, c
}

void OS_OSX::update_real_mouse_position() {
get_mouse_pos([window_object mouseLocationOutsideOfEventStream]);
_ALLOW_DISCARD_ get_mouse_pos([window_object mouseLocationOutsideOfEventStream]);
input->set_mouse_position(Point2(mouse_x, mouse_y));
}

Expand Down
2 changes: 1 addition & 1 deletion scene/2d/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ void CanvasItem::set_notify_transform(bool p_enable) {

if (notify_transform && is_inside_tree()) {
//this ensures that invalid globals get resolved, so notifications can be received
get_global_transform();
_ALLOW_DISCARD_ get_global_transform();
}
}

Expand Down

0 comments on commit 08cabf2

Please sign in to comment.