Skip to content

Commit d83536c

Browse files
committed
reduce copy & paste
We see several occurrence of "diagnostic push/pop" so why not make them macros. Tested on GCC8 / Clang 6. Note that ruby.h is intentionally left untouched because we don't want to introduce new public macros. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64118 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 7018acc commit d83536c

File tree

7 files changed

+61
-38
lines changed

7 files changed

+61
-38
lines changed

cont.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,9 @@ show_vm_pcs(const rb_control_frame_t *cfp,
628628
}
629629
}
630630
#endif
631+
COMPILER_WARNING_PUSH
631632
#ifdef __clang__
632-
#pragma clang diagnostic push
633-
#pragma clang diagnostic ignored "-Wduplicate-decl-specifier"
633+
COMPILER_WARNING_IGNORED(-Wduplicate-decl-specifier)
634634
#endif
635635
static VALUE
636636
cont_capture(volatile int *volatile stat)
@@ -694,9 +694,7 @@ cont_capture(volatile int *volatile stat)
694694
return contval;
695695
}
696696
}
697-
#ifdef __clang__
698-
#pragma clang diagnostic pop
699-
#endif
697+
COMPILER_WARNING_POP
700698

701699
static inline void
702700
fiber_restore_thread(rb_thread_t *th, rb_fiber_t *fib)

dln.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -1244,24 +1244,17 @@ rb_w32_check_imported(HMODULE ext, HMODULE mine)
12441244
#endif
12451245

12461246
#ifdef USE_DLN_DLOPEN
1247-
#ifdef __clang__
1248-
#pragma clang diagnostic push
1249-
#pragma clang diagnostic ignored "-Wpedantic"
1250-
#elif defined(__GNUC__) && (__GNUC__ >= 5)
1251-
#pragma GCC diagnostic push
1252-
#pragma GCC diagnostic ignored "-Wpedantic"
1247+
COMPILER_WARNING_PUSH
1248+
#if defined(__clang__) || GCC_VERSION_SINCE(4, 2, 0)
1249+
COMPILER_WARNING_IGNORED(-Wpedantic)
12531250
#endif
12541251
static bool
12551252
dln_incompatible_library_p(void *handle)
12561253
{
12571254
void *ex = dlsym(handle, EXTERNAL_PREFIX"ruby_xmalloc");
12581255
return ex && ex != ruby_xmalloc;
12591256
}
1260-
#ifdef __clang__
1261-
#pragma clang diagnostic pop
1262-
#elif defined(__GNUC__) && (__GNUC__ >= 5)
1263-
#pragma GCC diagnostic pop
1264-
#endif
1257+
COMPILER_WARNING_POP
12651258
#endif
12661259

12671260
void*

eval_intern.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ LONG WINAPI rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *);
160160
#if defined(USE_UNALIGNED_MEMBER_ACCESS) && USE_UNALIGNED_MEMBER_ACCESS && \
161161
defined(__clang__)
162162
# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
163-
_Pragma("GCC diagnostic push"); \
164-
_Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\""); \
163+
COMPILER_WARNING_PUSH; \
164+
COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
165165
typeof(expr) unaligned_member_access_result = (expr); \
166-
_Pragma("GCC diagnostic pop"); \
166+
COMPILER_WARNING_POP; \
167167
unaligned_member_access_result; \
168168
})
169169
#else

internal.h

+37
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,43 @@ rb_obj_builtin_type(VALUE obj)
21472147
# define BITFIELD(type) unsigned int
21482148
#endif
21492149

2150+
#if defined(_MSC_VER)
2151+
# define COMPILER_WARNING_PUSH __pragma(warning(push))
2152+
# define COMPILER_WARNING_POP __pragma(warning(pop))
2153+
# define COMPILER_WARNING_ERROR(flag) __pragma(warning(error: flag)))
2154+
# define COMPILER_WARNING_IGNORED(flag) __pragma(warning(suppress: flag)))
2155+
2156+
#elif defined(__clang__) /* clang 2.6 already had this feature */
2157+
# define COMPILER_WARNING_PUSH _Pragma("clang diagnostic push")
2158+
# define COMPILER_WARNING_POP _Pragma("clang diagnostic pop")
2159+
# define COMPILER_WARNING_SPECIFIER(kind, msg) \
2160+
clang diagnostic kind # msg
2161+
# define COMPILER_WARNING_ERROR(flag) \
2162+
COMPILER_WARNING_PRAGMA(COMPILER_WARNING_SPECIFIER(error, flag))
2163+
# define COMPILER_WARNING_IGNORED(flag) \
2164+
COMPILER_WARNING_PRAGMA(COMPILER_WARNING_SPECIFIER(ignored, flag))
2165+
2166+
#elif GCC_VERSION_SINCE(4, 2, 0)
2167+
/* https://gcc.gnu.org/onlinedocs/gcc-4.2.0/gcc/Diagnostic-Pragmas.html */
2168+
# define COMPILER_WARNING_PUSH _Pragma("GCC diagnostic push")
2169+
# define COMPILER_WARNING_POP _Pragma("GCC diagnostic pop")
2170+
# define COMPILER_WARNING_SPECIFIER(kind, msg) \
2171+
GCC diagnostic kind # msg
2172+
# define COMPILER_WARNING_ERROR(flag) \
2173+
COMPILER_WARNING_PRAGMA(COMPILER_WARNING_SPECIFIER(error, flag))
2174+
# define COMPILER_WARNING_IGNORED(flag) \
2175+
COMPILER_WARNING_PRAGMA(COMPILER_WARNING_SPECIFIER(ignored, flag))
2176+
2177+
#else /* other compilers to follow? */
2178+
# define COMPILER_WARNING_PUSH /* nop */
2179+
# define COMPILER_WARNING_POP /* nop */
2180+
# define COMPILER_WARNING_ERROR(cond, flag) /* nop */
2181+
# define COMPILER_WARNING_ignored(cond, flag) /* nop */
2182+
#endif
2183+
2184+
#define COMPILER_WARNING_PRAGMA(str) COMPILER_WARNING_PRAGMA_(str)
2185+
#define COMPILER_WARNING_PRAGMA_(str) _Pragma(#str)
2186+
21502187
#if defined(__cplusplus)
21512188
#if 0
21522189
{ /* satisfy cc-mode */

mjit.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,13 @@ start_process(const char *path, char *const *argv)
387387
}
388388
dev_null = rb_cloexec_open(ruby_null_device, O_WRONLY, 0);
389389

