Respect NDEBUG for asserts even though Ruby 3+ pollutes it.

Prior to this CL, asserts have no effect for Ruby 3+, because Ruby unconditionally defines `NDEBUG`: https://bugs.ruby-lang.org/issues/18777

We work around this by doing `#undef NDEBUG` right after including Ruby, if `NDEBUG` was not previously defined.

PiperOrigin-RevId: 535359088
pull/12915/head
Joshua Haberman 2 years ago committed by Copybara-Service
parent 5101674194
commit b2a520f693
  1. 2
      ruby/ext/google/protobuf_c/convert.h
  2. 2
      ruby/ext/google/protobuf_c/defs.h
  3. 2
      ruby/ext/google/protobuf_c/map.h
  4. 2
      ruby/ext/google/protobuf_c/message.h
  5. 14
      ruby/ext/google/protobuf_c/protobuf.h
  6. 2
      ruby/ext/google/protobuf_c/repeated_field.c
  7. 2
      ruby/ext/google/protobuf_c/repeated_field.h

@ -31,8 +31,6 @@
#ifndef RUBY_PROTOBUF_CONVERT_H_
#define RUBY_PROTOBUF_CONVERT_H_
#include <ruby/ruby.h>
#include "protobuf.h"
#include "ruby-upb.h"

@ -31,8 +31,6 @@
#ifndef RUBY_PROTOBUF_DEFS_H_
#define RUBY_PROTOBUF_DEFS_H_
#include <ruby/ruby.h>
#include "protobuf.h"
#include "ruby-upb.h"

@ -31,8 +31,6 @@
#ifndef RUBY_PROTOBUF_MAP_H_
#define RUBY_PROTOBUF_MAP_H_
#include <ruby/ruby.h>
#include "protobuf.h"
#include "ruby-upb.h"

@ -31,8 +31,6 @@
#ifndef RUBY_PROTOBUF_MESSAGE_H_
#define RUBY_PROTOBUF_MESSAGE_H_
#include <ruby/ruby.h>
#include "protobuf.h"
#include "ruby-upb.h"

@ -31,8 +31,16 @@
#ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
#define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
// Ruby 3+ defines NDEBUG itself, see: https://bugs.ruby-lang.org/issues/18777
#ifdef NDEBUG
#include <ruby.h>
#else
#include <ruby.h>
#undef NDEBUG
#endif
#include <assert.h> // Must be included after the NDEBUG logic above.
#include <ruby/encoding.h>
#include <ruby/ruby.h>
#include <ruby/vm.h>
#include "defs.h"
@ -110,7 +118,9 @@ extern VALUE cTypeError;
do { \
} while (false && (expr))
#else
#define PBRUBY_ASSERT(expr) assert(expr)
#define PBRUBY_ASSERT(expr) \
if (!(expr)) \
rb_bug("Assertion failed at %s:%d, expr: %s", __FILE__, __LINE__, #expr)
#endif
#define PBRUBY_MAX(x, y) (((x) > (y)) ? (x) : (y))

@ -100,6 +100,8 @@ VALUE RepeatedField_GetRubyWrapper(upb_Array* array, TypeInfo type_info,
PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.type == type_info.type);
PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.def.msgdef ==
type_info.def.msgdef);
PBRUBY_ASSERT(ruby_to_RepeatedField(val)->array == array);
return val;
}

@ -31,8 +31,6 @@
#ifndef RUBY_PROTOBUF_REPEATED_FIELD_H_
#define RUBY_PROTOBUF_REPEATED_FIELD_H_
#include <ruby/ruby.h>
#include "protobuf.h"
#include "ruby-upb.h"

Loading…
Cancel
Save