Search code examples
c++visual-c++gccgcc-warningmidl

Avoid gcc warning for (generated) tokens after preprocessing directive?


Microsoft's MIDL compiler generates C/C++ source code files that are slightly invalid, like the code in this extract:

#ifndef CLSID_DEFINED
#define CLSID_DEFINED
typedef IID CLSID;
#endif // CLSID_DEFINED

#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
        const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}

#endif !_MIDL_USE_GUIDDEF_

The tokens after #endif are ignored by Visual C++, but the Holy Standard require nothing there, and so g++ errs out, and even gcc (compiling as C) yields a warning:

H:\dev\tools\better keyboard\test>gcc com_server\com_server_i.c -c
com_server\com_server_i.c:68:8: warning: extra tokens at end of #endif directive
 #endif !_MIDL_USE_GUIDDEF_
        ^

H:\dev\tools\better keyboard\test>_

It gets tiresome and annoying to manually fix up that code each time that it's generated.

Is there some better way to avoid this apparently unnamed warning, assuming that gcc must compile the code?

I have looked at an existing question roughly about this, but to no avail.


Solution

  • Converting comments into an answer.

    The simplest mechanism is probably to post-process the generated code:

    sed -i.bak -e 's/^#endif .*/#endif/' com_server/com_server_i.c
    

    or equivalent. Or you can preserve the material after the #endif but put a comment there:

    sed -i.bak -e 's%^#endif \(.*\)%#endif // \1%' com_server/com_server_i.c
    

    If you're using a makefile, it is pretty easy to add the post-processing as an extra operation after the invocation of the MIDL compiler.

    The cross-referenced question won't readily help; the ! cannot be removed by macro definition. Actually, the presence of a macro after the #endif elicits the warning even if the macro expands to nothing.

    Have you checked the Microsoft bug reports for the MIDL compiler (to see whether it is a known problem that they decline to fix)? And have you checked the options to the MIDL compiler to see if there's anything that would fix this?