4

A bug in gcc-4.4 causes the #ident directive to emit a warning. We don't allow warnings in our compiler (-Werror) so I need to turn these off when compiled on certain GCC compiler versions. (See Best replacement for GCC #ident)

$ echo '#ident "FAILS on gcc-4.3.3"' > test.c
$ gcc-4.4 -c test.c
test.c:1: warning: #ident is a deprecated GCC extension

Since these occur in several locations I want to replace them with a macro which would conditionally emit either nothing (or something that approximates #ident) on those "bad" compilers or with the actual #ident directive on all others. Ideally, something like this:

# test2.c
#ifndef HAS_HASH_IDENT
#  define IDENT(x) //-- NO-OP
#else
#  define IDENT(x) #ident x
#endif

This doesn't work because the preprocessor chokes on the # of #ident, as it is interpreted as the stringize operator when used in a macro.

$ gcc-4.5 -Wall -E test2.c
test2.c:4:22: error: '#' is not followed by a macro parameter

I tried several macro redirection tricks, but nothing I came up with would satisfy the preprocessor.

Is something like this even possible?


Note: The #ident directive is passed on intact to the compiler by the preprocessor, so the problem I'm having is not restricted by some sort of preprocessor recursion limitation.

$ gcc-4.5 -E test.c         
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"
#ident "FAILS on gcc-4.3.3"
Community
  • 1
  • 1
Phil Hord
  • 12,780
  • 1
  • 26
  • 30
  • 3
    If you're going to be editing every occurrence of `#ident` anyway, why not just bracket them in `#ifdef`? – Ilmari Karonen Oct 03 '11 at 18:25
  • i see you have problems with gcc 4.4, but are using 4.5 aswell. Does this bug still exists? and what sort of bug it is? – fazo Oct 03 '11 at 18:31
  • @fazo The bug is that someone marked the #ident directive as deprecated in gcc 4.4. This was corrected in gcc 4.5, so it is only on machines with gcc 4.4 installed that this is a problem for me. But I have dozens of developers using this shared code, and I don't want to always have to explain the same solution (install gcc 4.5). But my question is really about macro-izing the #ident directive. – Phil Hord Feb 08 '12 at 23:42
  • You can have your build system make a copy of affected files, remove `#ident` from the copy and compile those instead. Very hacky but should work. – kichik Apr 02 '16 at 20:29

2 Answers2

1

Perhaps you just want to use this option

-fno-ident Ignore the #ident directive.

maybe this switches off the warning, too.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Good idea, but it didn't work. `$ gcc-4.4 -fno-ident -c test.c` `test.c:1: warning: #ident is a deprecated GCC extension` – Phil Hord Jul 23 '12 at 19:28
0

Have you tried selectively muting that specific warning? Something along the lines of https://stackoverflow.com/a/3125889/2003487

Community
  • 1
  • 1
Wojciech Migda
  • 738
  • 7
  • 16