tags:

views:

204

answers:

3

I have a large codebase that uses a number of unsafe functions, such as gmtime and strtok. Rather than trying to search through the codebase and replace these wholesale, I would like to make the compiler emit a warning or error when it sees them (to highlight the problem to maintenance developers). Is this possible with GCC?

I already know about __attribute__((deprecated)), but AFAIK I can't use it since I don't have control of the header files where these functions are declared.

A: 

with ld, you can use the --wrap option to intercept the functions when they are called and make them call a custom function that prints out a warning and/or calls a safer function of your choice. Unfortunately this will only solve the problem at runtime instead of compile time though.

Have a look at --wrap here: ld man page

Salgar
I saw that too, but I can't afford to push the problem to runtime. I want the *developer* to see this, not the *users*.Though, if I specify a list of symbols to wrap without providing `__wrap_<foo>` implementations, I should get linker errors, which is OK...
Tom
+3  A: 

Try this in a source file, with a gcc enough recent it should avoid developers using these both functions.

#pragma GCC poison gmtime
#pragma GCC poison strtok

The downside of it is that it is only valid for one compilation unit. If you use precompiled headers (which you surely do if your project is big), you could put them there. At least this solution does not involve decorating function declarations in system headers and works at compile time.

Poison is maybe a bit hard as it produces errors and not warnings. Does anyone know how to weaken it? At least it is a nice way to enforce a DO NOT USE FUNCTION xxx policy.

jdehaan
I'm happy with an error. I'll check this out. Also, no, we do not use precompiled headers. Linkage dominates our build-time already (and doesn't parallelize so nicely), so there's no benefit in optimizing the compilation side of it.
Tom
Do you have to make sure that you don't include any standard headers after using the poison pragma? Or does it only error for function calls and not declarations?
bk1e
Poisoning reacts on both usage and declaration. That adds the constraint you mention. I am using it at the end of the precompiled header and did not really care about this before.
jdehaan
+8  A: 

Create a custom header deprecated.h. In there, create your own wrapper functions, deprecated_strtok() etcetera that merely call strtok. Mark those with __attribute__((deprecated)). Below those definitions, #define strtok deprecated_strtok. Finally, use -include deprecated.h

MSalters
The only thing I don't like about this is that I have to repeat the entire function signature, and a transcription error (or slight change in interface between gcc/libc versions) can produce mysterious linkage errors. However, using this strategy I can use both `__attribute__((deprecated))` for suggestions, and `#pragma GCC poison` for mandates.
Tom