Temporarily disable gcc warning on redefinition - gcc

I'm trying to make this work (in GCC 4.6) without barking at me.
#define FOO ""
#define BAR ""
#if ....
#define FOO "Foo, good sir"
#endif
#if ...
#define BAR "Bar, my lady"
#endif
....
#define EVERYTHING FOO BAR ...
I am going to have a lot of these. So doing it that way instead of:
#if ...
#define FOO "Foo"
#else
#define FOO ""
#endif
Saves a lot of code, and makes it more readable. The warning that I get is:
warning: "FOO" redefined [enabled by default]
Is there a way to disable this warning in the code for this particular section? I found Diagnostic Pragmas to disable certain warnings, but I'm not able to find which warning (in this list of Options to Request or Suppress Warnings) that needs to be disabled here.
Anyone know how to do this? Or a different way to avoid having to #else #define all of them to the empty string?

Try using #undef:
#define FOO ""
#if ....
#undef FOO
#define FOO "Foo, good sir"
#endif

This warning comes from file named "cccp.c" in gcc (as of 2.95 version; is this file from "Soviet Russia"?), and it can't be turned off. There is still no option to disable this warning individually even in git head, gcc/libcpp/macro.c file (line 2527 and line 2994 of the same file)
I'll cite sources a bit.
2525 /* Returns nonzero if a macro redefinition warning is required. */
2526 static bool
2527 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2528 const cpp_macro *macro2)
2529 {
...
2537 /* Suppress warnings for builtins that lack the NODE_WARN flag. */
..
2545 /* Redefinitions of conditional (context-sensitive) macros, on
2546 the other hand, must be allowed silently. */
...
2550 /* Redefinition of a macro is allowed if and only if the old and new
2551 definitions are the same. (6.10.3 paragraph 2). */
...
2561 /* Check parameter spellings. */
...
2566 /* Check the replacement text or tokens. */
...
2573 for (i = 0; i < macro1->count; i++)
2574 if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2575 return true;
So in your case warn_of_redefinition function will return true. And here is real usage:
2989 if (node->type == NT_MACRO)
2990 {
2991 if (CPP_OPTION (pfile, warn_unused_macros))
2992 _cpp_warn_if_unused_macro (pfile, node, NULL);
2993
2994 if (warn_of_redefinition (pfile, node, macro))
2995 {
2996 const int reason = (node->flags & NODE_BUILTIN)
2997 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
2998 bool warned;
2999
3000 warned = cpp_pedwarning_with_line (pfile, reason,
3001 pfile->directive_line, 0,
3002 "\"%s\" redefined",
3003 NODE_NAME (node));
3004
3005 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3006 cpp_error_with_line (pfile, CPP_DL_NOTE,
3007 node->value.macro->line, 0,
3008 "this is the location of the previous definition");
3009 }
3010 }
So, there is no any specific option. And answer by Greg is good for this case, just undefine your empty string just before redefinition.

Or try using if else.
#if ...
# define FOO "Foo, doof sir"
#else
# define FOO ""
#endif

Related

can't find the definition of __arm64_sys_ppoll function in linux source

I'm trying to boot a development board containing arm64 core using busybox, u-boot and linux-5.10.0-rc5. The boot process is almost complete but when it enters the shell program, it stops shortly after(with no kernel panic). It doesn't even show the '#' prompt (but with qemu model, the image and busybox works ok with normal shell at the end). I could see that before it stops, there are some system calls from the busybox coming to the kernel, and when it stopped, it was processing system call 73.
(You can follow from arch/arm64/kernel/syscall.c, do_el0_svc () -> el0_svc_common -> invoke_syscall -> __invoke_syscall -> syscall_fn
By examining the files I could see syscall 73 is sys_ppoll. (in include/uapi/asm-generic/unistd.h). I found in include/uapi/asm-generic/unistd.h,
/* fs/select.c */
#if defined(__ARCH_WANT_TIME32_SYSCALLS) || __BITS_PER_LONG != 32
#define __NR_pselect6 72
__SC_COMP_3264(__NR_pselect6, sys_pselect6_time32, sys_pselect6, compat_sys_pselect6_time32)
#define __NR_ppoll 73
__SC_COMP_3264(__NR_ppoll, sys_ppoll_time32, sys_ppoll, compat_sys_ppoll_time32)
#endif
The definition of __SC_COMP_3264 is at the first lines of the same file. To see what lines are selected and compiled by the #if/#endif macros, I tried adding a characters 'x' to cause compile error and I could see what lines are compiled. That is shown below.
#ifndef __SYSCALL
x <---- compile error, so compiled, and __SYSCALL(x,y) defined to be nothing?
#define __SYSCALL(x, y)
#endif
#if __BITS_PER_LONG == 32 || defined(__SYSCALL_COMPAT)
x <--------- no compile error, so not compiled
#define __SC_3264(_nr, _32, _64) __SYSCALL(_nr, _32)
#else
#define __SC_3264(_nr, _32, _64) __SYSCALL(_nr, _64)
#endif
#ifdef __SYSCALL_COMPAT
x <-------------- no compile error, so not compiled
#define __SC_COMP(_nr, _sys, _comp) __SYSCALL(_nr, _comp)
#define __SC_COMP_3264(_nr, _32, _64, _comp) __SYSCALL(_nr, _comp)
#else
#define __SC_COMP(_nr, _sys, _comp) __SYSCALL(_nr, _sys)
#define __SC_COMP_3264(_nr, _32, _64, _comp) __SC_3264(_nr, _32, _64)
#endif
So this means __SYSCALL(x, y) is defined to be doing nothing. But if that was true, all the other syscall would have done nothing and I figured __SYSCALL was defined previously and found in arch/arm64/kernel/sys.c
#undef __SYSCALL
#define __SYSCALL(nr, sym) asmlinkage long __arm64_##sym(const struct pt_regs *);
#include <asm/unistd.h>
So the function definition becomes __arm64_sys_ppoll and I can see it in the System.map file.
But I couldn't find the definition of __arm64_sys_ppoll. Where can I find the source? My another question is, how can below line be compiled and make error when I do make -j28?
#ifndef __SYSCALL
x <---- compile error, so compiled, and __SYSCALL(x,y) defined to be nothing?
#define __SYSCALL(x, y)
#endif
By the way, this is what I see when I grep for sys_ppoll in the source(excluding all non-arm64 arch files).
./include/linux/compat.h:asmlinkage long compat_sys_ppoll_time32(struct pollfd __user *ufds,
./include/linux/compat.h:asmlinkage long compat_sys_ppoll_time64(struct pollfd __user *ufds,
./include/linux/syscalls.h:asmlinkage long sys_ppoll(struct pollfd __user *, unsigned int,
./include/linux/syscalls.h:asmlinkage long sys_ppoll_time32(struct pollfd __user *, unsigned int,
./include/uapi/asm-generic/unistd.h:__SC_COMP_3264(__NR_ppoll, sys_ppoll_time32, sys_ppoll, compat_sys_ppoll_time32)
./include/uapi/asm-generic/unistd.h:__SC_COMP(__NR_ppoll_time64, sys_ppoll, compat_sys_ppoll_time64)
./tools/include/uapi/asm-generic/unistd.h:__SC_COMP_3264(__NR_ppoll, sys_ppoll_time32, sys_ppoll, compat_sys_ppoll_time32)
./tools/include/uapi/asm-generic/unistd.h:__SC_COMP(__NR_ppoll_time64, sys_ppoll, compat_sys_ppoll_time64)
./arch/arm64/include/asm/unistd32.h:__SYSCALL(__NR_ppoll, compat_sys_ppoll_time32)
./arch/arm64/include/asm/unistd32.h:__SYSCALL(__NR_ppoll_time64, compat_sys_ppoll_time64)
Thanks for reading and sorry for the long question.
For SYSCALL_DEFINE0, ..., SYSCALL_DEFINE6, first see #Ian Abbott's comment to my original question.
In file include/uapi/asm-generic/unistd.h, you can see the syscall definitions.
For example if you want to see the source code for shmat, you can use grep to see in the file these lines,
/* ipc/shm.c */
#define __NR_shmget 194
__SYSCALL(__NR_shmget, sys_shmget)
#define __NR_shmctl 195
__SC_COMP(__NR_shmctl, sys_shmctl, compat_sys_shmctl)
#define __NR_shmat 196
__SC_COMP(__NR_shmat, sys_shmat, compat_sys_shmat)
#define __NR_shmdt 197
__SYSCALL(__NR_shmdt, sys_shmdt)
So it says the definition is in file ipc/shm.c. There you can see these lines.
SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
{
unsigned long ret;
long err;
err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
if (err)
return err;
force_successful_syscall_return();
return (long)ret;
}
You can see the definition of shmat function with argments list.

What is gcc option to ignore unused condition

#if !__STDC__ && (_MSC_VER <= 1000) && !defined(__BORLANDC__)
/* For backward compatibility */
typedef VARIANT_BOOL _VARIANT_BOOL;
#else
/* ANSI C/C++ reserve bool as keyword */
#define _VARIANT_BOOL /##/
#endif
This code is copied from a header file and #if block is active and #else block is disabled but gcc doesn't ignore inactive block.
....\wtypes.h|1071|error: pasting "/" and "/" does not give a valid preprocessing token
I know what gcc is saying but I don't want to modify the code for different compilers. The code MUST REMAIN UNTOUCHED.
How can I tell gcc to ignore unused code?

gcc: -Dvar=value not working as expected

I am trying to compile a library that has these lines:
#if AE_OS==AE_WINDOWS
#include windows.h
//stuff
#elif AE_OS==AE_POSIX
//other stuff
#endif
When I use cpp -DAE_OS=AE_POSIX I get
cpp/src/ap.cpp:63:21: fatal error: windows.h: No such file or directory
#include <windows.h>
^
Adding a space after the -D does not work, nor does putting AE_OS=AE_POSIX in single or double quotes, putting only AE_POSIX in single or double quotes, and trying all of these combinations with the quotes escaped.
Putting AE_POSIX in escaped single quotes at least does something different, but it is apparently still not correct:
cpp/src/ap.cpp:59:5: warning: character constant too long for its type [enabled by default]
#if AE_OS==AE_WINDOWS
^
I have also tried -D"AE_OS AE_POSIX" thinking that would be the same as #define "AE_OS AE_POSIX" but apparently it ignores the quotes since it defines AE_OS as 1:
<command-line>:0:16: error: missing binary operator before token "1"
cpp/src/ap.cpp:65:7: note: in expansion of macro ‘AE_OS’
#elif AE_OS==AE_POSIX
What is the correct way to do this?
The preprocessor understands expressions of integer type, not strings.
AE_POSIX and AE_WINDOWS are defined as different integers:
/*
* definitions
*/
#define AE_UNKNOWN 0
#define AE_MSVC 1
#define AE_GNUC 2
#define AE_SUNC 3
#define AE_INTEL 1
#define AE_SPARC 2
#define AE_WINDOWS 1
#define AE_POSIX 2
So for POSIX do this:
cpp -DAE_OS=2

"elif" in gcc: #elif with no expression error

I'm trying to do something like this:
#define RELEASE_TEST
#if RELEASE
// code1
#elif RELEASE_TEST
// code2
#else
// code3
#endif
gcc claims about #elif: "error: #elif with no expression".
I do not understand this error, because I provide "RELEASE_TEST" expression to "elif".
How to make my code working?
RELEASE_TEST is defined, but with no value, so this expands to just #elif, which isn't valid.
You could either use #elif defined(RELEASE_TEST) to test if it's defined at all, or #define RELEASE_TEST 1, which would cause that line to expand to #elif 1.

GCC - display preprocessed value during preprocessing

Is there a way to make GCC display the value of some preprocessed value during preprocessing? In particular, if I run the equivalent of:
gcc input.c -E >/dev/null
Is there a way to obtain the actual value of an expanded macro? Consider this example:
#if defined(A)
#define B bar
#else
#define B foo
#endif
#define XSTR(x) STR(x)
#define STR(x) #x
int main() {
#pragma message "B is " XSTR(B)
#error DIE
B a = 2;
return 0;
}
I would like to find out that B is foo in this case.
In my actual setup, I do not have access to the entirely preprocessed file, and I cannot remove the -E flag.
Rationale: I have a complex file setup with lots of syntax errors due to incorrectly defined macros, and the fastest way to debug it would be to use this #pragma/#error combination to find out the actual value, stop compilation, manually fix it, and run GCC again to find out where the next error will occur.

Resources