390+
COMPILER_WARNING_PUSH;
390391
#ifdef __GNUC__
391-
# pragma GCC diagnostic push
392-
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
392+
COMPILER_WARNING_IGNORED(-Wdeprecated-declarations);
393393
#endif
394394
if ((pid = vfork()) == 0) {
395-
#ifdef __GNUC__
396-
# pragma GCC diagnostic pop
397-
#endif
395+
COMPILER_WARNING_POP;
396+
398397
umask(0077);
399398
if (mjit_opts.verbose == 0) {
400399
/* CC can be started in a thread using a file which has been

process.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -3972,9 +3972,9 @@ retry_fork_async_signal_safe(int *status, int *ep,
39723972
while (1) {
39733973
prefork();
39743974
disable_child_handler_before_fork(&old);
3975+
COMPILER_WARNING_PUSH;
39753976
#ifdef __GNUC__
3976-
# pragma GCC diagnostic push
3977-
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
3977+
COMPILER_WARNING_IGNORED(-Wdeprecated-declarations);
39783978
#endif
39793979
#ifdef HAVE_WORKING_VFORK
39803980
if (!has_privilege())
@@ -3984,9 +3984,7 @@ retry_fork_async_signal_safe(int *status, int *ep,
39843984
#else
39853985
pid = fork();
39863986
#endif
3987-
#ifdef __GNUC__
3988-
# pragma GCC diagnostic pop
3989-
#endif
3987+
COMPILER_WARNING_POP;
39903988
if (pid == 0) {/* fork succeed, child process */
39913989
int ret;
39923990
close(ep[0]);
@@ -4055,13 +4053,13 @@ rb_fork_ruby(int *status)
40554053
prefork();
40564054
disable_child_handler_before_fork(&old);
40574055
before_fork_ruby();
4056+
COMPILER_WARNING_PUSH;
40584057
#ifdef __GNUC__
4059-
# pragma GCC diagnostic push
4060-
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4058+
COMPILER_WARNING_IGNORED(-Wdeprecated-declarations);
40614059
#endif
40624060
pid = fork();
40634061
#ifdef __GNUC__
4064-
# pragma GCC diagnostic pop
4062+
COMPILER_WARNING_POP;
40654063
#endif
40664064
err = errno;
40674065
after_fork_ruby();

template/prelude.c.tmpl

+4-6
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ prelude_prefix_path(VALUE self)
138138
% unless preludes.empty?
139139
#define PRELUDE_NAME(n) rb_usascii_str_new_static(prelude_name##n, sizeof(prelude_name##n)-1)
140140
#define PRELUDE_CODE(n) rb_usascii_str_new_static(prelude_code##n.L0, sizeof(prelude_code##n))
141-
#if defined __GNUC__ && __GNUC__ >= 5
142-
# pragma GCC diagnostic push
143-
# pragma GCC diagnostic error "-Wmissing-field-initializers"
141+
COMPILER_WARNING_PUSH
142+
#if GCC_VERSION_SINCE(4, 2, 0)
143+
COMPILER_WARNING_ERROR(-Wmissing-field-initializers)
144144
#endif
145145
static void
146146
prelude_eval(VALUE code, VALUE name, int line)
@@ -168,9 +168,7 @@ prelude_eval(VALUE code, VALUE name, int line)
168168
NULL, ISEQ_TYPE_TOP, &optimization));
169169
rb_ast_dispose(ast);
170170
}
171-
#if defined __GNUC__ && __GNUC__ >= 5
172-
# pragma GCC diagnostic pop
173-
#endif
171+
COMPILER_WARNING_POP
174172
% end
175173

176174
% if @have_sublib

0 commit comments

Comments
 (0)