This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

scope of a #define

C6000 Codegen 6.1.19

Here's a snippet from a header file:

IQmath_inline_all.h said:


static inline I32_IQ _atoIQN(const char *A, U32_IQ q_value)      
{
#define c1 ((I32_IQ)(0xffffffff))
#define c2 ((I64_IQ)(0xffffffff80000000))
#define c3 ((I64_IQ)(0x7fffffff))
#define c4 ((I32_IQ)(0x80000000))
#define c5 ((0xffffffff))
#define c6 ((0x80000000))

Inside a C source file we are including this header file.  It just so happens there is a local variable c1 inside the source file like this:

unsigned char c1 = 0;

This causes the compiler to throw an error related to I32_IQ.  I have worked around the problem temporarily by commenting out the function _atoIQN inside IQmath_inline_all.h.  However, I'm trying to determine if there's something we should do differently in _atoIQN such as adding #undef statements, or if maybe this is a compiler bug, i.e. should the scope of the #define be limited to the function it's in?  Perhaps since it's inlined the scope expands to the function into which it is being inlined?

Hopefully one of you codegen gurus can set me straight.  :)

Thanks,
Brad

 

  • This is just how the C preprocessor works, it is not a bug.  The C preprocessor operates over the entire text of the file, without regard to function boundaries or any other syntactic structure.  You should consider the preprocessor as a separate pass which does its work and ends before the compiler starts looking at the code.

    You should try hard to avoid using the same identifier for macros as you do for other identifiers (such as variables).  I strongly recommend the common practice of making your macros names all upper-case.

    You should try to avoid using #undef, the code will get pretty confusing quickly.

  • if you want these definitions to be function-local, you should declare them as local variables.  The optimizer will generate the same code.

    static inline I32_IQ _atoIQN(const char *A, U32_IQ q_value)
    {
        const I32_IQ c1 = (I32_IQ)0xffffffff;
        const I64_IQ c2 = (I64_IQ)0xffffffff80000000;
        const I64_IQ c3 = (I64_IQ)0x7fffffff;
        const I32_IQ c4 = (I32_IQ)0x80000000;
        const uint32_t c5 = 0xffffffff;
        const uint32_t c6 = 0x80000000;